Skip to content

Commit

Permalink
Fix AST location of call name in operator assignment (#13456)
Browse files Browse the repository at this point in the history
  • Loading branch information
FnControlOption authored May 22, 2023
1 parent 3c43a93 commit 986e03b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
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

0 comments on commit 986e03b

Please sign in to comment.