Class: SyntaxTree::ClassDeclaration
- Inherits:
-
Object
- Object
- SyntaxTree::ClassDeclaration
- Defined in:
- lib/syntax_tree.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.
-
#location ⇒ Object
readonly
- Location
-
the location of this node.
-
#superclass ⇒ Object
readonly
- nil | untyped
-
the optional superclass declaration.
Instance Method Summary collapse
- #child_nodes ⇒ Object
- #format(q) ⇒ Object
-
#initialize(constant:, superclass:, bodystmt:, location:, comments: []) ⇒ ClassDeclaration
constructor
A new instance of ClassDeclaration.
- #pretty_print(q) ⇒ Object
- #to_json(*opts) ⇒ Object
Constructor Details
#initialize(constant:, superclass:, bodystmt:, location:, comments: []) ⇒ ClassDeclaration
Returns a new instance of ClassDeclaration.
3543 3544 3545 3546 3547 3548 3549 |
# File 'lib/syntax_tree.rb', line 3543 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
3535 3536 3537 |
# File 'lib/syntax_tree.rb', line 3535 def bodystmt @bodystmt end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
3541 3542 3543 |
# File 'lib/syntax_tree.rb', line 3541 def comments @comments end |
#constant ⇒ Object (readonly)
- ConstPathRef | ConstRef | TopConstRef
-
the name of the class being
defined
3529 3530 3531 |
# File 'lib/syntax_tree.rb', line 3529 def constant @constant end |
#location ⇒ Object (readonly)
- Location
-
the location of this node
3538 3539 3540 |
# File 'lib/syntax_tree.rb', line 3538 def location @location end |
#superclass ⇒ Object (readonly)
- nil | untyped
-
the optional superclass declaration
3532 3533 3534 |
# File 'lib/syntax_tree.rb', line 3532 def superclass @superclass end |
Instance Method Details
#child_nodes ⇒ Object
3551 3552 3553 |
# File 'lib/syntax_tree.rb', line 3551 def child_nodes [constant, superclass, bodystmt] end |
#format(q) ⇒ Object
3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 |
# File 'lib/syntax_tree.rb', line 3555 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 |
#pretty_print(q) ⇒ Object
3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 |
# File 'lib/syntax_tree.rb', line 3589 def pretty_print(q) q.group(2, "(", ")") do q.text("class") q.breakable q.pp(constant) if superclass q.breakable q.pp(superclass) end q.breakable q.pp(bodystmt) q.pp(Comment::List.new(comments)) end end |
#to_json(*opts) ⇒ Object
3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 |
# File 'lib/syntax_tree.rb', line 3608 def to_json(*opts) { type: :class, constant: constant, superclass: superclass, bodystmt: bodystmt, loc: location, cmts: comments }.to_json(*opts) end |