Skip to content

Commit 2367cb4

Browse files
vinistockkddnewton
andcommitted
Fix lambda local locations
Co-authored-by: Kevin Newton <[email protected]>
1 parent 654ebcb commit 2367cb4

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

lib/syntax_tree/parser.rb

+6
Original file line numberDiff line numberDiff line change
@@ -2391,8 +2391,14 @@ def lambda_locals(source)
23912391
}
23922392
}
23932393

2394+
parent_line = lineno - 1
2395+
parent_column =
2396+
consume_token(Semicolon).location.start_column - tokens[index][0][1]
2397+
23942398
tokens[(index + 1)..].each_with_object([]) do |token, locals|
23952399
(lineno, column), type, value, = token
2400+
column += parent_column if lineno == 1
2401+
lineno += parent_line
23962402

23972403
# Make the state transition for the parser. If there isn't a transition
23982404
# from the current state to a new state for this type, then we're in a

test/parser_test.rb

+48
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,53 @@ def test_does_not_choke_on_invalid_characters_in_source_string
7474
\xC5
7575
RUBY
7676
end
77+
78+
def test_lambda_vars_with_parameters_location
79+
tree = SyntaxTree.parse(<<~RUBY)
80+
# comment
81+
# comment
82+
->(_i; a) { a }
83+
RUBY
84+
85+
local_location =
86+
tree.statements.body.last.params.contents.locals.first.location
87+
88+
assert_equal(3, local_location.start_line)
89+
assert_equal(3, local_location.end_line)
90+
assert_equal(7, local_location.start_column)
91+
assert_equal(8, local_location.end_column)
92+
end
93+
94+
def test_lambda_vars_location
95+
tree = SyntaxTree.parse(<<~RUBY)
96+
# comment
97+
# comment
98+
->(; a) { a }
99+
RUBY
100+
101+
local_location =
102+
tree.statements.body.last.params.contents.locals.first.location
103+
104+
assert_equal(3, local_location.start_line)
105+
assert_equal(3, local_location.end_line)
106+
assert_equal(5, local_location.start_column)
107+
assert_equal(6, local_location.end_column)
108+
end
109+
110+
def test_multiple_lambda_vars_location
111+
tree = SyntaxTree.parse(<<~RUBY)
112+
# comment
113+
# comment
114+
->(; a, b, c) { a }
115+
RUBY
116+
117+
local_location =
118+
tree.statements.body.last.params.contents.locals.last.location
119+
120+
assert_equal(3, local_location.start_line)
121+
assert_equal(3, local_location.end_line)
122+
assert_equal(11, local_location.start_column)
123+
assert_equal(12, local_location.end_column)
124+
end
77125
end
78126
end

0 commit comments

Comments
 (0)