@@ -2087,13 +2087,18 @@ def ===(other)
2087
2087
end
2088
2088
end
2089
2089
2090
- # BlockVar represents the parameters being declared for a block. Effectively
2091
- # this node is everything contained within the pipes. This includes all of the
2092
- # various parameter types, as well as block-local variable declarations.
2090
+ # BlockVar represents the parameters being declared for a block or lambda.
2091
+ # This includes all of the various parameter types, as well as
2092
+ # block-local/lambda -local variable declarations.
2093
2093
#
2094
2094
# method do |positional, optional = value, keyword:, █ local|
2095
2095
# end
2096
2096
#
2097
+ # OR
2098
+ #
2099
+ # -> (positional, optional = value, keyword:, █ local) do
2100
+ # end
2101
+ #
2097
2102
class BlockVar < Node
2098
2103
# [Params] the parameters being declared with the block
2099
2104
attr_reader :params
@@ -2104,10 +2109,15 @@ class BlockVar < Node
2104
2109
# [Array[ Comment | EmbDoc ]] the comments attached to this node
2105
2110
attr_reader :comments
2106
2111
2107
- def initialize ( params :, locals :, location :)
2112
+ # [boolean] whether or not the variables are within pipes
2113
+ attr_reader :pipe
2114
+ alias pipe? pipe
2115
+
2116
+ def initialize ( params :, locals :, location :, pipe :)
2108
2117
@params = params
2109
2118
@locals = locals
2110
2119
@location = location
2120
+ @pipe = pipe
2111
2121
@comments = [ ]
2112
2122
end
2113
2123
@@ -2119,12 +2129,13 @@ def child_nodes
2119
2129
[ params , *locals ]
2120
2130
end
2121
2131
2122
- def copy ( params : nil , locals : nil , location : nil )
2132
+ def copy ( params : nil , locals : nil , location : nil , pipe : nil )
2123
2133
node =
2124
2134
BlockVar . new (
2125
2135
params : params || self . params ,
2126
2136
locals : locals || self . locals ,
2127
- location : location || self . location
2137
+ location : location || self . location ,
2138
+ pipe : pipe || self . pipe
2128
2139
)
2129
2140
2130
2141
node . comments . concat ( comments . map ( &:copy ) )
@@ -2134,7 +2145,13 @@ def copy(params: nil, locals: nil, location: nil)
2134
2145
alias deconstruct child_nodes
2135
2146
2136
2147
def deconstruct_keys ( _keys )
2137
- { params : params , locals : locals , location : location , comments : comments }
2148
+ {
2149
+ params : params ,
2150
+ locals : locals ,
2151
+ location : location ,
2152
+ comments : comments ,
2153
+ pipe : pipe
2154
+ }
2138
2155
end
2139
2156
2140
2157
# Within the pipes of the block declaration, we don't want any spaces. So
@@ -2150,30 +2167,44 @@ def call(q)
2150
2167
SEPARATOR = Separator . new . freeze
2151
2168
2152
2169
def format ( q )
2153
- q . text ( "|" )
2154
- q . group do
2155
- q . remove_breaks ( q . format ( params ) )
2156
-
2157
- if locals . any?
2158
- q . text ( "; " )
2159
- q . seplist ( locals , SEPARATOR ) { |local | q . format ( local ) }
2170
+ if pipe?
2171
+ q . text ( "|" )
2172
+ q . group do
2173
+ q . remove_breaks ( q . format ( params ) )
2174
+ format_locals ( q )
2160
2175
end
2176
+ q . text ( "|" )
2177
+ else
2178
+ q . format ( params )
2179
+ format_locals ( q )
2161
2180
end
2162
- q . text ( "|" )
2163
2181
end
2164
2182
2165
2183
def ===( other )
2166
2184
other . is_a? ( BlockVar ) && params === other . params &&
2167
2185
ArrayMatch . call ( locals , other . locals )
2168
2186
end
2169
2187
2188
+ def empty?
2189
+ params . empty? && locals . empty?
2190
+ end
2191
+
2170
2192
# When a single required parameter is declared for a block, it gets
2171
2193
# automatically expanded if the values being yielded into it are an array.
2172
2194
def arg0?
2173
2195
params . requireds . length == 1 && params . optionals . empty? &&
2174
2196
params . rest . nil? && params . posts . empty? && params . keywords . empty? &&
2175
2197
params . keyword_rest . nil? && params . block . nil?
2176
2198
end
2199
+
2200
+ private
2201
+
2202
+ def format_locals ( q )
2203
+ if locals . any?
2204
+ q . text ( "; " )
2205
+ q . seplist ( locals , SEPARATOR ) { |local | q . format ( local ) }
2206
+ end
2207
+ end
2177
2208
end
2178
2209
2179
2210
# BlockArg represents declaring a block parameter on a method definition.
@@ -7076,7 +7107,7 @@ def ===(other)
7076
7107
# ->(value) { value * 2 }
7077
7108
#
7078
7109
class Lambda < Node
7079
- # [LambdaVar | Paren] the parameter declaration for this lambda
7110
+ # [BlockVar | Paren] the parameter declaration for this lambda
7080
7111
attr_reader :params
7081
7112
7082
7113
# [BodyStmt | Statements] the expressions to be executed in this lambda
@@ -7192,76 +7223,6 @@ def ===(other)
7192
7223
end
7193
7224
end
7194
7225
7195
- # LambdaVar represents the parameters being declared for a lambda. Effectively
7196
- # this node is everything contained within the parentheses. This includes all
7197
- # of the various parameter types, as well as block-local variable
7198
- # declarations.
7199
- #
7200
- # -> (positional, optional = value, keyword:, █ local) do
7201
- # end
7202
- #
7203
- class LambdaVar < Node
7204
- # [Params] the parameters being declared with the block
7205
- attr_reader :params
7206
-
7207
- # [Array[ Ident ]] the list of block-local variable declarations
7208
- attr_reader :locals
7209
-
7210
- # [Array[ Comment | EmbDoc ]] the comments attached to this node
7211
- attr_reader :comments
7212
-
7213
- def initialize ( params :, locals :, location :)
7214
- @params = params
7215
- @locals = locals
7216
- @location = location
7217
- @comments = [ ]
7218
- end
7219
-
7220
- def accept ( visitor )
7221
- visitor . visit_lambda_var ( self )
7222
- end
7223
-
7224
- def child_nodes
7225
- [ params , *locals ]
7226
- end
7227
-
7228
- def copy ( params : nil , locals : nil , location : nil )
7229
- node =
7230
- LambdaVar . new (
7231
- params : params || self . params ,
7232
- locals : locals || self . locals ,
7233
- location : location || self . location
7234
- )
7235
-
7236
- node . comments . concat ( comments . map ( &:copy ) )
7237
- node
7238
- end
7239
-
7240
- alias deconstruct child_nodes
7241
-
7242
- def deconstruct_keys ( _keys )
7243
- { params : params , locals : locals , location : location , comments : comments }
7244
- end
7245
-
7246
- def empty?
7247
- params . empty? && locals . empty?
7248
- end
7249
-
7250
- def format ( q )
7251
- q . format ( params )
7252
-
7253
- if locals . any?
7254
- q . text ( "; " )
7255
- q . seplist ( locals , BlockVar ::SEPARATOR ) { |local | q . format ( local ) }
7256
- end
7257
- end
7258
-
7259
- def ===( other )
7260
- other . is_a? ( LambdaVar ) && params === other . params &&
7261
- ArrayMatch . call ( locals , other . locals )
7262
- end
7263
- end
7264
-
7265
7226
# LBrace represents the use of a left brace, i.e., {.
7266
7227
class LBrace < Node
7267
7228
# [String] the left brace
0 commit comments