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

[#499] Create a Rubocop cop for ordering instructions in class (e.g. Models) #509

Merged
merged 5 commits into from
Apr 29, 2024

Conversation

Goose97
Copy link
Contributor

@Goose97 Goose97 commented Mar 29, 2024

What happened 👀

Enforce class template with custom Rubocop rule

https://nimblehq.co/compass/development/code-conventions/ruby/#convention-40

Insight 📝

The cop is divided into two phases:

  1. Expression categorize phase: every expressions in the class body will classify into one of these categories. If none matches, it'll be :unknown
  2. Validate the order of the class body expressions according to the template

Proof Of Work 📹

Project bootstrap

Untitled.mp4

Linter example

Screen-Recording-2024-03-29-at-1.35.03-PM.mp4

@Goose97 Goose97 force-pushed the feature/gh499-custom-rubocop-class-template branch 9 times, most recently from be94524 to 64e481d Compare March 29, 2024 10:46
@Goose97 Goose97 marked this pull request as ready for review March 29, 2024 10:55
@Goose97 Goose97 requested a review from a user March 29, 2024 10:55
@Goose97 Goose97 self-assigned this Apr 3, 2024
@malparty
Copy link
Member

malparty commented Apr 4, 2024

Sorry I was a bit busy. I'll try to review next Monday (I blocked some time in my calendar for it 🙇 )


add_offense(
second[:expression],
message: error_message(second[:category], expressions.pluck(:category))
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
message: error_message(second[:category], expressions.pluck(:category))
message: error_message(second[:category], expressions.map { |expression| expression[:category] })

I tried in the CX project, but it seems that the Rails helpers could not be loaded. More over, in case we want to reuse this cop in a non Rails project, it can be nice to use pure Ruby only :)

image

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 absolutely 👍 . Fixed in 8b5fce5

I didn't realize that :pluck is one of the niceties from Rails 🙈

Copy link
Member

@malparty malparty left a comment

Choose a reason for hiding this comment

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

So it's working well on my side 🥳

Just one question: do you think we can improve the message so that it points to the line of code (e.g. "...should come before audited associated_with: :tenant") 💭

Note that this suggestion can be done in a future PR, it's not a blocker 👍

Message Code
image image

@Goose97 Goose97 force-pushed the feature/gh499-custom-rubocop-class-template branch from bf2ae9f to 8b5fce5 Compare April 9, 2024 08:39
@Goose97
Copy link
Contributor Author

Goose97 commented Apr 9, 2024

Just one question: do you think we can improve the message so that it points to the line of code (e.g. "...should come before audited associated_with: :tenant") 💭

Great suggestion. Added in f9fe5e6

Your example will now display this error instead:

Screen Shot 2024-04-09 at 17 43 38

@Goose97 Goose97 force-pushed the feature/gh499-custom-rubocop-class-template branch from d8385eb to da7c883 Compare April 9, 2024 10:48
@Goose97 Goose97 force-pushed the feature/gh499-custom-rubocop-class-template branch from da7c883 to 54e41c8 Compare April 9, 2024 10:50
@Goose97 Goose97 requested a review from malparty April 9, 2024 10:50
@Goose97 Goose97 force-pushed the feature/gh499-custom-rubocop-class-template branch from 54e41c8 to f9fe5e6 Compare April 9, 2024 10:52
Copy link
Member

@malparty malparty left a comment

Choose a reason for hiding this comment

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

Approved ahead, but we need to resolve the tests 🙇

@malparty malparty added this to the 5.14.0 milestone Apr 11, 2024
@Goose97 Goose97 force-pushed the feature/gh499-custom-rubocop-class-template branch from f9fe5e6 to 0eece68 Compare April 11, 2024 07:20
@malparty malparty modified the milestones: 5.14.0, 5.15.0 Apr 11, 2024
@Goose97 Goose97 force-pushed the feature/gh499-custom-rubocop-class-template branch from 18fde8b to 6b91a81 Compare April 11, 2024 08:45
@malparty malparty added this pull request to the merge queue Apr 29, 2024
Merged via the queue into develop with commit 0102560 Apr 29, 2024
5 checks passed
@malparty malparty deleted the feature/gh499-custom-rubocop-class-template branch April 29, 2024 02:17
@malparty malparty mentioned this pull request May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create a Rubocop cop for ordering instructions in class (e.g. Models)
3 participants