Skip to content

Commit

Permalink
[Fix #331] Fix crash in OpAsgnNode#name when the lhs is a send or…
Browse files Browse the repository at this point in the history
… `csend` node.
  • Loading branch information
dvandersluis authored and marcandre committed Nov 7, 2024
1 parent 62d4112 commit 048eb8d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/fix_fix_331_fix_crash_in_opasgnnodename_when.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#331](https://github.com/rubocop/rubocop-ast/issue/331): [Fix #331] Fix crash in `OpAsgnNode#name` when the lhs is a `send` or `csend` node. ([@dvandersluis][])
2 changes: 1 addition & 1 deletion lib/rubocop/ast/node/op_asgn_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def assignment_node
#
# @return [Symbol] the name of the variable being assigned
def name
assignment_node.name
assignment_node.call_type? ? assignment_node.method_name : assignment_node.name
end

# The operator being used for assignment as a symbol.
Expand Down
28 changes: 27 additions & 1 deletion spec/rubocop/ast/op_asgn_node_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# frozen_string_literal: true

RSpec.describe RuboCop::AST::OpAsgnNode do
let(:op_asgn_node) { parse_source(source).ast }
let(:parsed_source) { parse_source(source) }
let(:op_asgn_node) { parsed_source.ast }

describe '.new' do
context 'with an `op_asgn_node` node' do
Expand All @@ -15,8 +16,21 @@
subject { op_asgn_node.assignment_node }

let(:source) { 'var += value' }
let(:node) { parsed_source.node }

it { is_expected.to be_a(RuboCop::AST::AsgnNode) }

context 'when the LHS is a `send` node' do
let(:source) { '>> foo.var << += value' }

it { is_expected.to eq(node) }
end

context 'when the LHS is a `csend` node' do
let(:source) { '>> foo&.var << += value' }

it { is_expected.to eq(node) }
end
end

describe '#name' do
Expand All @@ -25,6 +39,18 @@
let(:source) { 'var += value' }

it { is_expected.to eq(:var) }

context 'when the LHS is a `send` node' do
let(:source) { 'foo.var += value' }

it { is_expected.to eq(:var) }
end

context 'when the LHS is a `csend` node' do
let(:source) { 'foo&.var += value' }

it { is_expected.to eq(:var) }
end
end

describe '#operator' do
Expand Down

0 comments on commit 048eb8d

Please sign in to comment.