From 657c60ee9054210c70a6e1bc786652fce72cb913 Mon Sep 17 00:00:00 2001 From: Daniel Vandersluis Date: Wed, 5 May 2021 10:30:59 -0400 Subject: [PATCH] [Fix #9773] Fix `Style/EmptyLiteral` to not register offenses for `String.new` when `Style/FrozenStringLiteral` is enabled. --- ...x_fix_styleemptyliteral_to_not_register.md | 1 + .../cop/mixin/frozen_string_literal.rb | 6 +++ lib/rubocop/cop/style/empty_literal.rb | 9 ++++- spec/rubocop/cop/style/empty_literal_spec.rb | 40 +++++++++++++++---- 4 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 changelog/fix_fix_styleemptyliteral_to_not_register.md diff --git a/changelog/fix_fix_styleemptyliteral_to_not_register.md b/changelog/fix_fix_styleemptyliteral_to_not_register.md new file mode 100644 index 00000000000..9b2fa75719b --- /dev/null +++ b/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][]) diff --git a/lib/rubocop/cop/mixin/frozen_string_literal.rb b/lib/rubocop/cop/mixin/frozen_string_literal.rb index 42cad089a2e..14b9b0065f3 100644 --- a/lib/rubocop/cop/mixin/frozen_string_literal.rb +++ b/lib/rubocop/cop/mixin/frozen_string_literal.rb @@ -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? diff --git a/lib/rubocop/cop/style/empty_literal.rb b/lib/rubocop/cop/style/empty_literal.rb index 87786097059..587f6b91d03 100644 --- a/lib/rubocop/cop/style/empty_literal.rb +++ b/lib/rubocop/cop/style/empty_literal.rb @@ -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 @@ -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 diff --git a/spec/rubocop/cop/style/empty_literal_spec.rb b/spec/rubocop/cop/style/empty_literal_spec.rb index 44df2bb45c7..e5d457a5a07 100644 --- a/spec/rubocop/cop/style/empty_literal_spec.rb +++ b/spec/rubocop/cop/style/empty_literal_spec.rb @@ -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() @@ -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 @@ -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