Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a false negative for Layout/HashAlignment #9849

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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