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 rubocop#9328] Honour shareable_constant_value magic comment #9929

Merged
merged 3 commits into from Aug 23, 2021

Conversation

thearjunmdas
Copy link
Contributor

@thearjunmdas thearjunmdas commented Jul 13, 2021

Ruby 3.0 supports shareable_constant_value magic comment. This PR updates Style/MutableConstant to honour shareable_constant_value magic comment.

Fixes #9328

Took PR 9410 as reference. Tried to add @caalberts as co-author in changelog, but changelog spec fails.


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.

@thearjunmdas
Copy link
Contributor Author

thearjunmdas commented Jul 13, 2021

@marcandre @koic - Please take a look when free.

Copy link
Contributor

@marcandre marcandre left a comment

Choose a reason for hiding this comment

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

Tests that could be added

# inline comments:
X = "x" # shareable_constant_value: literal
# shareable_constant_value: literal
X = Set[1, 2]
# blabla shareable_constant_value: literal blabla
  1. variations with hyphen

spec/rubocop/cop/style/mutable_constant_spec.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/mixin/shareable_constant_value.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/style/mutable_constant.rb Outdated Show resolved Hide resolved
@marcandre
Copy link
Contributor

Good PR @thearjunmdas ! 👍
I left a few comments for you

@thearjunmdas
Copy link
Contributor Author

Good PR @thearjunmdas ! 👍
I left a few comments for you

Thanks a lot for the quick review. Will address the comments and get back.

@thearjunmdas
Copy link
Contributor Author

#9929 (review) - Added all the test cases mentioned here.
Addressed all other review comments @marcandre. I am new to RSpec, please check if the way I have refactored is okay.

Please take a look and feel free to add more comments if we could make it better.

lib/rubocop/cop/mixin/shareable_constant_value.rb Outdated Show resolved Hide resolved
spec/rubocop/cop/style/mutable_constant_spec.rb Outdated Show resolved Hide resolved
spec/rubocop/cop/style/mutable_constant_spec.rb Outdated Show resolved Hide resolved
lib/rubocop/cop/style/mutable_constant.rb Outdated Show resolved Hide resolved
@thearjunmdas
Copy link
Contributor Author

thearjunmdas commented Jul 20, 2021

Have added some comments, please review them - so that I can proceed with the changes.

@thearjunmdas
Copy link
Contributor Author

Please do take a look, @marcandre

@thearjunmdas
Copy link
Contributor Author

ping @marcandre

@thearjunmdas
Copy link
Contributor Author

@koic @marcandre - can you folks please take a look? lets try to merge this before it becomes stale?

@@ -0,0 +1 @@
* [#9328](https://github.com/rubocop/rubocop/issues/9328): Recognize shareable_constant_value magic comment. ([@thearjunmdas][])
Copy link
Member

Choose a reason for hiding this comment

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

If you wanted to add a second person here, you could do something like this:

Suggested change
* [#9328](https://github.com/rubocop/rubocop/issues/9328): Recognize shareable_constant_value magic comment. ([@thearjunmdas][])
* [#9328](https://github.com/rubocop/rubocop/issues/9328): Recognize shareable_constant_value magic comment. ([@thearjunmdas][], [@caalberts][])

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dvandersluis thanks for the tip. Worked perfectly.

@bbatsov
Copy link
Collaborator

bbatsov commented Aug 19, 2021

@thearjunmdas Please, also document the support for this magic comment in the cop's description, so people would know what to expect.

@bbatsov
Copy link
Collaborator

bbatsov commented Aug 19, 2021

You'll also have to rebase on top of master.

@marcandre
Copy link
Contributor

Oh, so sorry I missed the pings 😅

@thearjunmdas
Copy link
Contributor Author

Thanks for reviving the PR @marcandre and @bbatsov. I will update on the comments.

P.S. Cleaning the commit history - squashing my commits, rebasing - please ignore intermittent commit history change. Will clean it up.

[Fix rubocop#9328] Resolve review comments

[Fix rubocop#9328] Removing nil check, refactoring spec

[Fix rubocop#9328] Tests for code next to inline comments

[Fix rubocop#9328] Move module to mutable_constant
@thearjunmdas thearjunmdas reopened this Aug 21, 2021
@thearjunmdas
Copy link
Contributor Author

@bbatsov @marcandre rebased on top of master, added documentation. Please let me know, if anything else needs to be done.


it 'raises offense when shareable_constant_value is specified as an inline comment' do
expect_offense(<<~RUBY)
X = [1, 2, 3] # shareable_constant_value: literal
Copy link
Collaborator

Choose a reason for hiding this comment

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

At some point we'll need a lint cop to check for this. :D

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes, sense. Since it's an experimental feature, maybe we need not do it right away.

# NOTE: This special directive helps to create constants
# that hold only immutable objects, or Ractor-shareable
# constants. - ruby docs
#
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bbatsov - Added some examples. Also added an excerpt from ruby docs so that shareable_constant_value doesn't turn out to be an alternative to .freeze

@thearjunmdas
Copy link
Contributor Author

@bbatsov - please take a look.

@bbatsov bbatsov merged commit f145884 into rubocop:master Aug 23, 2021
@bbatsov
Copy link
Collaborator

bbatsov commented Aug 23, 2021

Looks good. Thanks!

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)
4 participants