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

EmptyLiteral autocorrector respects StringLiterals/EnforcedStyle config #2594

Merged
merged 1 commit into from
Jan 8, 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 @@ -67,6 +67,7 @@

### Bug Fixes

* [#2594](https://github.com/bbatsov/rubocop/issues/2594): `Style/EmptyLiteral` autocorrector respects `Style/StringLiterals:EnforcedStyle` config. ([@DNNX][])
* [#2411](https://github.com/bbatsov/rubocop/issues/2411): Make local inherited configuration override configuration loaded from gems. ([@jonas054][])
* [#2413](https://github.com/bbatsov/rubocop/issues/2413): Allow `%Q` for dynamic strings with double quotes inside them. ([@jonas054][])
* [#2404](https://github.com/bbatsov/rubocop/issues/2404): `Style/Next` does not remove comments when auto-correcting. ([@lumeet][])
Expand Down
16 changes: 15 additions & 1 deletion lib/rubocop/cop/style/empty_literal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,25 @@ def autocorrect(node)
return if first_arg_in_method_call_without_parentheses?(node)
'{}'
when STR_NODE
"''"
if enforce_double_quotes?
'""'
else
"''"
end
end
->(corrector) { corrector.replace(node.source_range, name) }
end

private

def enforce_double_quotes?
string_literals_config['EnforcedStyle'] == 'double_quotes'
end

def string_literals_config
config.for_cop('Style/StringLiterals')
end

def first_arg_in_method_call_without_parentheses?(node)
return false unless node.parent && node.parent.send_type?

Expand Down
17 changes: 17 additions & 0 deletions spec/rubocop/cop/style/empty_literal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,22 @@
new_source = autocorrect_source(cop, 'test = String.new')
expect(new_source).to eq("test = ''")
end

context 'when double-quoted string literals are preferred' do
let(:config) do
RuboCop::Config.new(
'Style/StringLiterals' =>
{
'EnforcedStyle' => 'double_quotes'
}
)
end
subject(:cop) { described_class.new(config) }

it 'auto-corrects String.new to a double-quoted empty string literal' do
new_source = autocorrect_source(cop, 'test = String.new')
expect(new_source).to eq('test = ""')
end
end
end
end