@@ -187,14 +187,20 @@ class Lexer
187
187
EXPR_BEG = 0x1 # :nodoc:
188
188
EXPR_LABEL = 0x400 # :nodoc:
189
189
190
+ # It is used to determine whether `do` is of the token type `kDO` or `kDO_LAMBDA`.
191
+ #
192
+ # NOTE: In edge cases like `-> (foo = -> (bar) {}) do end`, please note that `kDO` is still returned
193
+ # instead of `kDO_LAMBDA`, which is expected: https://github.com/ruby/prism/pull/3046
194
+ LAMBDA_TOKEN_TYPES = [ :kDO_LAMBDA , :tLAMBDA , :tLAMBEG ]
195
+
190
196
# The `PARENTHESIS_LEFT` token in Prism is classified as either `tLPAREN` or `tLPAREN2` in the Parser gem.
191
197
# The following token types are listed as those classified as `tLPAREN`.
192
198
LPAREN_CONVERSION_TOKEN_TYPES = [
193
199
:kBREAK , :kCASE , :tDIVIDE , :kFOR , :kIF , :kNEXT , :kRETURN , :kUNTIL , :kWHILE , :tAMPER , :tANDOP , :tBANG , :tCOMMA , :tDOT2 , :tDOT3 ,
194
200
:tEQL , :tLPAREN , :tLPAREN2 , :tLSHFT , :tNL , :tOP_ASGN , :tOROP , :tPIPE , :tSEMI , :tSTRING_DBEG , :tUMINUS , :tUPLUS
195
201
]
196
202
197
- private_constant :TYPES , :EXPR_BEG , :EXPR_LABEL , :LPAREN_CONVERSION_TOKEN_TYPES
203
+ private_constant :TYPES , :EXPR_BEG , :EXPR_LABEL , :LAMBDA_TOKEN_TYPES , : LPAREN_CONVERSION_TOKEN_TYPES
198
204
199
205
# The Parser::Source::Buffer that the tokens were lexed from.
200
206
attr_reader :source_buffer
@@ -236,6 +242,13 @@ def to_a
236
242
location = Range . new ( source_buffer , offset_cache [ token . location . start_offset ] , offset_cache [ token . location . end_offset ] )
237
243
238
244
case type
245
+ when :kDO
246
+ types = tokens . map ( &:first )
247
+ nearest_lambda_token_type = types . reverse . find { |type | LAMBDA_TOKEN_TYPES . include? ( type ) }
248
+
249
+ if nearest_lambda_token_type == :tLAMBDA
250
+ type = :kDO_LAMBDA
251
+ end
239
252
when :tCHARACTER
240
253
value . delete_prefix! ( "?" )
241
254
when :tCOMMENT
0 commit comments