Skip to content
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

Fix AST location of call name in operator assignment #13456

Merged
merged 2 commits into from
May 22, 2023
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 spec/compiler/parser/parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2514,6 +2514,12 @@ module Crystal
source_between(source, node.name_location, node.name_end_location).should eq("foo")
end

it "sets correct location of call name in operator assignment" do
source = "@foo.bar += 1"
node = Parser.parse(source).as(OpAssign).target.as(Call)
source_between(source, node.name_location, node.name_end_location).should eq("bar")
end

it "sets correct location of element in array literal" do
source = "%i(foo bar)"
elements = Parser.new(source).parse.as(ArrayLiteral).elements
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/crystal/syntax/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,6 @@ module Crystal
end

check AtomicWithMethodCheck
name_location = @token.location

if @token.value == Keyword::IS_A_QUESTION
atomic = parse_is_a(atomic).at(location)
Expand All @@ -722,6 +721,7 @@ module Crystal
else
@token.type.to_s
end
name_location = @token.location
end_location = token_end_location

@wants_regex = false
Expand Down Expand Up @@ -765,14 +765,14 @@ module Crystal
atomic.name_location = name_location
next
when .assignment_operator?
name_location = @token.location
op_name_location = @token.location
method = @token.type.to_s.byte_slice(0, @token.type.to_s.size - 1)
next_token_skip_space_or_newline
value = parse_op_assign
call = Call.new(atomic, name).at(location)
call.name_location = name_location
atomic = OpAssign.new(call, method, value).at(location)
atomic.name_location = name_location
atomic.name_location = op_name_location
next
else
call_args = preserve_stop_on_do { space_consumed ? parse_call_args_space_consumed : parse_call_args }
Expand Down