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

Add RSpec/ClassCheck cop #1357

Merged
merged 1 commit into from Sep 7, 2022
Merged

Conversation

r7kamura
Copy link
Contributor

@r7kamura r7kamura commented Aug 5, 2022

It would be nice to have a new cop similar to the Style/ClassCheck cop.


Before submitting the PR make sure the following are checked:

  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Updated documentation.
  • Added an entry to the CHANGELOG.md if the new code introduces user-observable changes.
  • The build (bundle exec rake) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).

If you have created a new cop:

  • Added the new cop to config/default.yml.
  • The cop is configured as Enabled: pending in config/default.yml.
  • The cop is configured as Enabled: true in .rubocop.yml.
  • The cop documents examples of good and bad code.
  • The tests assert both that bad code is reported and that good code is not reported.
  • Set VersionAdded in default/config.yml to the next minor version.

If you have modified an existing cop's configuration options:

  • Set VersionChanged in config/default.yml to the next major version.

format(
MSG,
current: node.method_name,
preferred: opposite_method_name(node.method_name)
Copy link
Member

Choose a reason for hiding this comment

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

How about the following?

Suggested change
preferred: opposite_method_name(node.method_name)
preferred: style

@ydah
Copy link
Member

ydah commented Aug 19, 2022

CI jobs that is failing now is being handled by the following PR Please wait a moment:

Copy link
Collaborator

@bquorning bquorning left a comment

Choose a reason for hiding this comment

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

Nice addition to our collection of cops. I just had a couple of comments.

.rubocop.yml Outdated
Comment on lines 82 to 84
RSpec/ClassCheck:
Enabled: true

Copy link
Collaborator

Choose a reason for hiding this comment

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

Please move to line 110 or thereabouts.

Comment on lines 62 to 77
def autocorrect(corrector, node)
corrector.replace(node.loc.selector, style)
end

def format_message(node)
format(
MSG,
current: node.method_name,
preferred: style
)
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think I am directly contradicting @ydah when I say that I don’t like using style in line 63 and 70. The name of the style and the preferred literal code are incidentally identical, but they are two different concepts. While it’s not a big deal here (we probably won’t change the style names soon) it just doesn’t look right to me.

⬆️ This is just my opinion, not necessarily the truth 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Certainly, you have a point.

One of the reasons I chose to use style here is that it is better to calculate preferred method names based on style, rather than based on node. I think we can probably agree on this point. On top of that, if we want to make it explicit that style and method name are not the same concept, we should put a map from style to preferred method name, even if it seems a bit redundant.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree that the map would be the best approach. And having it map from style to preferred code (e.g. { be_a: 'be_a', be_kind_of: 'be_kind_of' }) does seem a bit redundant.

@r7kamura r7kamura force-pushed the feature/class-check branch 2 times, most recently from 601fb69 to cea3ae5 Compare August 25, 2022 14:22
Copy link
Member

@Darhazer Darhazer left a comment

Choose a reason for hiding this comment

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

Since there is no restriction on the receiver, only on the method name, such a cop is prone to false positives, e.g. ResponseMock.be_a(sucessful_response) would be flagged. Even though there is a low chance for people to use such names, it won't hurt to check that the receiver is nil


context 'when enforced style is be_kind_of and be_kind_of is used' do
let(:cop_config) do
{ 'EnforcedStyle' => 'be_kind_of' }
Copy link
Member

Choose a reason for hiding this comment

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

I'd rather organize the spec in two top level contexts (by the enforced style) and then have examples for the usages; thus avoiding the repetition in cop_config

Comment on lines 58 to 60
let(:cop_config) do
{ 'EnforcedStyle' => 'be_kind_of' }
end
Copy link
Member

Choose a reason for hiding this comment

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

You can move this config in the parent context, so you don't need to repeat it in the next ones

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh, I forgot to move them. Thank you! 👍

Copy link
Member

@Darhazer Darhazer left a comment

Choose a reason for hiding this comment

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

Thank you

@Darhazer Darhazer merged commit d272e8e into rubocop:master Sep 7, 2022
@r7kamura r7kamura deleted the feature/class-check branch September 7, 2022 11:17
@@ -106,6 +106,8 @@ RSpec/BeNil:
Enabled: true
RSpec/ChangeByZero:
Enabled: true
RSpec/ClassCheck:
Enabled: true

This comment was marked as resolved.

This comment was marked as resolved.

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.

None yet

5 participants