Skip to content

Fix lambda local locations #327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/syntax_tree/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2391,8 +2391,14 @@ def lambda_locals(source)
}
}

parent_line = lineno - 1
parent_column =
consume_token(Semicolon).location.start_column - tokens[index][0][1]

tokens[(index + 1)..].each_with_object([]) do |token, locals|
(lineno, column), type, value, = token
column += parent_column if lineno == 1
lineno += parent_line

# Make the state transition for the parser. If there isn't a transition
# from the current state to a new state for this type, then we're in a
Expand Down
48 changes: 48 additions & 0 deletions test/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,53 @@ def test_does_not_choke_on_invalid_characters_in_source_string
\xC5
RUBY
end

def test_lambda_vars_with_parameters_location
tree = SyntaxTree.parse(<<~RUBY)
# comment
# comment
->(_i; a) { a }
RUBY

local_location =
tree.statements.body.last.params.contents.locals.first.location

assert_equal(3, local_location.start_line)
assert_equal(3, local_location.end_line)
assert_equal(7, local_location.start_column)
assert_equal(8, local_location.end_column)
end

def test_lambda_vars_location
tree = SyntaxTree.parse(<<~RUBY)
# comment
# comment
->(; a) { a }
RUBY

local_location =
tree.statements.body.last.params.contents.locals.first.location

assert_equal(3, local_location.start_line)
assert_equal(3, local_location.end_line)
assert_equal(5, local_location.start_column)
assert_equal(6, local_location.end_column)
end

def test_multiple_lambda_vars_location
tree = SyntaxTree.parse(<<~RUBY)
# comment
# comment
->(; a, b, c) { a }
RUBY

local_location =
tree.statements.body.last.params.contents.locals.last.location

assert_equal(3, local_location.start_line)
assert_equal(3, local_location.end_line)
assert_equal(11, local_location.start_column)
assert_equal(12, local_location.end_column)
end
end
end