From 477a5e89cd1636e1e46e68f54685b701e30268b2 Mon Sep 17 00:00:00 2001 From: Ted Johansson Date: Mon, 22 May 2017 11:10:51 +0800 Subject: [PATCH] [Fix #4394] Prevent some cops from breaking on safe navigation 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. --- CHANGELOG.md | 1 + lib/rubocop/ast/builder.rb | 2 +- spec/rubocop/ast/send_node_spec.rb | 13 +++++++++++-- spec/rubocop/cop/style/for_spec.rb | 12 ++++++++++++ spec/rubocop/cop/style/lambda_spec.rb | 12 ++++++++++++ 5 files changed, 37 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c32a5c3f362..0dc92d12ab4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/rubocop/ast/builder.rb b/lib/rubocop/ast/builder.rb index 52bf7b7d3fb8..8859d5e86e6d 100644 --- a/lib/rubocop/ast/builder.rb +++ b/lib/rubocop/ast/builder.rb @@ -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], diff --git a/spec/rubocop/ast/send_node_spec.rb b/spec/rubocop/ast/send_node_spec.rb index c19dfa66c77d..477b15cbdce6 100644 --- a/spec/rubocop/ast/send_node_spec.rb +++ b/spec/rubocop/ast/send_node_spec.rb @@ -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 diff --git a/spec/rubocop/cop/style/for_spec.rb b/spec/rubocop/cop/style/for_spec.rb index e70eafd3de14..9cc320ff470d 100644 --- a/spec/rubocop/cop/style/for_spec.rb +++ b/spec/rubocop/cop/style/for_spec.rb @@ -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 diff --git a/spec/rubocop/cop/style/lambda_spec.rb b/spec/rubocop/cop/style/lambda_spec.rb index e853a96ba40f..c9314bdc3a38 100644 --- a/spec/rubocop/cop/style/lambda_spec.rb +++ b/spec/rubocop/cop/style/lambda_spec.rb @@ -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