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
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #deconstruct_keys(keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(constant:, superclass:, bodystmt:, location:, comments: []) ⇒ ClassDeclaration
constructor
A new instance of ClassDeclaration.
Methods inherited from Node
Constructor Details
#initialize(constant:, superclass:, bodystmt:, location:, comments: []) ⇒ ClassDeclaration
Returns a new instance of ClassDeclaration.
2413 2414 2415 2416 2417 2418 2419 |
# File 'lib/syntax_tree/node.rb', line 2413 def initialize(constant:, superclass:, bodystmt:, location:, comments: []) @constant = constant @superclass = superclass @bodystmt = bodystmt @location = location @comments = comments end |
Instance Attribute Details
#bodystmt ⇒ Object (readonly)
- BodyStmt
-
the expressions to execute within the context of the class
2408 2409 2410 |
# File 'lib/syntax_tree/node.rb', line 2408 def bodystmt @bodystmt end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
2411 2412 2413 |
# File 'lib/syntax_tree/node.rb', line 2411 def comments @comments end |
#constant ⇒ Object (readonly)
- ConstPathRef | ConstRef | TopConstRef
-
the name of the class being
defined
2402 2403 2404 |
# File 'lib/syntax_tree/node.rb', line 2402 def constant @constant end |
#superclass ⇒ Object (readonly)
- nil | untyped
-
the optional superclass declaration
2405 2406 2407 |
# File 'lib/syntax_tree/node.rb', line 2405 def superclass @superclass end |
Instance Method Details
#accept(visitor) ⇒ Object
2421 2422 2423 |
# File 'lib/syntax_tree/node.rb', line 2421 def accept(visitor) visitor.visit_class(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
2425 2426 2427 |
# File 'lib/syntax_tree/node.rb', line 2425 def child_nodes [constant, superclass, bodystmt] end |
#deconstruct_keys(keys) ⇒ Object
2431 2432 2433 2434 2435 2436 2437 2438 2439 |
# File 'lib/syntax_tree/node.rb', line 2431 def deconstruct_keys(keys) { constant: constant, superclass: superclass, bodystmt: bodystmt, location: location, comments: comments } end |
#format(q) ⇒ Object
2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 |
# File 'lib/syntax_tree/node.rb', line 2441 def format(q) declaration = -> do q.group do q.text("class ") q.format(constant) if superclass q.text(" < ") q.format(superclass) end end end if bodystmt.empty? q.group do declaration.call q.breakable(force: true) q.text("end") end else q.group do declaration.call q.indent do q.breakable(force: true) q.format(bodystmt) end q.breakable(force: true) q.text("end") end end end |