-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add spaces as EnforcedStyle to SpaceInsideParens (#5672)
Simply supply `EnforcedStyle: space` in your rubocop.yml configuration and instead of enforcing no spaces inside parens, it will enforce spaces. Even supports auto-correct! Stylistic choice, to be sure, but one that can help alleviate cluttered code and parentheses hell.
- Loading branch information
1 parent
ed013be
commit c6efac6
Showing
5 changed files
with
210 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,120 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Layout::SpaceInsideParens do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'registers an offense for spaces inside parens' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
f( 3) | ||
^ Space inside parentheses detected. | ||
g = (a + 3 ) | ||
^ Space inside parentheses detected. | ||
RUBY | ||
end | ||
RSpec.describe RuboCop::Cop::Layout::SpaceInsideParens, :config do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
it 'accepts parentheses in block parameter list' do | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
list.inject(Tms.new) { |sum, (label, item)| | ||
} | ||
RUBY | ||
end | ||
context 'when EnforcedStyle is no_space' do | ||
let(:cop_config) { { 'EnforcedStyle' => 'no_space' } } | ||
|
||
it 'accepts parentheses with no spaces' do | ||
expect_no_offenses('split("\\n")') | ||
end | ||
it 'registers an offense for spaces inside parens' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
f( 3) | ||
^ Space inside parentheses detected. | ||
g = (a + 3 ) | ||
^ Space inside parentheses detected. | ||
RUBY | ||
end | ||
|
||
it 'accepts parentheses with line break' do | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
f( | ||
1) | ||
RUBY | ||
end | ||
it 'accepts parentheses in block parameter list' do | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
list.inject(Tms.new) { |sum, (label, item)| | ||
} | ||
RUBY | ||
end | ||
|
||
it 'accepts parentheses with no spaces' do | ||
expect_no_offenses('split("\\n")') | ||
end | ||
|
||
it 'accepts parentheses with line break' do | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
f( | ||
1) | ||
RUBY | ||
end | ||
|
||
it 'accepts parentheses with comment and line break' do | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
f( # Comment | ||
1) | ||
RUBY | ||
end | ||
|
||
it 'accepts parentheses with comment and line break' do | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
f( # Comment | ||
1) | ||
RUBY | ||
it 'auto-corrects unwanted space' do | ||
new_source = autocorrect_source(<<-RUBY.strip_indent) | ||
f( 3) | ||
g = ( a + 3 ) | ||
RUBY | ||
expect(new_source).to eq(<<-RUBY.strip_indent) | ||
f(3) | ||
g = (a + 3) | ||
RUBY | ||
end | ||
end | ||
|
||
it 'auto-corrects unwanted space' do | ||
new_source = autocorrect_source(<<-RUBY.strip_indent) | ||
f( 3) | ||
g = ( a + 3 ) | ||
RUBY | ||
expect(new_source).to eq(<<-RUBY.strip_indent) | ||
f(3) | ||
g = (a + 3) | ||
RUBY | ||
context 'when EnforcedStyle is space' do | ||
let(:cop_config) { { 'EnforcedStyle' => 'space' } } | ||
|
||
it 'registers an offense for no spaces inside parens' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
f( 3) | ||
^ No space inside parentheses detected. | ||
g = (a + 3 ) | ||
^ No space inside parentheses detected. | ||
split("\\n") | ||
^ No space inside parentheses detected. | ||
^ No space inside parentheses detected. | ||
RUBY | ||
end | ||
|
||
it 'accepts parentheses in block parameter list with no spaces' do | ||
expect_offense(<<-RUBY.strip_indent) | ||
list.inject( Tms.new ) { |sum, (label, item)| | ||
^ No space inside parentheses detected. | ||
^ No space inside parentheses detected. | ||
} | ||
RUBY | ||
end | ||
|
||
it 'accepts parentheses with spaces' do | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
f( 3 ) | ||
g = ( a + 3 ) | ||
split( "\\n" ) | ||
RUBY | ||
end | ||
|
||
it 'accepts parentheses with line break' do | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
f( | ||
1 ) | ||
RUBY | ||
end | ||
|
||
it 'accepts parentheses with comment and line break' do | ||
expect_no_offenses(<<-RUBY.strip_indent) | ||
f( # Comment | ||
1 ) | ||
RUBY | ||
end | ||
|
||
it 'auto-corrects wanted space' do | ||
new_source = autocorrect_source(<<-RUBY.strip_indent) | ||
f(3) | ||
f( 3) | ||
f(3 ) | ||
g = (a + 3) | ||
g = ( a + 3) | ||
g = (a + 3 ) | ||
RUBY | ||
expect(new_source).to eq(<<-RUBY.strip_indent) | ||
f( 3 ) | ||
f( 3 ) | ||
f( 3 ) | ||
g = ( a + 3 ) | ||
g = ( a + 3 ) | ||
g = ( a + 3 ) | ||
RUBY | ||
end | ||
end | ||
end |