Class: SyntaxTree::BodyStmt
Overview
bodystmt can’t actually determine its bounds appropriately because it doesn’t necessarily know where it started. So the parent node needs to report back down into this one where it goes.
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#else_clause ⇒ Object
readonly
- nil | Statements
-
the optional set of statements inside the else clause.
-
#ensure_clause ⇒ Object
readonly
- nil | Ensure
-
the optional ensure clause.
-
#rescue_clause ⇒ Object
readonly
- nil | Rescue
-
the optional rescue chain attached to the begin clause.
-
#statements ⇒ Object
readonly
- Statements
-
the list of statements inside the begin clause.
Attributes inherited from Node
Instance Method Summary collapse
- #bind(start_char, end_char) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #deconstruct_keys(keys) ⇒ Object
- #empty? ⇒ Boolean
- #format(q) ⇒ Object
-
#initialize(statements:, rescue_clause:, else_clause:, ensure_clause:, location:, comments: []) ⇒ BodyStmt
constructor
A new instance of BodyStmt.
- #pretty_print(q) ⇒ Object
- #to_json(*opts) ⇒ Object
Constructor Details
#initialize(statements:, rescue_clause:, else_clause:, ensure_clause:, location:, comments: []) ⇒ BodyStmt
Returns a new instance of BodyStmt.
2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 |
# File 'lib/syntax_tree/node.rb', line 2134 def initialize( statements:, rescue_clause:, else_clause:, ensure_clause:, location:, comments: [] ) @statements = statements @rescue_clause = rescue_clause @else_clause = else_clause @ensure_clause = ensure_clause @location = location @comments = comments end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
2132 2133 2134 |
# File 'lib/syntax_tree/node.rb', line 2132 def comments @comments end |
#else_clause ⇒ Object (readonly)
- nil | Statements
-
the optional set of statements inside the else clause
2126 2127 2128 |
# File 'lib/syntax_tree/node.rb', line 2126 def else_clause @else_clause end |
#ensure_clause ⇒ Object (readonly)
- nil | Ensure
-
the optional ensure clause
2129 2130 2131 |
# File 'lib/syntax_tree/node.rb', line 2129 def ensure_clause @ensure_clause end |
#rescue_clause ⇒ Object (readonly)
- nil | Rescue
-
the optional rescue chain attached to the begin clause
2123 2124 2125 |
# File 'lib/syntax_tree/node.rb', line 2123 def rescue_clause @rescue_clause end |
#statements ⇒ Object (readonly)
- Statements
-
the list of statements inside the begin clause
2120 2121 2122 |
# File 'lib/syntax_tree/node.rb', line 2120 def statements @statements end |
Instance Method Details
#bind(start_char, end_char) ⇒ Object
2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 |
# File 'lib/syntax_tree/node.rb', line 2150 def bind(start_char, end_char) @location = Location.new( start_line: location.start_line, start_char: start_char, end_line: location.end_line, end_char: end_char ) parts = [rescue_clause, else_clause, ensure_clause] # Here we're going to determine the bounds for the statements consequent = parts.compact.first statements.bind( start_char, consequent ? consequent.location.start_char : end_char ) # Next we're going to determine the rescue clause if there is one if rescue_clause consequent = parts.drop(1).compact.first rescue_clause.bind_end( consequent ? consequent.location.start_char : end_char ) end end |
#child_nodes ⇒ Object Also known as: deconstruct
2181 2182 2183 |
# File 'lib/syntax_tree/node.rb', line 2181 def child_nodes [statements, rescue_clause, else_clause, ensure_clause] end |
#deconstruct_keys(keys) ⇒ Object
2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 |
# File 'lib/syntax_tree/node.rb', line 2187 def deconstruct_keys(keys) { statements: statements, rescue_clause: rescue_clause, else_clause: else_clause, ensure_clause: ensure_clause, location: location, comments: comments } end |
#empty? ⇒ Boolean
2177 2178 2179 |
# File 'lib/syntax_tree/node.rb', line 2177 def empty? statements.empty? && !rescue_clause && !else_clause && !ensure_clause end |
#format(q) ⇒ Object
2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 |
# File 'lib/syntax_tree/node.rb', line 2198 def format(q) q.group do q.format(statements) unless statements.empty? if rescue_clause q.nest(-2) do q.breakable(force: true) q.format(rescue_clause) end end if else_clause q.nest(-2) do q.breakable(force: true) q.text("else") end q.breakable(force: true) q.format(else_clause) end if ensure_clause q.nest(-2) do q.breakable(force: true) q.format(ensure_clause) end end end end |
#pretty_print(q) ⇒ Object
2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 |
# File 'lib/syntax_tree/node.rb', line 2227 def pretty_print(q) q.group(2, "(", ")") do q.text("bodystmt") q.breakable q.pp(statements) if rescue_clause q.breakable q.pp(rescue_clause) end if else_clause q.breakable q.pp(else_clause) end if ensure_clause q.breakable q.pp(ensure_clause) end q.pp(Comment::List.new(comments)) end end |
#to_json(*opts) ⇒ Object
2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 |
# File 'lib/syntax_tree/node.rb', line 2253 def to_json(*opts) { type: :bodystmt, stmts: statements, rsc: rescue_clause, els: else_clause, ens: ensure_clause, loc: location, cmts: comments }.to_json(*opts) end |