Skip to content

Commit

Permalink
Merge pull request rubocop#2802 from rrosenblum/frozen_string_literal…
Browse files Browse the repository at this point in the history
…_when_needed

[Fix rubocop#2739] FrozenStringLiteralComment when_needed adds a comment to all files targeted to Ruby 2.3+
  • Loading branch information
bbatsov committed Feb 9, 2016
2 parents 32791e1 + 0c7f91b commit 0c0f76b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 55 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 1 addition & 12 deletions lib/rubocop/cop/style/frozen_string_literal_comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,15 @@ 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)

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)
Expand Down
78 changes: 38 additions & 40 deletions spec/rubocop/cop/style/frozen_string_literal_comment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0c0f76b

Please sign in to comment.