Skip to content

Commit

Permalink
Fix a false negative for Layout/HashAlignment
Browse files Browse the repository at this point in the history
Resolves standardrb/standard#300

This PR fixes a false negative for `Layout/HashAlignment` when
setting `EnforcedStyle: with_fixed_indentation` of `Layout/ArgumentAlignment`
and using misaligned keyword arguments.
  • Loading branch information
koic committed Jun 3, 2021
1 parent 27cb14d commit 2cc2f98
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
@@ -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][])
3 changes: 2 additions & 1 deletion lib/rubocop/cop/layout/hash_alignment.rb
Expand Up @@ -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!
Expand Down
32 changes: 32 additions & 0 deletions spec/rubocop/cop/layout/hash_alignment_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit 2cc2f98

Please sign in to comment.