Skip to content

Commit 399d3be

Browse files
authored
Merge pull request #3616 from presidentbeef/it_in_stabby_lambda
RubyParser translation for stabby lambdas with `it`
2 parents 3e8066e + 7362b11 commit 399d3be

5 files changed

Lines changed: 58 additions & 48 deletions

File tree

lib/prism/translation/ruby_parser.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,8 +1151,8 @@ def visit_keyword_rest_parameter_node(node)
11511151
def visit_lambda_node(node)
11521152
parameters =
11531153
case node.parameters
1154-
when nil, NumberedParametersNode
1155-
s(node, :args)
1154+
when nil, ItParametersNode, NumberedParametersNode
1155+
0
11561156
else
11571157
visit(node.parameters)
11581158
end

snapshots/it.txt

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,46 @@
1-
@ ProgramNode (location: (1,0)-(3,3))
1+
@ ProgramNode (location: (1,0)-(5,9))
22
├── flags: ∅
33
├── locals: []
44
└── statements:
5-
@ StatementsNode (location: (1,0)-(3,3))
5+
@ StatementsNode (location: (1,0)-(5,9))
66
├── flags: ∅
7-
└── body: (length: 1)
8-
└── @ CallNode (location: (1,0)-(3,3))
9-
├── flags: newline, ignore_visibility
10-
├── receiver: ∅
11-
├── call_operator_loc: ∅
12-
├── name: :x
13-
├── message_loc: (1,0)-(1,1) = "x"
14-
├── opening_loc: ∅
15-
├── arguments: ∅
16-
├── closing_loc: ∅
17-
└── block:
18-
@ BlockNode (location: (1,2)-(3,3))
7+
└── body: (length: 2)
8+
├── @ CallNode (location: (1,0)-(3,3))
9+
│ ├── flags: newline, ignore_visibility
10+
│ ├── receiver: ∅
11+
│ ├── call_operator_loc: ∅
12+
│ ├── name: :x
13+
│ ├── message_loc: (1,0)-(1,1) = "x"
14+
│ ├── opening_loc: ∅
15+
│ ├── arguments: ∅
16+
│ ├── closing_loc: ∅
17+
│ └── block:
18+
│ @ BlockNode (location: (1,2)-(3,3))
19+
│ ├── flags: ∅
20+
│ ├── locals: []
21+
│ ├── parameters:
22+
│ │ @ ItParametersNode (location: (1,2)-(3,3))
23+
│ │ └── flags: ∅
24+
│ ├── body:
25+
│ │ @ StatementsNode (location: (2,2)-(2,4))
26+
│ │ ├── flags: ∅
27+
│ │ └── body: (length: 1)
28+
│ │ └── @ ItLocalVariableReadNode (location: (2,2)-(2,4))
29+
│ │ └── flags: newline
30+
│ ├── opening_loc: (1,2)-(1,4) = "do"
31+
│ └── closing_loc: (3,0)-(3,3) = "end"
32+
└── @ LambdaNode (location: (5,0)-(5,9))
33+
├── flags: newline
34+
├── locals: []
35+
├── operator_loc: (5,0)-(5,2) = "->"
36+
├── opening_loc: (5,3)-(5,4) = "{"
37+
├── closing_loc: (5,8)-(5,9) = "}"
38+
├── parameters:
39+
│ @ ItParametersNode (location: (5,0)-(5,9))
40+
│ └── flags: ∅
41+
└── body:
42+
@ StatementsNode (location: (5,5)-(5,7))
1943
├── flags: ∅
20-
├── locals: []
21-
├── parameters:
22-
│ @ ItParametersNode (location: (1,2)-(3,3))
23-
│ └── flags: ∅
24-
├── body:
25-
│ @ StatementsNode (location: (2,2)-(2,4))
26-
│ ├── flags: ∅
27-
│ └── body: (length: 1)
28-
│ └── @ ItLocalVariableReadNode (location: (2,2)-(2,4))
29-
│ └── flags: newline
30-
├── opening_loc: (1,2)-(1,4) = "do"
31-
└── closing_loc: (3,0)-(3,3) = "end"
44+
└── body: (length: 1)
45+
└── @ ItLocalVariableReadNode (location: (5,5)-(5,7))
46+
└── flags: newline

test/prism/fixtures/it.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
x do
22
it
33
end
4+
5+
-> { it }

test/prism/ruby/parser_test.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,13 @@ def test_it_block_parameter_syntax
171171
actual_ast = Prism::Translation::Parser34.new.tokenize(buffer)[0]
172172

173173
it_block_parameter_sexp = parse_sexp {
174+
s(:begin,
174175
s(:itblock,
175176
s(:send, nil, :x), :it,
176-
s(:lvar, :it))
177+
s(:lvar, :it)),
178+
s(:itblock,
179+
s(:lambda), :it,
180+
s(:lvar, :it)))
177181
}
178182

179183
assert_equal(it_block_parameter_sexp, actual_ast.to_sexp)

test/prism/ruby/ruby_parser_test.rb

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,22 @@
1313
return
1414
end
1515

16-
# We want to also compare lines and files to make sure we're setting them
17-
# correctly.
18-
Sexp.prepend(
19-
Module.new do
20-
def ==(other)
21-
super && line == other.line && file == other.file # && line_max == other.line_max
22-
end
23-
end
24-
)
25-
2616
module Prism
2717
class RubyParserTest < TestCase
2818
todos = [
2919
"encoding_euc_jp.txt",
30-
"newline_terminated.txt",
3120
"regex_char_width.txt",
32-
"seattlerb/bug169.txt",
3321
"seattlerb/masgn_colon3.txt",
3422
"seattlerb/messy_op_asgn_lineno.txt",
3523
"seattlerb/op_asgn_primary_colon_const_command_call.txt",
3624
"seattlerb/regexp_esc_C_slash.txt",
3725
"seattlerb/str_lit_concat_bad_encodings.txt",
3826
"strings.txt",
3927
"unescaping.txt",
40-
"unparser/corpus/literal/kwbegin.txt",
41-
"unparser/corpus/literal/send.txt",
4228
"whitequark/masgn_const.txt",
4329
"whitequark/pattern_matching_constants.txt",
44-
"whitequark/pattern_matching_implicit_array_match.txt",
4530
"whitequark/pattern_matching_single_match.txt",
4631
"whitequark/ruby_bug_12402.txt",
47-
"whitequark/ruby_bug_14690.txt",
48-
"whitequark/space_args_block.txt"
4932
]
5033

5134
# https://github.com/seattlerb/ruby_parser/issues/344
@@ -105,10 +88,16 @@ def assert_ruby_parser(fixture, allowed_failure)
10588
source = fixture.read
10689
expected = ignore_warnings { ::RubyParser.new.parse(source, fixture.path) }
10790
actual = Prism::Translation::RubyParser.new.parse(source, fixture.path)
91+
on_failure = -> { message(expected, actual) }
10892

10993
if !allowed_failure
110-
assert_equal(expected, actual, -> { message(expected, actual) })
111-
elsif expected == actual
94+
assert_equal(expected, actual, on_failure)
95+
96+
unless actual.nil?
97+
assert_equal(expected.line, actual.line, on_failure)
98+
assert_equal(expected.file, actual.file, on_failure)
99+
end
100+
elsif expected == actual && expected.line && actual.line && expected.file == actual.file
112101
puts "#{name} now passes"
113102
end
114103
end

0 commit comments

Comments
 (0)