From fd6dcac3a80c1e0334a77e1bffeb4c987c51ab03 Mon Sep 17 00:00:00 2001 From: Masataka Pocke Kuwabara Date: Sat, 11 Jan 2020 15:00:41 +0900 Subject: [PATCH] [Fix #7641] Remove Style/BracesAroundHashParameters cop --- CHANGELOG.md | 1 + config/default.yml | 18 - lib/rubocop.rb | 1 - lib/rubocop/config_obsoletion.rb | 3 +- .../cop/layout/multiline_hash_brace_layout.rb | 4 - .../space_inside_hash_literal_braces.rb | 11 +- lib/rubocop/cop/mixin/trailing_comma.rb | 11 +- .../style/braces_around_hash_parameters.rb | 209 -------- .../cop/style/trailing_comma_in_arguments.rb | 22 - manual/cops.md | 1 - manual/cops_style.md | 58 -- spec/rubocop/cli/cli_autocorrect_spec.rb | 289 ++-------- spec/rubocop/cli_spec.rb | 10 +- .../braces_around_hash_parameters_spec.rb | 494 ------------------ 14 files changed, 49 insertions(+), 1083 deletions(-) delete mode 100644 lib/rubocop/cop/style/braces_around_hash_parameters.rb delete mode 100644 spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c91e8f8882..9cb234e5390 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ ### Changes * [#7636](https://github.com/rubocop-hq/rubocop/issues/7636): Remove `console` from `Lint/Debugger` to prevent false positives. ([@gsamokovarov][]) +* [#7641](https://github.com/rubocop-hq/rubocop/issues/7641): **(Breaking)** Remove `Style/BracesAroundHashParameters` cop. ([@pocke][]) ## 0.79.0 (2020-01-06) diff --git a/config/default.yml b/config/default.yml index a6b4975a7d3..584a5aba313 100644 --- a/config/default.yml +++ b/config/default.yml @@ -2356,24 +2356,6 @@ Style/BlockDelimiters: # collection.each do |element| puts element end AllowBracesOnProceduralOneLiners: false -Style/BracesAroundHashParameters: - Description: 'Enforce braces style around hash parameters.' - Enabled: true - VersionAdded: '0.14.1' - VersionChanged: '0.28' - EnforcedStyle: no_braces - SupportedStyles: - # The `braces` style enforces braces around all method parameters that are - # hashes. - - braces - # The `no_braces` style checks that the last parameter doesn't have braces - # around it. - - no_braces - # The `context_dependent` style checks that the last parameter doesn't have - # braces around it, but requires braces if the second to last parameter is - # also a hash literal. - - context_dependent - Style/CaseEquality: Description: 'Avoid explicit use of the case equality operator(===).' StyleGuide: '#no-case-equality' diff --git a/lib/rubocop.rb b/lib/rubocop.rb index ad8340ccacf..00b5eb195e1 100644 --- a/lib/rubocop.rb +++ b/lib/rubocop.rb @@ -402,7 +402,6 @@ require_relative 'rubocop/cop/style/begin_block' require_relative 'rubocop/cop/style/block_comments' require_relative 'rubocop/cop/style/block_delimiters' -require_relative 'rubocop/cop/style/braces_around_hash_parameters' require_relative 'rubocop/cop/style/case_equality' require_relative 'rubocop/cop/style/character_literal' require_relative 'rubocop/cop/style/class_and_module_children' diff --git a/lib/rubocop/config_obsoletion.rb b/lib/rubocop/config_obsoletion.rb index 1f2d704bd32..cfe2c1e0c96 100644 --- a/lib/rubocop/config_obsoletion.rb +++ b/lib/rubocop/config_obsoletion.rb @@ -69,7 +69,8 @@ class ConfigObsoletion 'Style/TrailingCommaInHashLiteral', 'Style/TrailingCommaInLiteral' => 'Style/TrailingCommaInArrayLiteral ' \ 'and/or ' \ - 'Style/TrailingCommaInHashLiteral' + 'Style/TrailingCommaInHashLiteral', + 'Style/BracesAroundHashParameters' => nil }.map do |old_name, other_cops| if other_cops more = ". Please use #{other_cops} instead".gsub(%r{[A-Z]\w+/\w+}, diff --git a/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb b/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb index dcd1955ff12..39f301eaeab 100644 --- a/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +++ b/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb @@ -105,10 +105,6 @@ class MultilineHashBraceLayout < Cop ALWAYS_SAME_LINE_MESSAGE = 'Closing hash brace must be on the same ' \ 'line as the last hash element.' - def self.autocorrect_incompatible_with - [Style::BracesAroundHashParameters] - end - def on_hash(node) check_brace_layout(node) end diff --git a/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb b/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb index fe7aa64fd64..a2d03bb10b1 100644 --- a/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +++ b/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb @@ -83,17 +83,10 @@ def on_hash(node) def autocorrect(range) lambda do |corrector| - # It is possible that BracesAroundHashParameters will remove the - # braces while this cop inserts spaces. This can lead to unwanted - # changes to the inspected code. If we replace the brace with a - # brace plus space (rather than just inserting a space), then any - # removal of the same brace will give us a clobbering error. This - # in turn will make RuboCop fall back on cop-by-cop - # auto-correction. Problem solved. case range.source when /\s/ then corrector.remove(range) - when '{' then corrector.replace(range, '{ ') - else corrector.replace(range, ' }') + when '{' then corrector.insert_after(range, ' ') + else corrector.insert_before(range, ' ') end end end diff --git a/lib/rubocop/cop/mixin/trailing_comma.rb b/lib/rubocop/cop/mixin/trailing_comma.rb index e237b59eabb..000639c7765 100644 --- a/lib/rubocop/cop/mixin/trailing_comma.rb +++ b/lib/rubocop/cop/mixin/trailing_comma.rb @@ -23,7 +23,7 @@ def check(node, items, kind, begin_pos, end_pos) if comma_offset && !inside_comment?(after_last_item, comma_offset) check_comma(node, kind, after_last_item.begin_pos + comma_offset) elsif should_have_comma?(style, node) - put_comma(node, items, kind) + put_comma(items, kind) end end @@ -145,9 +145,7 @@ def avoid_comma(kind, comma_begin_pos, extra_info) add_offense(range, location: range, message: msg) end - def put_comma(node, items, kind) - return if avoid_autocorrect?(elements(node)) - + def put_comma(items, kind) last_item = items.last return if last_item.block_pass_type? @@ -169,11 +167,6 @@ def autocorrect_range(item) range_between(expr.begin_pos + ix, expr.end_pos) end - # By default, there's no reason to avoid auto-correct. - def avoid_autocorrect?(_nodes) - false - end - def any_heredoc?(items) items.any? { |item| heredoc?(item) } end diff --git a/lib/rubocop/cop/style/braces_around_hash_parameters.rb b/lib/rubocop/cop/style/braces_around_hash_parameters.rb deleted file mode 100644 index 7791466090b..00000000000 --- a/lib/rubocop/cop/style/braces_around_hash_parameters.rb +++ /dev/null @@ -1,209 +0,0 @@ -# frozen_string_literal: true - -module RuboCop - module Cop - module Style - # This cop checks for braces around the last parameter in a method call - # if the last parameter is a hash. - # It supports `braces`, `no_braces` and `context_dependent` styles. - # - # @example EnforcedStyle: braces - # # The `braces` style enforces braces around all method - # # parameters that are hashes. - # - # # bad - # some_method(x, y, a: 1, b: 2) - # - # # good - # some_method(x, y, {a: 1, b: 2}) - # - # @example EnforcedStyle: no_braces (default) - # # The `no_braces` style checks that the last parameter doesn't - # # have braces around it. - # - # # bad - # some_method(x, y, {a: 1, b: 2}) - # - # # good - # some_method(x, y, a: 1, b: 2) - # - # @example EnforcedStyle: context_dependent - # # The `context_dependent` style checks that the last parameter - # # doesn't have braces around it, but requires braces if the - # # second to last parameter is also a hash literal. - # - # # bad - # some_method(x, y, {a: 1, b: 2}) - # some_method(x, y, {a: 1, b: 2}, a: 1, b: 2) - # - # # good - # some_method(x, y, a: 1, b: 2) - # some_method(x, y, {a: 1, b: 2}, {a: 1, b: 2}) - class BracesAroundHashParameters < Cop - include ConfigurableEnforcedStyle - include RangeHelp - - MSG = '%s curly braces around a hash parameter.' - - def on_send(node) - return if node.assignment_method? || node.operator_method? - - return unless node.arguments? && node.last_argument.hash_type? && - !node.last_argument.empty? - - check(node.last_argument, node.arguments) - end - alias on_csend on_send - - def autocorrect(send_node) - hash_node = send_node.last_argument - - lambda do |corrector| - if hash_node.braces? - remove_braces_with_whitespace(corrector, - hash_node, - extra_space(hash_node)) - else - add_braces(corrector, hash_node) - end - end - end - - private - - def check(arg, args) - case style - when :braces - check_braces(arg) - when :no_braces - check_no_braces(arg) - when :context_dependent - check_context_dependent(arg, args) - end - end - - def check_braces(arg) - add_arg_offense(arg, :missing) unless arg.braces? - end - - def check_no_braces(arg) - return unless arg.braces? && !braces_needed_for_semantics?(arg) - - add_arg_offense(arg, :redundant) - end - - def check_context_dependent(arg, args) - braces_around_second_from_end = args.size > 1 && args[-2].hash_type? - - if arg.braces? - unless braces_around_second_from_end || - braces_needed_for_semantics?(arg) - add_arg_offense(arg, :redundant) - end - elsif braces_around_second_from_end - add_arg_offense(arg, :missing) - end - end - - # Returns true if there's block inside the braces of the given hash arg - # and that block uses do..end. The reason for wanting to check this is - # that the do..end could bind to a different method invocation if the - # hash braces were removed. - def braces_needed_for_semantics?(arg) - arg.each_pair do |_key, value| - return true if value.block_type? && !value.braces? - end - false - end - - def add_arg_offense(arg, type) - add_offense(arg.parent, location: arg.source_range, - message: format(MSG, - type: type.to_s.capitalize)) - end - - def extra_space(hash_node) - { - newlines: extra_left_space?(hash_node) && - extra_right_space?(hash_node), - left: extra_left_space?(hash_node), - right: extra_right_space?(hash_node) - } - end - - def extra_left_space?(hash_node) - @extra_left_space ||= begin - top_line = hash_node.source_range.source_line - top_line.delete(' ') == '{' - end - end - - def extra_right_space?(hash_node) - @extra_right_space ||= begin - bottom_line_number = hash_node.source_range.last_line - bottom_line = processed_source.lines[bottom_line_number - 1] - bottom_line.delete(' ') == '}' - end - end - - def remove_braces_with_whitespace(corrector, node, space) - loc = node.loc - - if node.multiline? - remove_braces_with_range(corrector, - left_whole_line_range(loc.begin), - right_whole_line_range(loc.end)) - else - remove_braces_with_range(corrector, - left_brace_and_space(loc.begin, space), - right_brace_and_space(loc.end, space)) - end - end - - def remove_braces_with_range(corrector, left_range, right_range) - corrector.remove(left_range) - corrector.remove(right_range) - end - - def left_whole_line_range(loc_begin) - if range_by_whole_lines(loc_begin).source.strip == '{' - range_by_whole_lines(loc_begin, include_final_newline: true) - else - loc_begin - end - end - - def right_whole_line_range(loc_end) - if range_by_whole_lines(loc_end).source.strip =~ /\A}\s*,?\z/ - range_by_whole_lines(loc_end, include_final_newline: true) - else - loc_end - end - end - - def left_brace_and_space(loc_begin, space) - range_with_surrounding_space(range: loc_begin, - side: :right, - newlines: space[:newlines], - whitespace: space[:left]) - end - - def right_brace_and_space(loc_end, space) - brace_and_space = - range_with_surrounding_space( - range: loc_end, - side: :left, - newlines: space[:newlines], - whitespace: space[:right] - ) - range_with_surrounding_comma(brace_and_space, :left) - end - - def add_braces(corrector, node) - corrector.insert_before(node.source_range, '{') - corrector.insert_after(node.source_range, '}') - end - end - end - end -end diff --git a/lib/rubocop/cop/style/trailing_comma_in_arguments.rb b/lib/rubocop/cop/style/trailing_comma_in_arguments.rb index cea023e9d88..d47fd53e0cc 100644 --- a/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +++ b/lib/rubocop/cop/style/trailing_comma_in_arguments.rb @@ -68,28 +68,6 @@ def autocorrect(range) def self.autocorrect_incompatible_with [Layout::HeredocArgumentClosingParenthesis] end - - private - - def avoid_autocorrect?(args) - args.last.hash_type? && args.last.braces? && - braces_will_be_removed?(args) - end - - # Returns true if running with --auto-correct would remove the braces - # of the last argument. - def braces_will_be_removed?(args) - brace_config = config.for_cop('Style/BracesAroundHashParameters') - return false unless brace_config.fetch('Enabled') - return false if brace_config['AutoCorrect'] == false - - brace_style = brace_config['EnforcedStyle'] - return true if brace_style == 'no_braces' - - return false unless brace_style == 'context_dependent' - - args.one? || !args[-2].hash_type? - end end end end diff --git a/manual/cops.md b/manual/cops.md index 514e28e521a..28b01cec738 100644 --- a/manual/cops.md +++ b/manual/cops.md @@ -315,7 +315,6 @@ In the following section you find all available cops: * [Style/BeginBlock](cops_style.md#stylebeginblock) * [Style/BlockComments](cops_style.md#styleblockcomments) * [Style/BlockDelimiters](cops_style.md#styleblockdelimiters) -* [Style/BracesAroundHashParameters](cops_style.md#stylebracesaroundhashparameters) * [Style/CaseEquality](cops_style.md#stylecaseequality) * [Style/CharacterLiteral](cops_style.md#stylecharacterliteral) * [Style/ClassAndModuleChildren](cops_style.md#styleclassandmodulechildren) diff --git a/manual/cops_style.md b/manual/cops_style.md index 4a6991f9ed8..9821432bcf9 100644 --- a/manual/cops_style.md +++ b/manual/cops_style.md @@ -488,64 +488,6 @@ AllowBracesOnProceduralOneLiners | `false` | Boolean * [https://rubystyle.guide#single-line-blocks](https://rubystyle.guide#single-line-blocks) -## Style/BracesAroundHashParameters - -Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged ---- | --- | --- | --- | --- -Enabled | Yes | Yes | 0.14.1 | 0.28 - -This cop checks for braces around the last parameter in a method call -if the last parameter is a hash. -It supports `braces`, `no_braces` and `context_dependent` styles. - -### Examples - -#### EnforcedStyle: braces - -```ruby -# The `braces` style enforces braces around all method -# parameters that are hashes. - -# bad -some_method(x, y, a: 1, b: 2) - -# good -some_method(x, y, {a: 1, b: 2}) -``` -#### EnforcedStyle: no_braces (default) - -```ruby -# The `no_braces` style checks that the last parameter doesn't -# have braces around it. - -# bad -some_method(x, y, {a: 1, b: 2}) - -# good -some_method(x, y, a: 1, b: 2) -``` -#### EnforcedStyle: context_dependent - -```ruby -# The `context_dependent` style checks that the last parameter -# doesn't have braces around it, but requires braces if the -# second to last parameter is also a hash literal. - -# bad -some_method(x, y, {a: 1, b: 2}) -some_method(x, y, {a: 1, b: 2}, a: 1, b: 2) - -# good -some_method(x, y, a: 1, b: 2) -some_method(x, y, {a: 1, b: 2}, {a: 1, b: 2}) -``` - -### Configurable attributes - -Name | Default value | Configurable values ---- | --- | --- -EnforcedStyle | `no_braces` | `braces`, `no_braces`, `context_dependent` - ## Style/CaseEquality Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged diff --git a/spec/rubocop/cli/cli_autocorrect_spec.rb b/spec/rubocop/cli/cli_autocorrect_spec.rb index cb0a86c9163..059402a37a1 100644 --- a/spec/rubocop/cli/cli_autocorrect_spec.rb +++ b/spec/rubocop/cli/cli_autocorrect_spec.rb @@ -181,9 +181,7 @@ def batch }, 'Style/TrailingCommaInHashLiteral' => { 'EnforcedStyleForMultiline' => comma_style - }, - 'Style/BracesAroundHashParameters' => - braces_around_hash_parameters_config + } } end @@ -208,196 +206,58 @@ def batch let(:comma_style) do 'comma' end + let(:expected_corrected_source) do + <<~RUBY + # frozen_string_literal: true - context 'and Style/BracesAroundHashParameters is disabled' do - let(:braces_around_hash_parameters_config) do - { - 'Enabled' => false, - 'AutoCorrect' => false, - 'EnforcedStyle' => 'braces' - } - end - - let(:expected_corrected_source) do - <<~RUBY - # frozen_string_literal: true - - func({ - @abc => 0, - @xyz => 1, - }) - func( - { - abc: 0, - }, - ) - func( - {}, - { - xyz: 1, - }, - ) - RUBY - end - - include_examples 'corrects offenses without producing a double comma' - end - - context 'and BracesAroundHashParameters style is `no_braces`' do - let(:braces_around_hash_parameters_config) do - { - 'EnforcedStyle' => 'no_braces' - } - end - - let(:expected_corrected_source) do - <<~RUBY - # frozen_string_literal: true - - func( - @abc => 0, - @xyz => 1, - ) - func( + func({ + @abc => 0, + @xyz => 1, + }) + func( + { abc: 0, - ) - func( - {}, + }, + ) + func( + {}, + { xyz: 1, - ) - RUBY - end - - include_examples 'corrects offenses without producing a double comma' + }, + ) + RUBY end - context 'and BracesAroundHashParameters style is `context_dependent`' do - let(:braces_around_hash_parameters_config) do - { - 'EnforcedStyle' => 'context_dependent' - } - end - - let(:expected_corrected_source) do - <<~RUBY - # frozen_string_literal: true - - func( - @abc => 0, - @xyz => 1, - ) - func( - abc: 0, - ) - func( - {}, - { - xyz: 1, - }, - ) - RUBY - end - - include_examples 'corrects offenses without producing a double comma' - end + include_examples 'corrects offenses without producing a double comma' end context 'when the style is `consistent_comma`' do let(:comma_style) do 'consistent_comma' end + let(:expected_corrected_source) do + <<~RUBY + # frozen_string_literal: true - context 'and Style/BracesAroundHashParameters is disabled' do - let(:braces_around_hash_parameters_config) do - { - 'Enabled' => false, - 'AutoCorrect' => false, - 'EnforcedStyle' => 'braces' - } - end - - let(:expected_corrected_source) do - <<~RUBY - # frozen_string_literal: true - - func({ - @abc => 0, - @xyz => 1, - }) - func( - { - abc: 0, - }, - ) - func( - {}, - { - xyz: 1, - }, - ) - RUBY - end - - include_examples 'corrects offenses without producing a double comma' - end - - context 'and BracesAroundHashParameters style is `no_braces`' do - let(:braces_around_hash_parameters_config) do - { - 'EnforcedStyle' => 'no_braces' - } - end - - let(:expected_corrected_source) do - <<~RUBY - # frozen_string_literal: true - - func( - @abc => 0, - @xyz => 1, - ) - func( + func({ + @abc => 0, + @xyz => 1, + }) + func( + { abc: 0, - ) - func( - {}, + }, + ) + func( + {}, + { xyz: 1, - ) - RUBY - end - - include_examples 'corrects offenses without producing a double comma' + }, + ) + RUBY end - context 'and BracesAroundHashParameters style is `context_dependent`' do - let(:braces_around_hash_parameters_config) do - { - 'EnforcedStyle' => 'context_dependent' - } - end - - let(:expected_corrected_source) do - <<~RUBY - # frozen_string_literal: true - - func( - @abc => 0, - @xyz => 1, - ) - func( - abc: 0, - ) - func( - {}, - { - xyz: 1, - }, - ) - RUBY - end - - include_examples 'corrects offenses without producing a double comma' - end + include_examples 'corrects offenses without producing a double comma' end end @@ -1010,39 +870,6 @@ def do_something expect(IO.read('example.rb')).to eq(corrected) end - it 'corrects complicated cases conservatively' do - # Two cops make corrections here; Style/BracesAroundHashParameters, and - # Layout/HashAlignment. Because they make minimal corrections relating only - # to their specific areas, and stay away from cleaning up extra - # whitespace in the process, the combined changes don't interfere with - # each other and the result is semantically the same as the starting - # point. - source = <<~RUBY - expect(subject[:address]).to eq({ - street1: '1 Market', - street2: '#200', - city: 'Some Town', - state: 'CA', - postal_code: '99999-1111' - }) - RUBY - create_file('example.rb', source) - expect(cli.run(['-D', '--auto-correct'])).to eq(0) - corrected = - <<~RUBY - # frozen_string_literal: true - - expect(subject[:address]).to eq( - street1: '1 Market', - street2: '#200', - city: 'Some Town', - state: 'CA', - postal_code: '99999-1111' - ) - RUBY - expect(IO.read('example.rb')).to eq(corrected) - end - it 'honors Exclude settings in individual cops' do source = 'puts %x(ls)' create_file('example.rb', source) @@ -1169,28 +996,6 @@ def baz RUBY end - it 'can handle spaces when removing braces' do - create_file('example.rb', - ["assert_post_status_code 400, 's', {:type => 'bad'}"]) - exit_status = cli.run( - %w[--auto-correct --format emacs - --only SpaceInsideHashLiteralBraces,BracesAroundHashParameters] - ) - expect(exit_status).to eq(0) - expect(IO.read('example.rb')) - .to eq(<<~RUBY) - assert_post_status_code 400, 's', :type => 'bad' - RUBY - e = abs('example.rb') - # TODO: Don't report that a problem is corrected when it - # actually went away due to another correction. - expect($stdout.string).to eq(<<~RESULT) - #{e}:1:35: C: [Corrected] Layout/SpaceInsideHashLiteralBraces: Space inside { missing. - #{e}:1:35: C: [Corrected] Style/BracesAroundHashParameters: Redundant curly braces around a hash parameter. - #{e}:1:50: C: [Corrected] Layout/SpaceInsideHashLiteralBraces: Space inside } missing. - RESULT - end - # A case where two cops, EmptyLinesAroundBody and EmptyLines, try to # remove the same line in autocorrect. it 'can correct two empty lines at end of class body' do @@ -1679,26 +1484,6 @@ def self.some_method(foo, bar: 1) RUBY end - it 'corrects BracesAroundHashParameters offenses leaving the ' \ - 'MultilineHashBraceLayout offense unchanged' do - create_file('example.rb', <<~RUBY) - def method_a - do_something({ a: 1, - }) - end - RUBY - - expect($stderr.string).to eq('') - expect(cli.run(%w[--auto-correct])).to eq(0) - expect(IO.read('example.rb')).to eq(<<~RUBY) - # frozen_string_literal: true - - def method_a - do_something(a: 1) - end - RUBY - end - it 'corrects HeredocArgumentClosingParenthesis offenses and ' \ 'ignores TrailingCommaInArguments offense' do create_file('example.rb', <<~RUBY) diff --git a/spec/rubocop/cli_spec.rb b/spec/rubocop/cli_spec.rb index 308d6ba7393..807c412b7e3 100644 --- a/spec/rubocop/cli_spec.rb +++ b/spec/rubocop/cli_spec.rb @@ -1461,16 +1461,16 @@ def meow_at_4am? it 'prints an error message for an unrecognized EnforcedStyle' do create_file('example/example1.rb', 'puts "hello"') create_file('example/.rubocop.yml', <<~YAML) - Style/BracesAroundHashParameters: - EnforcedStyle: context + Layout/AccessModifierIndentation: + EnforcedStyle: ident YAML expect(cli.run(%w[--format simple example])).to eq(2) expect($stderr.string) - .to eq(["Error: invalid EnforcedStyle 'context' for " \ - 'Style/BracesAroundHashParameters found in ' \ + .to eq(["Error: invalid EnforcedStyle 'ident' for " \ + 'Layout/AccessModifierIndentation found in ' \ 'example/.rubocop.yml', - 'Valid choices are: braces, no_braces, context_dependent', + 'Valid choices are: outdent, indent', ''].join("\n")) end diff --git a/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb b/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb deleted file mode 100644 index 06c2759db3d..00000000000 --- a/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb +++ /dev/null @@ -1,494 +0,0 @@ -# frozen_string_literal: true - -RSpec.describe RuboCop::Cop::Style::BracesAroundHashParameters, :config do - subject(:cop) { described_class.new(config) } - - shared_examples 'general non-offenses' do - it 'accepts one non-hash parameter' do - expect_no_offenses('where(2)') - end - - it 'accepts multiple non-hash parameters' do - expect_no_offenses('where(1, "2")') - end - - it 'accepts one empty hash parameter' do - expect_no_offenses('where({})') - end - - it 'accepts one empty hash parameter with whitespace' do - expect_no_offenses(['where( { ', - " }\t ) "].join("\n")) - end - - it 'accepts braces that are needed to make a block bind to an inner ' \ - 'method call' do - expect_no_offenses('f 1, { k: proc do h end }') - end - end - - shared_examples 'no_braces and context_dependent non-offenses' do - it 'accepts one hash parameter without braces' do - expect_no_offenses('where(x: "y")') - end - - it 'accepts one hash parameter without braces and with multiple keys' do - expect_no_offenses('where(x: "y", foo: "bar")') - end - - it 'accepts one hash parameter without braces and with one hash value' do - expect_no_offenses('where(x: { "y" => "z" })') - end - - it 'accepts property assignment with braces' do - expect_no_offenses('x.z = { y: "z" }') - end - - it 'accepts operator with a hash parameter with braces' do - expect_no_offenses('x.z - { y: "z" }') - end - end - - shared_examples 'no_braces and context_dependent offenses' do - it 'registers an offense for one non-hash parameter followed by a hash ' \ - 'parameter with braces' do - expect_offense(<<~RUBY) - where(1, { y: 2 }) - ^^^^^^^^ Redundant curly braces around a hash parameter. - RUBY - end - - context 'when using safe navigation operator' do - it 'registers an offense for one non-hash parameter followed by a hash ' \ - 'parameter with braces' do - expect_offense(<<~RUBY) - a&.where(1, { y: 2 }) - ^^^^^^^^ Redundant curly braces around a hash parameter. - RUBY - end - end - - it 'registers an offense for one object method hash parameter with ' \ - 'braces' do - expect_offense(<<~RUBY) - x.func({ y: "z" }) - ^^^^^^^^^^ Redundant curly braces around a hash parameter. - RUBY - end - - it 'registers an offense for one hash parameter with braces' do - expect_offense(<<~RUBY) - where({ x: 1 }) - ^^^^^^^^ Redundant curly braces around a hash parameter. - RUBY - end - - it 'registers an offense for one hash parameter with braces and ' \ - 'whitespace' do - expect_offense(<<~RUBY) - where( - { x: 1 } ) - ^^^^^^^^ Redundant curly braces around a hash parameter. - RUBY - end - - it 'registers an offense for one hash parameter with braces and multiple ' \ - 'keys' do - expect_offense(<<~RUBY) - where({ x: 1, foo: "bar" }) - ^^^^^^^^^^^^^^^^^^^^ Redundant curly braces around a hash parameter. - RUBY - end - end - - shared_examples 'no_braces and context_dependent auto-corrections' do - it 'corrects one non-hash parameter followed by a hash parameter with ' \ - 'braces' do - corrected = autocorrect_source('where(1, { y: 2 })') - expect(corrected).to eq('where(1, y: 2)') - end - - it 'corrects one object method hash parameter with braces' do - corrected = autocorrect_source('x.func({ y: "z" })') - expect(corrected).to eq('x.func(y: "z")') - end - - it 'corrects one hash parameter with braces' do - corrected = autocorrect_source('where({ x: 1 })') - expect(corrected).to eq('where(x: 1)') - end - - it 'corrects one hash parameter with braces and whitespace' do - corrected = autocorrect_source(['where( ', - ' { x: 1 } )'].join("\n")) - expect(corrected).to eq(['where( ', - ' x: 1 )'].join("\n")) - end - - it 'corrects one hash parameter with braces and multiple keys' do - corrected = autocorrect_source('where({ x: 1, foo: "bar" })') - expect(corrected).to eq('where(x: 1, foo: "bar")') - end - - it 'corrects one hash parameter with braces and extra leading whitespace' do - corrected = autocorrect_source('where({ x: 1, y: 2 })') - expect(corrected).to eq('where(x: 1, y: 2)') - end - - it 'corrects one hash parameter with braces and extra trailing ' \ - 'whitespace' do - corrected = autocorrect_source('where({ x: 1, y: 2 })') - expect(corrected).to eq('where(x: 1, y: 2)') - end - - it 'corrects one hash parameter with braces and a trailing comma' do - corrected = autocorrect_source('where({ x: 1, y: 2, })') - expect(corrected).to eq('where(x: 1, y: 2)') - end - - it 'corrects one hash parameter with braces and trailing comma and ' \ - 'whitespace' do - corrected = autocorrect_source('where({ x: 1, y: 2, })') - expect(corrected).to eq('where(x: 1, y: 2)') - end - - it 'corrects one hash parameter with braces without adding extra space' do - corrected = autocorrect_source('get :i, { q: { x: 1 } }') - expect(corrected).to eq('get :i, q: { x: 1 }') - end - - it 'does not break indent' do - src = <<-RUBY - foo({ - a: 1, - b: 2 - }) - RUBY - corrected = autocorrect_source(src) - expect(corrected).to eq(<<-RUBY) - foo( - a: 1, - b: 2 - ) - RUBY - end - - it 'does not remove trailing comma nor realign args' do - src = <<~RUBY - foo({ - a: 1, - b: 2, - }) - RUBY - corrected = autocorrect_source(src) - expect(corrected).to eq(<<~RUBY) - foo( - a: 1, - b: 2, - ) - RUBY - end - - it 'corrects brace removal with 2 extra lines' do - src = <<~RUBY - foo( - { - baz: 10 - } - ) - RUBY - corrected = autocorrect_source(src) - expect(corrected).to eq(<<~RUBY) - foo( - baz: 10 - ) - RUBY - end - - it 'corrects brace removal with extra lines & multiple pairs' do - src = <<~RUBY - foo( - { - qux: "bar", - baz: "bar", - thud: "bar" - } - ) - RUBY - corrected = autocorrect_source(src) - expect(corrected).to eq(<<~RUBY) - foo( - qux: "bar", - baz: "bar", - thud: "bar" - ) - RUBY - end - - it 'corrects brace removal with lower extra line' do - src = <<~RUBY - foo({ - baz: 7 - } - ) - RUBY - corrected = autocorrect_source(src) - expect(corrected).to eq(<<~RUBY) - foo( - baz: 7 - ) - RUBY - end - - it 'corrects brace removal with top extra line' do - src = <<~RUBY - foo( - { - baz: 5 - }) - RUBY - corrected = autocorrect_source(src) - expect(corrected).to eq(<<~RUBY) - foo( - baz: 5 - ) - RUBY - end - - context 'with a comment following the last key-value pair' do - it 'corrects and leaves line breaks' do - src = <<~RUBY - r = opts.merge({ - p1: opts[:a], - p2: (opts[:b] || opts[:c]) # a comment - }) - RUBY - corrected = autocorrect_source(src) - expect(corrected).to eq(<<~RUBY) - r = opts.merge( - p1: opts[:a], - p2: (opts[:b] || opts[:c]) # a comment - ) - RUBY - end - end - - context 'in a method call without parentheses' do - it 'corrects a hash parameter with trailing comma' do - src = 'get :i, { x: 1, }' - corrected = autocorrect_source(src) - expect(corrected).to eq('get :i, x: 1') - end - end - - context 'in a method call with multi line arguments without parentheses' do - it 'removes hash braces' do - src = "render 'foo', {\n foo: bar\n}" - corrected = autocorrect_source(src) - expect(corrected).to eq("render 'foo', \n foo: bar\n") - end - end - end - - context 'when EnforcedStyle is no_braces' do - let(:cop_config) { { 'EnforcedStyle' => 'no_braces' } } - - context 'for correct code' do - include_examples 'general non-offenses' - include_examples 'no_braces and context_dependent non-offenses' - end - - context 'for incorrect code' do - include_examples 'no_braces and context_dependent offenses' - - it 'registers an offense for two hash parameters with braces' do - expect_offense(<<~RUBY) - where({ x: 1 }, { y: 2 }) - ^^^^^^^^ Redundant curly braces around a hash parameter. - RUBY - end - end - - describe '#autocorrect' do - include_examples 'no_braces and context_dependent auto-corrections' - - it 'corrects one hash parameter with braces' do - corrected = autocorrect_source('where(1, { x: 1 })') - expect(corrected).to eq('where(1, x: 1)') - end - - it 'corrects two hash parameters with braces' do - corrected = autocorrect_source('where(1, { x: 1 }, { y: 2 })') - expect(corrected).to eq('where(1, { x: 1 }, y: 2)') - end - - it 'corrects two hash parameters with braces & extra lines' do - src = <<~RUBY - foo( - { - qux: 9 - }, - { - bar: 0 - } - ) - RUBY - corrected = autocorrect_source(src) - expect(corrected).to eq(<<~RUBY) - foo( - { - qux: 9 - }, - bar: 0 - ) - RUBY - end - - it 'corrects parameters with braces & trailing comma' do - corrected = autocorrect_source('foo(1, { x: 1, y: 2, },)') - expect(corrected).to eq('foo(1, x: 1, y: 2,)') - end - - it 'corrects hash multiline parameters with braces & trailing comma' do - src = <<~RUBY - foo( - { - foo: 1, - bar: 2, - } , - ) - RUBY - corrected = autocorrect_source(src) - expect(corrected).to eq(<<~RUBY) - foo( - foo: 1, - bar: 2, - ) - RUBY - end - - it 'corrects when the opening brace is before the first hash element' \ - 'at same line' do - corrected = autocorrect_source(<<~RUBY) - foo = Foo.new( - { foo: 'foo', - bar: 'bar', - baz: 'this is the last element'} - ) - RUBY - - expect(corrected).to eq(<<~RUBY) - foo = Foo.new( - foo: 'foo', - bar: 'bar', - baz: 'this is the last element' - ) - RUBY - end - end - end - - context 'when EnforcedStyle is context_dependent' do - let(:cop_config) { { 'EnforcedStyle' => 'context_dependent' } } - - context 'for correct code' do - include_examples 'general non-offenses' - include_examples 'no_braces and context_dependent non-offenses' - - it 'accepts two hash parameters with braces' do - expect_no_offenses('where({ x: 1 }, { y: 2 })') - end - end - - context 'for incorrect code' do - include_examples 'no_braces and context_dependent offenses' - - it 'registers an offense for one hash parameter with braces and one ' \ - 'without' do - expect_offense(<<~RUBY) - where({ x: 1 }, y: 2) - ^^^^ Missing curly braces around a hash parameter. - RUBY - end - end - - describe '#autocorrect' do - include_examples 'no_braces and context_dependent auto-corrections' - - it 'corrects one hash parameter with braces and one without' do - corrected = autocorrect_source('where(1, { x: 1 }, y: 2)') - expect(corrected).to eq('where(1, { x: 1 }, {y: 2})') - end - - it 'corrects one hash parameter with braces' do - corrected = autocorrect_source('where(1, { x: 1 })') - expect(corrected).to eq('where(1, x: 1)') - end - end - end - - context 'when EnforcedStyle is braces' do - let(:cop_config) { { 'EnforcedStyle' => 'braces' } } - - context 'for correct code' do - include_examples 'general non-offenses' - - it 'accepts one hash parameter with braces' do - expect_no_offenses('where({ x: 1 })') - end - - it 'accepts multiple hash parameters with braces' do - expect_no_offenses('where({ x: 1 }, { y: 2 })') - end - - it 'accepts one hash parameter with braces and whitespace' do - expect_no_offenses(<<~RUBY) - where( { x: 1 - } ) - RUBY - end - end - - context 'for incorrect code' do - it 'registers an offense for one hash parameter without braces' do - expect_offense(<<~RUBY) - where(x: "y") - ^^^^^^ Missing curly braces around a hash parameter. - RUBY - end - - it 'registers an offense for one hash parameter with multiple keys and ' \ - 'without braces' do - expect_offense(<<~RUBY) - where(x: "y", foo: "bar") - ^^^^^^^^^^^^^^^^^^ Missing curly braces around a hash parameter. - RUBY - end - - it 'registers an offense for one hash parameter without braces with ' \ - 'one hash value' do - expect_offense(<<~RUBY) - where(x: { "y" => "z" }) - ^^^^^^^^^^^^^^^^^ Missing curly braces around a hash parameter. - RUBY - end - end - - describe '#autocorrect' do - it 'corrects one hash parameter without braces' do - corrected = autocorrect_source('where(x: "y")') - expect(corrected).to eq('where({x: "y"})') - end - - it 'corrects one hash parameter with multiple keys and without braces' do - corrected = autocorrect_source('where(x: "y", foo: "bar")') - expect(corrected).to eq('where({x: "y", foo: "bar"})') - end - - it 'corrects one hash parameter without braces with one hash value' do - corrected = autocorrect_source('where(x: { "y" => "z" })') - expect(corrected).to eq('where({x: { "y" => "z" }})') - end - end - end -end