From fe316c246a020b92189983495996b044d7a257c1 Mon Sep 17 00:00:00 2001 From: Andrew Tribone Date: Tue, 19 Mar 2019 09:29:43 -0700 Subject: [PATCH] Fix `Style/BlockDelimiters` to properly check if the node is chaned when `braces_for_chaining` is set. --- CHANGELOG.md | 2 + lib/rubocop/cop/style/block_delimiters.rb | 8 +--- .../cop/style/block_delimiters_spec.rb | 39 +++++++++++++++++++ 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e715858899a..75d37fbe760 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ * [#6998](https://github.com/rubocop-hq/rubocop/pull/6998): Fix autocorrect of `Naming/RescuedExceptionsVariableName` to also rename all references to the variable. ([@Darhazer][]) * [#6992](https://github.com/rubocop-hq/rubocop/pull/6992): Fix unknown default configuration for `Layout/IndentFirstParameter` cop. ([@drenmi][]) * [#6972](https://github.com/rubocop-hq/rubocop/issues/6972): Fix a false positive for `Style/MixinUsage` when using inside block and `if` condition is after `include`. ([@koic][]) +* [#6847](https://github.com/rubocop-hq/rubocop/pull/6847): Fix `Style/BlockDelimiters` to properly check if the node is chaned when `braces_for_chaining` is set. ([@att14][]) ## 0.68.0 (2019-04-29) @@ -3989,3 +3990,4 @@ [@andreaseger]: https://github.com/andreaseger [@yakout]: https://github.com/yakout [@RicardoTrindade]: https://github.com/RicardoTrindade +[@att14]: https://github.com/att14 diff --git a/lib/rubocop/cop/style/block_delimiters.rb b/lib/rubocop/cop/style/block_delimiters.rb index c2fa577c641..97ec834dad9 100644 --- a/lib/rubocop/cop/style/block_delimiters.rb +++ b/lib/rubocop/cop/style/block_delimiters.rb @@ -165,7 +165,7 @@ def semantic_message(node) def braces_for_chaining_message(node) if node.multiline? - if return_value_chaining?(node) + if node.chained? 'Prefer `{...}` over `do...end` for multi-line chained blocks.' else 'Prefer `do...end` for multi-line blocks without chaining.' @@ -267,7 +267,7 @@ def braces_for_chaining_style?(node) block_begin = node.loc.begin.source block_begin == if node.multiline? - (return_value_chaining?(node) ? '{' : 'do') + (node.chained? ? '{' : 'do') else '{' end @@ -277,10 +277,6 @@ def braces_style?(node) node.loc.begin.source == '{' end - def return_value_chaining?(node) - node.parent && node.parent.send_type? && node.parent.dot? - end - def correction_would_break_code?(node) return unless node.keywords? diff --git a/spec/rubocop/cop/style/block_delimiters_spec.rb b/spec/rubocop/cop/style/block_delimiters_spec.rb index 76c7b562cd0..29ca38cf307 100644 --- a/spec/rubocop/cop/style/block_delimiters_spec.rb +++ b/spec/rubocop/cop/style/block_delimiters_spec.rb @@ -490,6 +490,45 @@ RUBY end + it 'allows when :[] is chained' do + expect_no_offenses(<<-RUBY.strip_indent) + foo = [{foo: :bar}].find { |h| + h.key?(:foo) + }[:foo] + RUBY + end + + it 'allows do/end inside Hash[]' do + expect_no_offenses(<<-RUBY.strip_indent) + Hash[ + {foo: :bar}.map do |k, v| + [k, v] + end + ] + RUBY + end + + it 'allows chaining to } inside of Hash[]' do + expect_no_offenses(<<-RUBY.strip_indent) + Hash[ + {foo: :bar}.map { |k, v| + [k, v] + }.uniq + ] + RUBY + end + + it 'disallows {} with no chain inside of Hash[]' do + expect_offense(<<-RUBY.strip_indent) + Hash[ + {foo: :bar}.map { |k, v| + ^ Prefer `do...end` for multi-line blocks without chaining. + [k, v] + } + ] + RUBY + end + context 'when there are braces around a multi-line block' do it 'registers an offense in the simple case' do expect_offense(<<-RUBY.strip_indent)