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
- #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.
- #pretty_print(q) ⇒ Object
- #to_json(*opts) ⇒ Object
Constructor Details
#initialize(constant:, superclass:, bodystmt:, location:, comments: []) ⇒ ClassDeclaration
2973 2974 2975 2976 2977 2978 2979 |
# File 'lib/syntax_tree/node.rb', line 2973 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
2968 2969 2970 |
# File 'lib/syntax_tree/node.rb', line 2968 def bodystmt @bodystmt end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
2971 2972 2973 |
# File 'lib/syntax_tree/node.rb', line 2971 def comments @comments end |
#constant ⇒ Object (readonly)
- ConstPathRef | ConstRef | TopConstRef
-
the name of the class being
defined
2962 2963 2964 |
# File 'lib/syntax_tree/node.rb', line 2962 def constant @constant end |
#superclass ⇒ Object (readonly)
- nil | untyped
-
the optional superclass declaration
2965 2966 2967 |
# File 'lib/syntax_tree/node.rb', line 2965 def superclass @superclass end |
Instance Method Details
#child_nodes ⇒ Object Also known as: deconstruct
2981 2982 2983 |
# File 'lib/syntax_tree/node.rb', line 2981 def child_nodes [constant, superclass, bodystmt] end |
#deconstruct_keys(keys) ⇒ Object
2987 2988 2989 2990 2991 2992 2993 2994 2995 |
# File 'lib/syntax_tree/node.rb', line 2987 def deconstruct_keys(keys) { constant: constant, superclass: superclass, bodystmt: bodystmt, location: location, comments: comments } end |
#format(q) ⇒ Object
2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 |
# File 'lib/syntax_tree/node.rb', line 2997 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
3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 |
# File 'lib/syntax_tree/node.rb', line 3031 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
3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 |
# File 'lib/syntax_tree/node.rb', line 3050 def to_json(*opts) { type: :class, constant: constant, superclass: superclass, bodystmt: bodystmt, loc: location, cmts: comments }.to_json(*opts) end |