forked from rubocop/rubocop
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rubocop#8271 from fatkodima/hash-array-last-item
Add new `Style/HashArrayLastItem` cop
- Loading branch information
Showing
7 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Style | ||
# Checks for presence or absence of braces around hash literal as a last | ||
# array item depending on configuration. | ||
# | ||
# @example EnforcedStyle: braces (default) | ||
# # bad | ||
# [1, 2, one: 1, two: 2] | ||
# | ||
# # good | ||
# [1, 2, { one: 1, two: 2 }] | ||
# | ||
# @example EnforcedStyle: no_braces | ||
# # bad | ||
# [1, 2, { one: 1, two: 2 }] | ||
# | ||
# # good | ||
# [1, 2, one: 1, two: 2] | ||
# | ||
class HashAsLastArrayItem < Base | ||
include ConfigurableEnforcedStyle | ||
extend AutoCorrector | ||
|
||
def on_hash(node) | ||
return unless node.parent&.array_type? | ||
|
||
if braces_style? | ||
check_braces(node) | ||
else | ||
check_no_braces(node) | ||
end | ||
end | ||
|
||
private | ||
|
||
def check_braces(node) | ||
return if node.braces? | ||
|
||
add_offense(node, message: 'Wrap hash in `{` and `}`.') do |corrector| | ||
corrector.wrap(node, '{', '}') | ||
end | ||
end | ||
|
||
def check_no_braces(node) | ||
return unless node.braces? | ||
|
||
add_offense(node, message: 'Omit the braces around the hash.') do |corrector| | ||
corrector.remove(node.loc.begin) | ||
corrector.remove(node.loc.end) | ||
end | ||
end | ||
|
||
def braces_style? | ||
style == :braces | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Style::HashAsLastArrayItem, :config do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
context 'when EnforcedStyle is braces' do | ||
let(:cop_config) do | ||
{ 'EnforcedStyle' => 'braces' } | ||
end | ||
|
||
it 'registers an offense and corrects when hash without braces' do | ||
expect_offense(<<~RUBY) | ||
[1, 2, one: 1, two: 2] | ||
^^^^^^^^^^^^^^ Wrap hash in `{` and `}`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
[1, 2, {one: 1, two: 2}] | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when hash with braces' do | ||
expect_no_offenses(<<~RUBY) | ||
[1, 2, { one: 1, two: 2 }] | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when hash is not inside array' do | ||
expect_no_offenses(<<~RUBY) | ||
foo(one: 1, two: 2) | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when EnforcedStyle is no_braces' do | ||
let(:cop_config) do | ||
{ 'EnforcedStyle' => 'no_braces' } | ||
end | ||
|
||
it 'registers an offense and corrects when hash with braces' do | ||
expect_offense(<<~RUBY) | ||
[1, 2, { one: 1, two: 2 }] | ||
^^^^^^^^^^^^^^^^^^ Omit the braces around the hash. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
[1, 2, one: 1, two: 2 ] | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when hash without braces' do | ||
expect_no_offenses(<<~RUBY) | ||
[1, 2, one: 1, two: 2] | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when hash is not inside array' do | ||
expect_no_offenses(<<~RUBY) | ||
foo({ one: 1, two: 2 }) | ||
RUBY | ||
end | ||
end | ||
end |