From 52ba69e233225ff2b6956e412119a612864cf00c Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Tue, 6 Jul 2021 11:01:43 +0900 Subject: [PATCH] [Fix #9914] Fix an error for `Layout/HashAlignment` Fixes #9914. This PR fixes an error for `Layout/HashAlignment` when using aligned hash argument for `proc.()`. --- changelog/fix_an_error_for_layout_hash_alignment.md | 1 + lib/rubocop/cop/layout/hash_alignment.rb | 2 +- spec/rubocop/cop/layout/hash_alignment_spec.rb | 6 ++++++ 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_an_error_for_layout_hash_alignment.md diff --git a/changelog/fix_an_error_for_layout_hash_alignment.md b/changelog/fix_an_error_for_layout_hash_alignment.md new file mode 100644 index 00000000000..282dd3237b8 --- /dev/null +++ b/changelog/fix_an_error_for_layout_hash_alignment.md @@ -0,0 +1 @@ +* [#9914](https://github.com/rubocop/rubocop/issues/9914): Fix an error for `Layout/HashAlignment` when using aligned hash argument for `proc.()`. ([@koic][]) diff --git a/lib/rubocop/cop/layout/hash_alignment.rb b/lib/rubocop/cop/layout/hash_alignment.rb index 9ff70a1d94e..d94b5291fac 100644 --- a/lib/rubocop/cop/layout/hash_alignment.rb +++ b/lib/rubocop/cop/layout/hash_alignment.rb @@ -220,7 +220,7 @@ def on_hash(node) def autocorrect_incompatible_with_other_cops?(node) enforce_first_argument_with_fixed_indentation? && node.pairs.any? && - node.parent&.call_type? && node.parent.loc.selector.line == node.pairs.first.loc.line + node.parent&.call_type? && node.parent.loc.selector&.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 946c06ac3b2..2b41ae05610 100644 --- a/spec/rubocop/cop/layout/hash_alignment_spec.rb +++ b/spec/rubocop/cop/layout/hash_alignment_spec.rb @@ -177,6 +177,12 @@ def example foo({}) RUBY end + + it 'does not register an offense using aligned hash argument for `proc.()`' do + expect_no_offenses(<<~RUBY) + proc.(key: value) + RUBY + end end context 'always ignore last argument hash' do