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

switch SpaceAfterPunctuation mixin to use SpaceInsideHashLiteralBraces config #3016

Merged
merged 1 commit into from
May 6, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
* [#3083](https://github.com/bbatsov/rubocop/issues/3083): Do not register an offense for splat block args in `Style/SymbolProc`. ([@rrosenblum][])
* [#3063](https://github.com/bbatsov/rubocop/issues/3063): Don't auto-correct `a + \` into `a + \\` in `Style/LineEndConcatenation`. ([@jonas054][])
* [#3034](https://github.com/bbatsov/rubocop/issues/3034): Report offenses for `RuntimeError.new(msg)` in `Style/RedundantException`. ([@jonas054][])
* [#3016](https://github.com/bbatsov/rubocop/issues/3016): `Style/SpaceAfterComma` now uses `Style/SpaceInsideHashLiteralBraces`'s setting. ([@ptarjan][])

### Changes

Expand Down
3 changes: 1 addition & 2 deletions lib/rubocop/cop/mixin/space_after_punctuation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ def investigate(processed_source)
end

def space_forbidden_before_rcurly?
cfg = config.for_cop('Style/SpaceInsideBlockBraces')
style = cfg['EnforcedStyle'] || 'space'
style = space_style_before_rcurly
style == 'no_space'
end

Expand Down
5 changes: 5 additions & 0 deletions lib/rubocop/cop/style/space_after_comma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ module Style
class SpaceAfterComma < Cop
include SpaceAfterPunctuation

def space_style_before_rcurly
cfg = config.for_cop('Style/SpaceInsideHashLiteralBraces')
cfg['EnforcedStyle'] || 'space'
end

def kind(token)
'comma' if token.type == :tCOMMA
end
Expand Down
5 changes: 5 additions & 0 deletions lib/rubocop/cop/style/space_after_semicolon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ module Style
class SpaceAfterSemicolon < Cop
include SpaceAfterPunctuation

def space_style_before_rcurly
cfg = config.for_cop('Style/SpaceInsideBlockBraces')
cfg['EnforcedStyle'] || 'space'
end

def kind(token)
'semicolon' if token.type == :tSEMI
end
Expand Down
21 changes: 21 additions & 0 deletions spec/rubocop/cli/cli_autocorrect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -993,4 +993,25 @@ def abs(path)
'puts "Hello", 123_456',
''].join("\n"))
end

it 'handles different SpaceInsideBlockBraces and ' \
'SpaceInsideHashLiteralBraces' do
create_file('example.rb', ['{foo: bar,',
' bar: baz,}',
'foo.each {bar;}'])
create_file('.rubocop.yml', [
'Style/SpaceInsideBlockBraces:',
' EnforcedStyle: space',
'Style/SpaceInsideHashLiteralBraces:',
' EnforcedStyle: no_space',
'Style/TrailingCommaInLiteral:',
' EnforcedStyleForMultiline: consistent_comma'
])
expect(cli.run(%w(--auto-correct))).to eq(1)
expect($stderr.string).to eq('')
expect(IO.read('example.rb')).to eq(['{foo: bar,',
' bar: baz,}',
'foo.each { bar; }',
''].join("\n"))
end
end
42 changes: 41 additions & 1 deletion spec/rubocop/cop/style/space_after_comma_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
require 'spec_helper'

describe RuboCop::Cop::Style::SpaceAfterComma do
subject(:cop) { described_class.new }
subject(:cop) { described_class.new(config) }
let(:config) do
RuboCop::Config.new('Style/SpaceInsideHashLiteralBraces' => brace_config)
end
let(:brace_config) { {} }

shared_examples 'ends with an item' do |items, correct_items|
it 'registers an offense' do
Expand Down Expand Up @@ -46,4 +50,40 @@

it_behaves_like 'ends with an item', '1,2', '1, 2'
end

context 'inside hash braces' do
shared_examples 'common behavior' do
it 'accepts a space between a comma and a closing brace' do
inspect_source(cop, '{ foo:bar, }')
expect(cop.messages).to be_empty
end
end

context 'when EnforcedStyle for SpaceInsideBlockBraces is space' do
let(:brace_config) do
{ 'Enabled' => true, 'EnforcedStyle' => 'space' }
end

it_behaves_like 'common behavior'

it 'registers an offense for no space between a comma and a ' \
'closing brace' do
inspect_source(cop, '{ foo:bar,}')
expect(cop.messages).to eq(['Space missing after comma.'])
end
end

context 'when EnforcedStyle for SpaceInsideBlockBraces is no_space' do
let(:brace_config) do
{ 'Enabled' => true, 'EnforcedStyle' => 'no_space' }
end

it_behaves_like 'common behavior'

it 'accepts no space between a comma and a closing brace' do
inspect_source(cop, '{foo:bar,}')
expect(cop.messages).to be_empty
end
end
end
end