Class: SyntaxTree::ClassDeclaration
- Defined in:
- lib/syntax_tree/node.rb
Overview
Class represents defining a class using the class keyword.
class Container
end
Classes can have path names as their class name in case it’s being nested under a namespace, as in:
class Namespace::Container
end
Classes can also be defined as a top-level path, in the case that it’s already in a namespace but you want to define it at the top-level instead, as in:
module OtherNamespace
class ::Namespace::Container
end
end
All of these declarations can also have an optional superclass reference, as in:
class Child < Parent
end
That superclass can actually be any Ruby expression, it doesn’t necessarily need to be a constant, as in:
class Child < method
end
Instance Attribute Summary collapse
-
#bodystmt ⇒ Object
readonly
- BodyStmt
-
the expressions to execute within the context of the class.
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#constant ⇒ Object
readonly
- ConstPathRef | ConstRef | TopConstRef
-
the name of the class being defined.
-
#superclass ⇒ Object
readonly
- nil | untyped
-
the optional superclass declaration.
Attributes inherited from Node
Instance Method Summary collapse
- #===(other) ⇒ Object
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #copy(constant: nil, superclass: nil, bodystmt: nil, location: nil) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(constant:, superclass:, bodystmt:, location:) ⇒ ClassDeclaration
constructor
A new instance of ClassDeclaration.
Methods inherited from Node
#construct_keys, #pretty_print, #to_json
Constructor Details
#initialize(constant:, superclass:, bodystmt:, location:) ⇒ ClassDeclaration
Returns a new instance of ClassDeclaration.
3285 3286 3287 3288 3289 3290 3291 |
# File 'lib/syntax_tree/node.rb', line 3285 def initialize(constant:, superclass:, bodystmt:, location:) @constant = constant @superclass = superclass @bodystmt = bodystmt @location = location @comments = [] end |
Instance Attribute Details
#bodystmt ⇒ Object (readonly)
- BodyStmt
-
the expressions to execute within the context of the class
3280 3281 3282 |
# File 'lib/syntax_tree/node.rb', line 3280 def bodystmt @bodystmt end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
3283 3284 3285 |
# File 'lib/syntax_tree/node.rb', line 3283 def comments @comments end |
#constant ⇒ Object (readonly)
- ConstPathRef | ConstRef | TopConstRef
-
the name of the class being
defined
3274 3275 3276 |
# File 'lib/syntax_tree/node.rb', line 3274 def constant @constant end |
#superclass ⇒ Object (readonly)
- nil | untyped
-
the optional superclass declaration
3277 3278 3279 |
# File 'lib/syntax_tree/node.rb', line 3277 def superclass @superclass end |
Instance Method Details
#===(other) ⇒ Object
3348 3349 3350 3351 |
# File 'lib/syntax_tree/node.rb', line 3348 def ===(other) other.is_a?(ClassDeclaration) && constant === other.constant && superclass === other.superclass && bodystmt === other.bodystmt end |
#accept(visitor) ⇒ Object
3293 3294 3295 |
# File 'lib/syntax_tree/node.rb', line 3293 def accept(visitor) visitor.visit_class(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
3297 3298 3299 |
# File 'lib/syntax_tree/node.rb', line 3297 def child_nodes [constant, superclass, bodystmt] end |
#copy(constant: nil, superclass: nil, bodystmt: nil, location: nil) ⇒ Object
3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 |
# File 'lib/syntax_tree/node.rb', line 3301 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 |
#deconstruct_keys(_keys) ⇒ Object
3316 3317 3318 3319 3320 3321 3322 3323 3324 |
# File 'lib/syntax_tree/node.rb', line 3316 def deconstruct_keys(_keys) { constant: constant, superclass: superclass, bodystmt: bodystmt, location: location, comments: comments } end |
#format(q) ⇒ Object
3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 |
# File 'lib/syntax_tree/node.rb', line 3326 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 |