Class: SyntaxTree::Elsif
- Inherits:
-
Object
- Object
- SyntaxTree::Elsif
- Defined in:
- lib/syntax_tree.rb
Overview
Elsif represents another clause in an if or unless chain.
if variable
elsif other_variable
end
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#consequent ⇒ Object
readonly
- nil | Elsif | Else
-
the next clause in the chain.
-
#location ⇒ Object
readonly
- Location
-
the location of this node.
-
#predicate ⇒ Object
readonly
- untyped
-
the expression to be checked.
-
#statements ⇒ Object
readonly
- Statements
-
the expressions to be executed.
Instance Method Summary collapse
- #child_nodes ⇒ Object
- #format(q) ⇒ Object
-
#initialize(predicate:, statements:, consequent:, location:, comments: []) ⇒ Elsif
constructor
A new instance of Elsif.
- #pretty_print(q) ⇒ Object
- #to_json(*opts) ⇒ Object
Constructor Details
#initialize(predicate:, statements:, consequent:, location:, comments: []) ⇒ Elsif
Returns a new instance of Elsif.
5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 |
# File 'lib/syntax_tree.rb', line 5043 def initialize( predicate:, statements:, consequent:, location:, comments: [] ) @predicate = predicate @statements = statements @consequent = consequent @location = location @comments = comments end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
5041 5042 5043 |
# File 'lib/syntax_tree.rb', line 5041 def comments @comments end |
#consequent ⇒ Object (readonly)
- nil | Elsif | Else
-
the next clause in the chain
5035 5036 5037 |
# File 'lib/syntax_tree.rb', line 5035 def consequent @consequent end |
#location ⇒ Object (readonly)
- Location
-
the location of this node
5038 5039 5040 |
# File 'lib/syntax_tree.rb', line 5038 def location @location end |
#predicate ⇒ Object (readonly)
- untyped
-
the expression to be checked
5029 5030 5031 |
# File 'lib/syntax_tree.rb', line 5029 def predicate @predicate end |
#statements ⇒ Object (readonly)
- Statements
-
the expressions to be executed
5032 5033 5034 |
# File 'lib/syntax_tree.rb', line 5032 def statements @statements end |
Instance Method Details
#child_nodes ⇒ Object
5057 5058 5059 |
# File 'lib/syntax_tree.rb', line 5057 def child_nodes [predicate, statements, consequent] end |
#format(q) ⇒ Object
5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 |
# File 'lib/syntax_tree.rb', line 5061 def format(q) q.group do q.group do q.text("elsif ") q.nest("elsif".length - 1) { q.format(predicate) } end unless statements.empty? q.indent do q.breakable(force: true) q.format(statements) end end if consequent q.group do q.breakable(force: true) q.format(consequent) end end end end |
#pretty_print(q) ⇒ Object
5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 |
# File 'lib/syntax_tree.rb', line 5084 def pretty_print(q) q.group(2, "(", ")") do q.text("elsif") q.breakable q.pp(predicate) q.breakable q.pp(statements) if consequent q.breakable q.pp(consequent) end q.pp(Comment::List.new(comments)) end end |
#to_json(*opts) ⇒ Object
5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 |
# File 'lib/syntax_tree.rb', line 5103 def to_json(*opts) { type: :elsif, pred: predicate, stmts: statements, cons: consequent, loc: location, cmts: comments }.to_json(*opts) end |