Skip to content

Commit

Permalink
[Fix rubocop#4394] Prevent some cops from breaking on safe navigation…
Browse files Browse the repository at this point in the history
… operator

Some cops, e.g. `Style/For` and `Style/Lambda` would break when
encountering a safe navigation operator.

This change fixes that by granting the powers of the `SendNode`
extension to `csend` nodes.
  • Loading branch information
Drenmi committed May 22, 2017
1 parent d7dab3d commit 477a5e8
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
* [#4371](https://github.com/bbatsov/rubocop/issues/4371): Prevent `Style/MethodName` from complaining about unary operator definitions. ([@drenmi][])
* [#4366](https://github.com/bbatsov/rubocop/issues/4366): Prevent `Performance/RedundantMerge` from blowing up on double splat arguments. ([@drenmi][])
* [#4352](https://github.com/bbatsov/rubocop/issues/4352): Fix the auto-correct of `Style/AndOr` when Enumerable accessors (`[]`) are used. ([@rrosenblum][])
* [#4394](https://github.com/bbatsov/rubocop/issues/4394): [Fix #4394] Prevent some cops from breaking on safe navigation operator. ([@drenmi][])

## 0.48.1 (2017-04-03)

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/ast/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Builder < Parser::Builders::Default
OrNode => [:or],
PairNode => [:pair],
ResbodyNode => [:resbody],
SendNode => [:send],
SendNode => %i[csend send],
SuperNode => %i[super zsuper],
UntilNode => %i[until until_post],
WhenNode => [:when],
Expand Down
13 changes: 11 additions & 2 deletions spec/rubocop/ast/send_node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@
let(:send_node) { parse_source(source).ast }

describe '.new' do
let(:source) { 'foo.bar(:baz)' }
context 'with a regular method send' do
let(:source) { 'foo.bar(:baz)' }

it { expect(send_node).to be_a(described_class) }
end

it { expect(send_node).to be_a(described_class) }
context 'with a safe navigation method send' do
let(:ruby_version) { 2.3 }
let(:source) { 'foo&.bar(:baz)' }

it { expect(send_node).to be_a(described_class) }
end
end

describe '#receiver' do
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/style/for_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,17 @@ def func
end
END
end

context 'when using safe navigation operator' do
let(:ruby_version) { 2.3 }

it 'does not break' do
expect_no_offenses(<<-END.strip_indent)
def func
[1, 2, 3]&.each { |n| puts n }
end
END
end
end
end
end
12 changes: 12 additions & 0 deletions spec/rubocop/cop/style/lambda_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -424,4 +424,16 @@
it_behaves_like 'does not auto-correct'
end
end

context 'when using safe navigation operator' do
let(:ruby_version) { 2.3 }

it 'does not break' do
expect_no_offenses(<<-END.strip_indent)
foo&.bar do |_|
baz
end
END
end
end
end

0 comments on commit 477a5e8

Please sign in to comment.