@@ -2131,13 +2131,18 @@ def ===(other)
2131
2131
end
2132
2132
end
2133
2133
2134
- # BlockVar represents the parameters being declared for a block. Effectively
2135
- # this node is everything contained within the pipes. This includes all of the
2136
- # various parameter types, as well as block-local variable declarations.
2134
+ # BlockVar represents the parameters being declared for a block or lambda.
2135
+ # This includes all of the various parameter types, as well as
2136
+ # block-local/lambda -local variable declarations.
2137
2137
#
2138
2138
# method do |positional, optional = value, keyword:, █ local|
2139
2139
# end
2140
2140
#
2141
+ # OR
2142
+ #
2143
+ # -> (positional, optional = value, keyword:, █ local) do
2144
+ # end
2145
+ #
2141
2146
class BlockVar < Node
2142
2147
# [Params] the parameters being declared with the block
2143
2148
attr_reader :params
@@ -2148,10 +2153,15 @@ class BlockVar < Node
2148
2153
# [Array[ Comment | EmbDoc ]] the comments attached to this node
2149
2154
attr_reader :comments
2150
2155
2151
- def initialize ( params :, locals :, location :)
2156
+ # [boolean] whether or not the variables are within pipes
2157
+ attr_reader :pipe
2158
+ alias pipe? pipe
2159
+
2160
+ def initialize ( params :, locals :, location :, pipe :)
2152
2161
@params = params
2153
2162
@locals = locals
2154
2163
@location = location
2164
+ @pipe = pipe
2155
2165
@comments = [ ]
2156
2166
end
2157
2167
@@ -2163,12 +2173,13 @@ def child_nodes
2163
2173
[ params , *locals ]
2164
2174
end
2165
2175
2166
- def copy ( params : nil , locals : nil , location : nil )
2176
+ def copy ( params : nil , locals : nil , location : nil , pipe : nil )
2167
2177
node =
2168
2178
BlockVar . new (
2169
2179
params : params || self . params ,
2170
2180
locals : locals || self . locals ,
2171
- location : location || self . location
2181
+ location : location || self . location ,
2182
+ pipe : pipe || self . pipe
2172
2183
)
2173
2184
2174
2185
node . comments . concat ( comments . map ( &:copy ) )
@@ -2178,7 +2189,13 @@ def copy(params: nil, locals: nil, location: nil)
2178
2189
alias deconstruct child_nodes
2179
2190
2180
2191
def deconstruct_keys ( _keys )
2181
- { params : params , locals : locals , location : location , comments : comments }
2192
+ {
2193
+ params : params ,
2194
+ locals : locals ,
2195
+ location : location ,
2196
+ comments : comments ,
2197
+ pipe : pipe
2198
+ }
2182
2199
end
2183
2200
2184
2201
# Within the pipes of the block declaration, we don't want any spaces. So
@@ -2194,23 +2211,23 @@ def call(q)
2194
2211
SEPARATOR = Separator . new . freeze
2195
2212
2196
2213
def format ( q )
2197
- q . text ( "|" )
2198
- q . group do
2199
- q . remove_breaks ( q . format ( params ) )
2214
+ pipe? ? q . remove_breaks ( q . format ( params ) ) : q . format ( params )
2200
2215
2201
- if locals . any?
2202
- q . text ( "; " )
2203
- q . seplist ( locals , SEPARATOR ) { |local | q . format ( local ) }
2204
- end
2216
+ if locals . any?
2217
+ q . text ( "; " )
2218
+ q . seplist ( locals , SEPARATOR ) { |local | q . format ( local ) }
2205
2219
end
2206
- q . text ( "|" )
2207
2220
end
2208
2221
2209
2222
def ===( other )
2210
2223
other . is_a? ( BlockVar ) && params === other . params &&
2211
2224
ArrayMatch . call ( locals , other . locals )
2212
2225
end
2213
2226
2227
+ def empty?
2228
+ params . empty? && locals . empty?
2229
+ end
2230
+
2214
2231
# When a single required parameter is declared for a block, it gets
2215
2232
# automatically expanded if the values being yielded into it are an array.
2216
2233
def arg0?
@@ -4486,8 +4503,11 @@ def format_break(q, break_opening, break_closing)
4486
4503
q . format ( BlockOpenFormatter . new ( break_opening , opening ) , stackable : false )
4487
4504
4488
4505
if block_var
4489
- q . text ( " " )
4490
- q . format ( block_var )
4506
+ q . text ( " |" )
4507
+ q . group do
4508
+ q . format ( block_var )
4509
+ end
4510
+ q . text ( "|" )
4491
4511
end
4492
4512
4493
4513
unless bodystmt . empty?
@@ -4507,7 +4527,11 @@ def format_flat(q, flat_opening, flat_closing)
4507
4527
4508
4528
if block_var
4509
4529
q . breakable_space
4510
- q . format ( block_var )
4530
+ q . text ( "|" )
4531
+ q . group do
4532
+ q . format ( block_var )
4533
+ end
4534
+ q . text ( "|" )
4511
4535
q . breakable_space
4512
4536
end
4513
4537
@@ -7140,7 +7164,7 @@ def ===(other)
7140
7164
# ->(value) { value * 2 }
7141
7165
#
7142
7166
class Lambda < Node
7143
- # [LambdaVar | Paren] the parameter declaration for this lambda
7167
+ # [BlockVar | Paren] the parameter declaration for this lambda
7144
7168
attr_reader :params
7145
7169
7146
7170
# [BodyStmt | Statements] the expressions to be executed in this lambda
@@ -7258,76 +7282,6 @@ def ===(other)
7258
7282
end
7259
7283
end
7260
7284
7261
- # LambdaVar represents the parameters being declared for a lambda. Effectively
7262
- # this node is everything contained within the parentheses. This includes all
7263
- # of the various parameter types, as well as block-local variable
7264
- # declarations.
7265
- #
7266
- # -> (positional, optional = value, keyword:, █ local) do
7267
- # end
7268
- #
7269
- class LambdaVar < Node
7270
- # [Params] the parameters being declared with the block
7271
- attr_reader :params
7272
-
7273
- # [Array[ Ident ]] the list of block-local variable declarations
7274
- attr_reader :locals
7275
-
7276
- # [Array[ Comment | EmbDoc ]] the comments attached to this node
7277
- attr_reader :comments
7278
-
7279
- def initialize ( params :, locals :, location :)
7280
- @params = params
7281
- @locals = locals
7282
- @location = location
7283
- @comments = [ ]
7284
- end
7285
-
7286
- def accept ( visitor )
7287
- visitor . visit_lambda_var ( self )
7288
- end
7289
-
7290
- def child_nodes
7291
- [ params , *locals ]
7292
- end
7293
-
7294
- def copy ( params : nil , locals : nil , location : nil )
7295
- node =
7296
- LambdaVar . new (
7297
- params : params || self . params ,
7298
- locals : locals || self . locals ,
7299
- location : location || self . location
7300
- )
7301
-
7302
- node . comments . concat ( comments . map ( &:copy ) )
7303
- node
7304
- end
7305
-
7306
- alias deconstruct child_nodes
7307
-
7308
- def deconstruct_keys ( _keys )
7309
- { params : params , locals : locals , location : location , comments : comments }
7310
- end
7311
-
7312
- def empty?
7313
- params . empty? && locals . empty?
7314
- end
7315
-
7316
- def format ( q )
7317
- q . format ( params )
7318
-
7319
- if locals . any?
7320
- q . text ( "; " )
7321
- q . seplist ( locals , BlockVar ::SEPARATOR ) { |local | q . format ( local ) }
7322
- end
7323
- end
7324
-
7325
- def ===( other )
7326
- other . is_a? ( LambdaVar ) && params === other . params &&
7327
- ArrayMatch . call ( locals , other . locals )
7328
- end
7329
- end
7330
-
7331
7285
# LBrace represents the use of a left brace, i.e., {.
7332
7286
class LBrace < Node
7333
7287
# [String] the left brace
0 commit comments