diff --git a/CHANGELOG.md b/CHANGELOG.md index 9017b32c5d81..b8e3adbbed35 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,10 @@ * [#2660](https://github.com/bbatsov/rubocop/issues/2660): `Style/TrailingUnderscoreVariable` shows recommended code in its offense message. ([@alexdowad][]) * [#2671](https://github.com/bbatsov/rubocop/issues/2671): `Style/WordArray` doesn't attempt to inspect strings with invalid encoding, to avoid failing with an encoding error. ([@alexdowad][]) +### Changes + +* [#2739](https://github.com/bbatsov/rubocop/issues/2739): Change the configuration option `when_needed` in `Style/FrozenStringLiteralComment` to add a `frozen_string_literal` comment to all files when the `TargetRubyVersion` is set to 2.3+. ([@rrosenblum][]) + ## 0.37.0 (04/02/2016) ### New features diff --git a/config/default.yml b/config/default.yml index 47a478c1d0d5..6b1a6ecb21d9 100644 --- a/config/default.yml +++ b/config/default.yml @@ -481,9 +481,8 @@ Style/FormatString: Style/FrozenStringLiteralComment: EnforcedStyle: when_needed SupportedStyles: - # `when_needed` will add the frozen string literal comment to files that call - # `freeze` or `<<` on a string literal. It will only add the comment to files - # when running Ruby 2.3.0+. + # `when_needed` will add the frozen string literal comment to files + # only when the `TargetRubyVersion` is set to 2.3.0+. - when_needed # `always` will always add the frozen string literal comment to a file # regardless of the Ruby version or if `freeze` or `<<` are called on a diff --git a/lib/rubocop/cop/style/frozen_string_literal_comment.rb b/lib/rubocop/cop/style/frozen_string_literal_comment.rb index 98f145592420..beaf8258e257 100644 --- a/lib/rubocop/cop/style/frozen_string_literal_comment.rb +++ b/lib/rubocop/cop/style/frozen_string_literal_comment.rb @@ -16,11 +16,8 @@ class FrozenStringLiteralComment < Cop MSG = 'Missing frozen string literal comment.'.freeze SHEBANG = '#!'.freeze - def_node_matcher :frozen_strings, '{(send {dstr str} :<< ...) - (send {dstr str} :freeze)}' - def investigate(processed_source) - return unless style == :always + return if style == :when_needed && target_ruby_version < 2.3 return if processed_source.buffer.source.empty? return if frozen_string_literal_comment_exists?(processed_source) @@ -28,14 +25,6 @@ def investigate(processed_source) offense(processed_source) end - def on_send(node) - return unless style == :when_needed - return if target_ruby_version < 2.3 - return if frozen_string_literal_comment_exists?(processed_source) - - frozen_strings(node) { offense(processed_source) } - end - def autocorrect(_node) lambda do |corrector| last_special_comment = last_special_comment(processed_source) diff --git a/spec/rubocop/cop/style/frozen_string_literal_comment_spec.rb b/spec/rubocop/cop/style/frozen_string_literal_comment_spec.rb index af003222f357..e3eec4acef5a 100644 --- a/spec/rubocop/cop/style/frozen_string_literal_comment_spec.rb +++ b/spec/rubocop/cop/style/frozen_string_literal_comment_spec.rb @@ -277,62 +277,60 @@ end end - if RUBY_VERSION < '2.3.0' - context 'ruby < 2.3' do - context 'target_ruby_version < 2.3', :ruby19 do - it 'accepts freezing a string' do - inspect_source(cop, '"x".freeze') + context 'ruby < 2.3' do + context 'target_ruby_version < 2.3', :ruby19 do + it 'accepts freezing a string' do + inspect_source(cop, '"x".freeze') - expect(cop.offenses).to be_empty - end + expect(cop.offenses).to be_empty + end - it 'accepts calling << on a string' do - inspect_source(cop, '"x" << "y"') + it 'accepts calling << on a string' do + inspect_source(cop, '"x" << "y"') - expect(cop.offenses).to be_empty - end + expect(cop.offenses).to be_empty + end - it 'accepts freezing a string with interpolation' do - inspect_source(cop, '"#{foo}bar".freeze') + it 'accepts freezing a string with interpolation' do + inspect_source(cop, '"#{foo}bar".freeze') - expect(cop.offenses).to be_empty - end + expect(cop.offenses).to be_empty + end - it 'accepts calling << on a string with interpolation' do - inspect_source(cop, '"#{foo}bar" << "baz"') + it 'accepts calling << on a string with interpolation' do + inspect_source(cop, '"#{foo}bar" << "baz"') - expect(cop.offenses).to be_empty - end + expect(cop.offenses).to be_empty end + end - context 'target_ruby_version 2.3+', :ruby23 do - it 'accepts freezing a string' do - inspect_source(cop, '"x".freeze') + context 'target_ruby_version 2.3+', :ruby23 do + it 'accepts freezing a string' do + inspect_source(cop, '"x".freeze') - expect(cop.messages) - .to eq(['Missing frozen string literal comment.']) - end + expect(cop.messages) + .to eq(['Missing frozen string literal comment.']) + end - it 'accepts calling << on a string' do - inspect_source(cop, '"x" << "y"') + it 'accepts calling << on a string' do + inspect_source(cop, '"x" << "y"') - expect(cop.messages) - .to eq(['Missing frozen string literal comment.']) - end + expect(cop.messages) + .to eq(['Missing frozen string literal comment.']) + end - it 'accepts freezing a string with interpolation' do - inspect_source(cop, '"#{foo}bar".freeze') + it 'accepts freezing a string with interpolation' do + inspect_source(cop, '"#{foo}bar".freeze') - expect(cop.messages) - .to eq(['Missing frozen string literal comment.']) - end + expect(cop.messages) + .to eq(['Missing frozen string literal comment.']) + end - it 'accepts calling << on a string with interpolation' do - inspect_source(cop, '"#{foo}bar" << "baz"') + it 'accepts calling << on a string with interpolation' do + inspect_source(cop, '"#{foo}bar" << "baz"') - expect(cop.messages) - .to eq(['Missing frozen string literal comment.']) - end + expect(cop.messages) + .to eq(['Missing frozen string literal comment.']) end end end