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 #11696] Add new Style/DataInheritance cop #11728

Merged
merged 1 commit into from
Apr 3, 2023

Conversation

ktopolski
Copy link

Fixes #11696.
This PR adds new cop: Style/DataInheritance. It's based on the Style/StructInheritance cop.


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.

module RuboCop
module Cop
module Style
# Checks for inheritance from Data.define.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd also mention here what's the problem we're trying to solve (avoid the creation of the anonymous parent class).

Copy link
Author

Choose a reason for hiding this comment

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

Added couple words, thanks for the comment! I was considering adding outputs of .ancestors and whatnot, but was afraid it will be too verbose then. WDYT about current form?

@bbatsov
Copy link
Collaborator

bbatsov commented Mar 23, 2023

It would also be nice to add a matching guideline in the Ruby style guide and mention it in the cop's metadata.

@bbatsov
Copy link
Collaborator

bbatsov commented Mar 23, 2023

@koic
Copy link
Member

koic commented Mar 23, 2023

Data.define was added in Ruby 3.2. Using minimum_target_ruby_version 3.2 is more robust.

@ktopolski ktopolski force-pushed the add_new_data_inheritance_cop branch from a295679 to 022a32e Compare March 26, 2023 15:26
ktopolski pushed a commit to ktopolski/ruby-style-guide that referenced this pull request Mar 26, 2023
See also:
* [PR adding new cop for this rule](rubocop/rubocop#11728)
* [Original issue](rubocop/rubocop#11696)
@ktopolski ktopolski force-pushed the add_new_data_inheritance_cop branch 2 times, most recently from 5175dd7 to da921aa Compare March 26, 2023 18:50
@ktopolski
Copy link
Author

Thanks for the comments @koic and @bbatsov ! I've added the minimum_target_ruby_version 3.2 (nice catch) and opened PR to style guide. Looking forward to the re-review :)

ktopolski pushed a commit to ktopolski/ruby-style-guide that referenced this pull request Mar 26, 2023
See also:
* [PR adding new cop for this rule](rubocop/rubocop#11728)
* [Original issue](rubocop/rubocop#11696)

RSpec.describe RuboCop::Cop::Style::DataInheritance, :config do
context 'Ruby >= 3.2', :ruby32 do
it 'registers an offense when extending instance of Data' do
Copy link
Member

Choose a reason for hiding this comment

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

Can you replace the entire test description with Data.define?

Suggested change
it 'registers an offense when extending instance of Data' do
it 'registers an offense when extending instance of `Data.define`' do

end
RUBY
end
end
Copy link
Member

Choose a reason for hiding this comment

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

Can you add a test for context 'Ruby <= 3.1', :ruby31 do that does not register an offense?

end

# @!method data_constructor?(node)
def_node_matcher :data_constructor?, <<~PATTERN
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def_node_matcher :data_constructor?, <<~PATTERN
def_node_matcher :data_define?, <<~PATTERN

module RuboCop
module Cop
module Style
# Checks for inheritance from Data.define to avoid creating the anonymous parent class.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# Checks for inheritance from Data.define to avoid creating the anonymous parent class.
# Checks for inheritance from `Data.define` to avoid creating the anonymous parent class.

@bbatsov
Copy link
Collaborator

bbatsov commented Mar 31, 2023

@ktopolski It'd be nice to wrap this up soon, so it can be included in the next RuboCop release.

@ktopolski ktopolski force-pushed the add_new_data_inheritance_cop branch from eb41502 to bc42301 Compare April 2, 2023 09:22
@ktopolski ktopolski requested review from koic and bbatsov April 2, 2023 09:23
@ktopolski
Copy link
Author

Thanks @koic for bunch of useful suggestions, fixed them all 👍

@bbatsov - sorry, didn't have much time on my hands during the week 😅 hopefully I'm still in time for the next release!

@koic koic merged commit bf33f2f into rubocop:master Apr 3, 2023
12 checks passed
@koic
Copy link
Member

koic commented Apr 3, 2023

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.

DataInheritance cop similar to StructInheritance
3 participants