-
Notifications
You must be signed in to change notification settings - Fork 27
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
Refactor ForbidRBIOutsideOfAllowedPaths
& rename to RBIFilePath
#208
Conversation
it "registers an offense if an RBI file is outside AllowedPaths" do | ||
expect_offense(<<~RUBY, filename: "some/dir/file.rbi") | ||
# ... | ||
^{} RBI file path should match one of: rbi/**, sorbet/rbi/** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the {}
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is used to denote an offense attached to a blank line (or line containing only a comment).
The previous implementation attached the offense to the first character in the file which looked like
^ RBI file path should match one of: rbi/**, sorbet/rbi/**
after switching to expect_offense
but before switching to add_global_offense
(see commit history).
After switching to add_global_offense
, expect_offense
expects ^{}
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Incidentally, I just noticed rubocop/rubocop#12802 made the same change to some cops in RuboCop proper, and has the same changes to the tests.
This refactors the specs to be more idiomatic, passing the filename to the `expect_offense` and `expect_no_offenses` helpers, instead of stubbing the filename. It also decreases the use of pointless contexts, and omits redundant `Enabled: true` config.
This refactors the cop to inherit from `RuboCop::Cop::Base` instead of the deprecated `RuboCop::Cop::Cop` class.
Idiomatically, cops do not validate their config. Instead, they provide good defaults, and trust the host application to follow their example. The existing approach of adding an offense on every file is not a particularly good experience either, so we can just keep it simple and remove it.
The offense is with regards to the file name, so a global offense is more appropriate.
This name is more idiomatic and focussed on what is allowed (using the allowed paths) rather than forbidding other paths.
19de5bd
to
95e349d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me but I'd like if @KaanOzkan or @paracycle can confirm.
It looks like I'm going to mark this as a draft until I get that sorted. It's weird that the tests are passing, so I'd like to figure out why that is, so I can report the bug in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes look good to me, but it would be great to understand what's happening with the tests.
@@ -73,7 +73,7 @@ Sorbet/ForbidExtendTSigHelpersInShims: | |||
Include: | |||
- "**/*.rbi" | |||
|
|||
Sorbet/ForbidRBIOutsideOfAllowedPaths: | |||
Sorbet/RBIFilePath: | |||
Description: 'Forbids RBI files outside of the allowed paths' | |||
Enabled: true | |||
VersionAdded: 0.6.1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might need a VersionChanged:
entry here.
Okay, so it took some digging, but I think I figured out what was going on with the tests. Nothing complains about receiving a From there, when RuboCop itself is "immune" to this bug because I'll look into enabling |
Ahh, it looks like the reason that cop didn't trigger is because we're on an outdated RuboCop version, and it was only added a month ago. I'll look at adding Dependabot to this repo, which should prevent that going forward. |
This PR has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This makes the following changes:
Refactors
ForbidRBIOutsideOfAllowedPaths
specsThis refactors the specs to be more idiomatic, passing the filename to the
expect_offense
andexpect_no_offenses
helpers, instead of stubbing the filename. It also decreases the use of pointless contexts, and omits redundantEnabled: true
config.Refactor
ForbidRBIOutsideOfAllowedPaths
This refactors the cop to inherit from
RuboCop::Cop::Base
instead of the deprecatedRuboCop::Cop::Cop
class.Assumes
ForbidRBIOutsideOfAllowedPaths
config is validIdiomatically, cops do not validate their config. Instead, they provide good defaults, and trust the host application to follow their example.
The existing approach of adding an offense on every file is not a particularly good experience either, so we can just keep it simple and remove it.
Adds global offenses in
ForbidRBIOutsideOfAllowedPaths
The offense is with regards to the file name, so a global offense is more appropriate.
Renames
ForbidRBIOutsideOfAllowedPaths
toRBIFilePath
This name is more idiomatic and focussed on what is allowed (using the allowed paths) rather than forbidding other paths.
This is technically a breaking change, but if it applies to the host repo, RuboCop will fail to start up with an error explaining exactly what needs to be changed in the config, so it would not break anyone's production environments or corrupt any code.