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