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

Match rubocop-shopify with style guide recommendations #422

Merged
merged 4 commits into from
Jul 25, 2022

Conversation

Schwad
Copy link
Contributor

@Schwad Schwad commented Jul 21, 2022

Background

There is a discrepancy between shopify-rubocop and the Shopify Ruby Styleguide.

The Ruby Styleguide says:

Use a class << self block over def self. when defining class methods, and group them together within a single block.

( pr for background )

There is an option to enable this in rubocop under Style/ClassMethodsDefinitions for self_class. Our current default does the opposite, with the EnforcedStyle being def_self.

This corrects the default.

For Reviewers

This cop is currently set to Enabled: false. If we care enough about this to have an opinion in the styleguide should it be changed to Enabled: true? The def self vs class << self is a long-running question of preference so it might be nice to give clarity on this.

Signed-off-by: Nick Schwaderer [email protected]

@Schwad Schwad requested a review from a team July 21, 2022 15:43
@Schwad Schwad self-assigned this Jul 21, 2022
@Schwad Schwad requested a review from a team as a code owner July 21, 2022 15:43
Copy link
Contributor

@sambostock sambostock left a comment

Choose a reason for hiding this comment

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

This seems fine, given it doesn't actually enable the cop (which would be a bigger discussion, due to the impact), and just configures the cop so that if enabled it would enforce the style consistent with our recommendation.

Nit about the gem name in the description though 😅

-shopify-rubocop
+rubocop-shopify

@Schwad
Copy link
Contributor Author

Schwad commented Jul 21, 2022

Nit about the gem name in the description though 😅

First-time contributor 🙈 I'll get the name right some day! thanks

@Schwad Schwad changed the title Match shopify-rubocop with style guide recommendations Match rubocop-shopify with style guide recommendations Jul 21, 2022
Copy link
Contributor

@volmer volmer left a comment

Choose a reason for hiding this comment

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

Could you please change the config to Enabled: true?

The rule has been added to the Style Guide for years now, so everyone should be writing code that follows it by now, even if RuboCop wasn't able to catch those offences so far. Besides it wouldn't make sense to tweak a cop's config if it's not enabled by default.

@joeldrapper
Copy link

Is there a way to enforce this style only when there's more than one class method?

@csalvato
Copy link

Is there a way to enforce this style only when there's more than one class method?

For the sake of consistency, I don't think that makes sense, personally.

I actually prefer def self. in general, but if we are going to have an opinion on the style, it should be consistent even with just one class method, imo.

@Schwad
Copy link
Contributor Author

Schwad commented Jul 22, 2022

I actually prefer def self. in general

Me too. My concern is the problem that, for shopify, we have thousands of rubyists and if we do something unusual/different in our style guide and don't back it up with a cop, it's easily missed.

I agree with @volmer - let's enforce it.

The rule has been added to the Style Guide for years now, so everyone should be writing code that follows it by now, even if RuboCop wasn't able to catch those offences so far.

I don't know what I'm allowed to say since this is a public repository. But after a quick search at this org it appears both approaches are equally used. Many thousands of times.

Background

I'm only here because it was flagged up on my PR that I was going against the Shopify Ruby Style Guide 😅 - I was surprised my linter didn't catch the Shopify rule. So here we are.

@Schwad
Copy link
Contributor Author

Schwad commented Jul 22, 2022

test/test_helper.rb:7:3: C: Style/ClassMethodsDefinitions: Use class << self to define a class method.
  def self.warn(message) ...
  ^^^^^^^^^^^^^^^^^^^^^^

Even the style guide is in violation... I'll update that now.

Signed-off-by: Nick Schwaderer <[email protected]>
Copy link
Contributor

@adrianna-chang-shopify adrianna-chang-shopify left a comment

Choose a reason for hiding this comment

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

❤️

@Schwad Schwad merged commit 9655ee8 into main Jul 25, 2022
@Schwad Schwad deleted the match_with_style_guide branch July 25, 2022 16:11
@sambostock sambostock mentioned this pull request Aug 5, 2022
1 task
@shopify-shipit shopify-shipit bot temporarily deployed to rubygems August 5, 2022 19:56 Inactive
@andyw8
Copy link
Contributor

andyw8 commented Oct 13, 2022

@andyw8
Copy link
Contributor

andyw8 commented Oct 13, 2022

(the migration generator from older version of Rails used self.up and self.down - see
https://guides.rubyonrails.org/v2.3/migrations.html)

@rafaelfranca
Copy link
Member

That isn't true since Rails 3.1. I don't think we should care about very old Rails versions.

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.

9 participants