Skip to content

Commit

Permalink
[Fix rubocop#9773] Fix Style/EmptyLiteral to not register offenses …
Browse files Browse the repository at this point in the history
…for `String.new` when `Style/FrozenStringLiteral` is enabled.
  • Loading branch information
dvandersluis committed May 5, 2021
1 parent 5ba77b9 commit 657c60e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
1 change: 1 addition & 0 deletions changelog/fix_fix_styleemptyliteral_to_not_register.md
@@ -0,0 +1 @@
* [#9773](https://github.com/rubocop/rubocop/issues/9773): Fix `Style/EmptyLiteral` to not register offenses for `String.new` when `Style/FrozenStringLiteral` is enabled. ([@dvandersluis][])
6 changes: 6 additions & 0 deletions lib/rubocop/cop/mixin/frozen_string_literal.rb
Expand Up @@ -35,6 +35,12 @@ def frozen_string_literals_enabled?
leading_comment_lines.any? { |line| MagicComment.parse(line).frozen_string_literal? }
end

def frozen_string_literals_disabled?
leading_comment_lines.any? do |line|
MagicComment.parse(line).frozen_string_literal == false
end
end

def frozen_string_literal_specified?
leading_comment_lines.any? do |line|
MagicComment.parse(line).frozen_string_literal_specified?
Expand Down
9 changes: 8 additions & 1 deletion lib/rubocop/cop/style/empty_literal.rb
Expand Up @@ -62,7 +62,7 @@ def offense_message(node)
ARR_MSG
elsif offense_hash_node?(node)
HASH_MSG
elsif str_node(node) && !frozen_string_literals_enabled?
elsif str_node(node) && !frozen_strings?
format(STR_MSG, prefer: preferred_string_literal)
end
end
Expand Down Expand Up @@ -125,6 +125,13 @@ def correction(node)
end
end
end

def frozen_strings?
return true if frozen_string_literals_enabled?

frozen_string_cop_enabled = config.for_cop('Style/FrozenStringLiteral')['Enabled']
frozen_string_cop_enabled && !frozen_string_literals_disabled?
end
end
end
end
Expand Down
40 changes: 33 additions & 7 deletions spec/rubocop/cop/style/empty_literal_spec.rb
Expand Up @@ -207,6 +207,8 @@ def foo
end

describe 'Empty String', :config do
let(:other_cops) { { 'Style/FrozenStringLiteral' => { 'Enabled' => false } } }

it 'registers an offense for String.new()' do
expect_offense(<<~RUBY)
test = String.new()
Expand Down Expand Up @@ -249,13 +251,10 @@ def foo
end

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

it 'registers an offense for String.new' do
Expand Down Expand Up @@ -289,5 +288,32 @@ def foo
RUBY
end
end

context 'when Style/FrozenStringLiteral is enabled' do
let(:other_cops) { { 'Style/FrozenStringLiteral' => { 'Enabled' => true } } }

context 'and there is no magic comment' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
test = String.new
RUBY
end
end

context 'and there is a frozen-string-literal: false comment' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
# frozen-string-literal: false
test = String.new
^^^^^^^^^^ Use string literal `''` instead of `String.new`.
RUBY

expect_correction(<<~RUBY)
# frozen-string-literal: false
test = ''
RUBY
end
end
end
end
end

0 comments on commit 657c60e

Please sign in to comment.