From 2cc2f9810000f3d33d8718937c22e774397f3244 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Thu, 3 Jun 2021 21:08:40 +0900 Subject: [PATCH] Fix a false negative for `Layout/HashAlignment` Resolves https://github.com/testdouble/standard/pull/300 This PR fixes a false negative for `Layout/HashAlignment` when setting `EnforcedStyle: with_fixed_indentation` of `Layout/ArgumentAlignment` and using misaligned keyword arguments. --- ...alse_negative_for_layout_hash_alignment.md | 1 + lib/rubocop/cop/layout/hash_alignment.rb | 3 +- .../rubocop/cop/layout/hash_alignment_spec.rb | 32 +++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_a_false_negative_for_layout_hash_alignment.md diff --git a/changelog/fix_a_false_negative_for_layout_hash_alignment.md b/changelog/fix_a_false_negative_for_layout_hash_alignment.md new file mode 100644 index 000000000000..491f92445b7d --- /dev/null +++ b/changelog/fix_a_false_negative_for_layout_hash_alignment.md @@ -0,0 +1 @@ +* [#9849](https://github.com/rubocop/rubocop/pull/9849): Fix a false negative for `Layout/HashAlignment` when setting `EnforcedStyle: with_fixed_indentation` of `Layout/ArgumentAlignment` and using misaligned keyword arguments. ([@koic][]) diff --git a/lib/rubocop/cop/layout/hash_alignment.rb b/lib/rubocop/cop/layout/hash_alignment.rb index c70892c3cbd1..afab3fb27168 100644 --- a/lib/rubocop/cop/layout/hash_alignment.rb +++ b/lib/rubocop/cop/layout/hash_alignment.rb @@ -217,7 +217,8 @@ def on_hash(node) private def autocorrect_incompatible_with_other_cops?(node) - enforce_first_argument_with_fixed_indentation? && !node.braces? && node.parent&.call_type? + enforce_first_argument_with_fixed_indentation? && + node.parent&.call_type? && node.parent.loc.line == node.pairs.first.loc.line end def reset! diff --git a/spec/rubocop/cop/layout/hash_alignment_spec.rb b/spec/rubocop/cop/layout/hash_alignment_spec.rb index bf99bfced842..1c43e8783bf5 100644 --- a/spec/rubocop/cop/layout/hash_alignment_spec.rb +++ b/spec/rubocop/cop/layout/hash_alignment_spec.rb @@ -139,6 +139,38 @@ def example } RUBY end + + it 'registers and corrects an offense when using misaligned keyword arguments' do + expect_offense(<<~RUBY) + config.fog_credentials_as_kwargs( + provider: 'AWS', + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Align the keys of a hash literal if they span more than one line. + aws_access_key_id: ENV['S3_ACCESS_KEY'], + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Align the keys of a hash literal if they span more than one line. + aws_secret_access_key: ENV['S3_SECRET'], + region: ENV['S3_REGION'], + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Align the keys of a hash literal if they span more than one line. + ) + RUBY + + expect_correction(<<~RUBY) + config.fog_credentials_as_kwargs( + provider: 'AWS', + aws_access_key_id: ENV['S3_ACCESS_KEY'], + aws_secret_access_key: ENV['S3_SECRET'], + region: ENV['S3_REGION'], + ) + RUBY + end + + it 'does not register an offense using aligned hash literal' do + expect_no_offenses(<<~RUBY) + { + oh: :io, + hi: 'neat' + } + RUBY + end end context 'always ignore last argument hash' do