Skip to content

Commit c2e372a

Browse files
committed
RubyParser translation for stabby lambdas with it
1 parent 3e8066e commit c2e372a

4 files changed

Lines changed: 49 additions & 28 deletions

File tree

lib/prism/translation/ruby_parser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,7 @@ def visit_keyword_rest_parameter_node(node)
11511151
def visit_lambda_node(node)
11521152
parameters =
11531153
case node.parameters
1154-
when nil, NumberedParametersNode
1154+
when nil, ItParametersNode, NumberedParametersNode
11551155
s(node, :args)
11561156
else
11571157
visit(node.parameters)

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)

0 commit comments

Comments
 (0)