Skip to content
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

[Fix #9328] Recognize shareable_constant_value magic comment in Style/MutableConstant #9410

Conversation

caalberts
Copy link
Contributor

@caalberts caalberts commented Jan 23, 2021

Ruby 3.0 adds shareable_constant_value magic comment. This PR changes Style/MutableConstant to be aware of shareable_constant_value magic comment.

Fixes #9328.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@@ -103,23 +125,29 @@

include_examples(
'magic comment',
'# -*- encoding: ASCII-8BIT; frozen_string_literal: true -*-',
'# -*- encoding: ASCII-8BIT; frozen_string_literal: true; ' \
'shareable_constant_value: literal -*-',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This had to be split into 2 lines because of line length, but it makes the magic comment less readable. 🤔

@caalberts caalberts force-pushed the recognize-shareable-constant-value-magic-comment branch from 34627fb to 53476f0 Compare January 23, 2021 08:33
@caalberts caalberts changed the title Recognize shareable_constant_value magic comment [#9328] Recognize shareable_constant_value magic comment in Style/MutableConstant Jan 23, 2021
@caalberts caalberts force-pushed the recognize-shareable-constant-value-magic-comment branch from 53476f0 to 6b78bcb Compare January 23, 2021 08:36
@@ -83,6 +84,7 @@ def on_assignment(value)
end

def strict_check(value)
return if shareable_constant_value?
return if immutable_literal?(value)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find it surprising that the tests started failing when i changed immutable_literal?(value) to !mutable_literal?(value).

@caalberts
Copy link
Contributor Author

@marcandre Please take a look?

@caalberts caalberts changed the title [#9328] Recognize shareable_constant_value magic comment in Style/MutableConstant [Fix #9328] Recognize shareable_constant_value magic comment in Style/MutableConstant Jan 23, 2021
@marcandre
Copy link
Contributor

I can't have a thorough look right now, but shareable_constant_value is tricky, because it can change only part of the code. Does this PR handle this?

# shareable_constant_value: literal
X = [1, 2, 3]
# shareable_constant_value: false
Y = [1, 2, 3]

And raise on offense for Y and none for X?

@marcandre
Copy link
Contributor

Ping @caalberts

@caalberts
Copy link
Contributor Author

And raise on offense for Y and none for X?

Yes. X is tested in https://github.com/rubocop-hq/rubocop/pull/9410/files#diff-6e9b3e5cdbc957813be5790f5b8c0850c30c3f1bf0eef7576009023cad7c3f25R160. I'll add a test for Y on ruby 3.0.

@caalberts caalberts force-pushed the recognize-shareable-constant-value-magic-comment branch from 6b78bcb to 40e63ec Compare February 19, 2021 01:00
@@ -166,7 +172,7 @@ def extract_frozen_string_literal
end

def extract_shareable_constant_value
match('shareable[_-]constant[_-]values')
match('shareable[_-]constant[_-]value')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method didn't match shareable_constant_value: *

@bbatsov
Copy link
Collaborator

bbatsov commented Aug 19, 2021

Closing in favour of #9929 that builds on top of this PR.

@bbatsov bbatsov closed this Aug 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Style/MutableConstant should be aware of # shareable_constant_value (Ruby 3.0)
3 participants