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
Address frozen_string_literal
no longer applying to interpolated strings in Ruby 3.0 or higher
#10006
Merged
bbatsov
merged 3 commits into
rubocop:master
from
splattael:ruby3-interpolated-string-literals
Aug 23, 2021
Merged
Address frozen_string_literal
no longer applying to interpolated strings in Ruby 3.0 or higher
#10006
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#10006](https://github.com/rubocop/rubocop/pull/10006): Interpolated string literals are no longer frozen since Ruby 3.0. ([@splattael][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,9 @@ module Style | |
# | ||
# NOTE: Regexp and Range literals are frozen objects since Ruby 3.0. | ||
# | ||
# NOTE: From Ruby 3.0, this cop allows explicit freezing of interpolated | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Initially, I wanted to link https://rubyreferences.github.io/rubychanges/3.0.html#interpolated-string-literals-are-no-longer-frozen-when--frozen-string-literal-true-is-used but it seems we are not linking to external resources in inline docs very often. |
||
# string literals when `# frozen-string-literal: true` is used. | ||
# | ||
# @example EnforcedStyle: literals (default) | ||
# # bad | ||
# CONST = [1, 2, 3] | ||
|
@@ -151,8 +154,8 @@ def check(value) | |
range_enclosed_in_parentheses = range_enclosed_in_parentheses?(value) | ||
return unless mutable_literal?(value) || | ||
target_ruby_version <= 2.7 && range_enclosed_in_parentheses | ||
return if FROZEN_STRING_LITERAL_TYPES.include?(value.type) && | ||
frozen_string_literals_enabled? | ||
|
||
return if frozen_string_literal?(value) | ||
return if shareable_constant_value?(value) | ||
|
||
add_offense(value) { |corrector| autocorrect(corrector, value) } | ||
|
@@ -183,10 +186,6 @@ def immutable_literal?(node) | |
frozen_regexp_or_range_literals?(node) || node.immutable_literal? | ||
end | ||
|
||
def frozen_string_literal?(node) | ||
FROZEN_STRING_LITERAL_TYPES.include?(node.type) && frozen_string_literals_enabled? | ||
end | ||
|
||
def shareable_constant_value?(node) | ||
return false if target_ruby_version < 3.0 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems we are using different style to highlight notes. I went with
NOTE:
but would be happy to use "From Ruby 3.0, ..." pattern as seen above.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NOTE:
is better.