class SyntaxTree::ClassDeclaration
end
class Child < method
need to be a constant, as in:
That superclass can actually be any Ruby expression, it doesn’t necessarily
end
class Child < Parent
in:
All of these declarations can also have an optional superclass reference, as
end
end
class ::Namespace::Container
module OtherNamespace
as in:
already in a namespace but you want to define it at the top-level instead,
Classes can also be defined as a top-level path, in the case that it’s
end
class Namespace::Container
under a namespace, as in:
Classes can have path names as their class name in case it’s being nested
end
class Container
Class represents defining a class using the class
keyword.
def ===(other)
def ===(other) other.is_a?(ClassDeclaration) && constant === other.constant && superclass === other.superclass && bodystmt === other.bodystmt end
def accept(visitor)
def accept(visitor) visitor.visit_class(self) end
def child_nodes
def child_nodes [constant, superclass, bodystmt] end
def copy(constant: nil, superclass: nil, bodystmt: nil, location: nil)
def copy(constant: nil, superclass: nil, bodystmt: nil, location: nil) node = ClassDeclaration.new( constant: constant || self.constant, superclass: superclass || self.superclass, bodystmt: bodystmt || self.bodystmt, location: location || self.location ) node.comments.concat(comments.map(&:copy)) node end
def deconstruct_keys(_keys)
def deconstruct_keys(_keys) { constant: constant, superclass: superclass, bodystmt: bodystmt, location: location, comments: comments } end
def format(q)
def format(q) if bodystmt.empty? q.group do format_declaration(q) q.breakable_force q.text("end") end else q.group do format_declaration(q) q.indent do q.breakable_force q.format(bodystmt) end q.breakable_force q.text("end") end end end
def format_declaration(q)
def format_declaration(q) q.group do q.text("class ") q.format(constant) if superclass q.text(" < ") q.format(superclass) end end end
def initialize(constant:, superclass:, bodystmt:, location:)
def initialize(constant:, superclass:, bodystmt:, location:) @constant = constant @superclass = superclass @bodystmt = bodystmt @location = location @comments = [] end