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 allowed method for Lint/Debugger #8929

Merged
merged 3 commits into from Nov 6, 2020

Conversation

mfbmina
Copy link
Contributor

@mfbmina mfbmina commented Oct 23, 2020

This PR fixes #8927.

It allows the user to add AllowedMethods for Lint/Debugger cop.

  • 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.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

@bbatsov
Copy link
Collaborator

bbatsov commented Oct 23, 2020

You should also update the docs and the default configuration. And remove the hardcoded methods.

@mfbmina mfbmina force-pushed the allowed_methods_on_lint_debugger branch from 5837b74 to 5ac9f11 Compare October 23, 2020 19:59
@mfbmina
Copy link
Contributor Author

mfbmina commented Oct 23, 2020

@bbatsov please check again!

@@ -1415,6 +1415,18 @@ Lint/Debugger:
Enabled: true
VersionAdded: '0.14'
VersionChanged: '0.49'
AllowedMethods:
Copy link
Contributor

Choose a reason for hiding this comment

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

this seems the opposite of Allowed, should this be DisallowedMethods? ReportedMethods? DebuggerMethods? just Methods?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yep, you're toally right. I like the name DebuggerMethods best, although I'm also fine with "ForbiddenMethods".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I also felt like this when implementing... shall we go with DebuggerMethods then?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yep.

@bbatsov
Copy link
Collaborator

bbatsov commented Oct 25, 2020

please check again!

Yout still have replace the hardcoded node patterns with something that's generated dynamically based on the cop's configuration.

@mfbmina
Copy link
Contributor Author

mfbmina commented Oct 25, 2020

Yout still have replace the hardcoded node patterns with something that's generated dynamically based on the cop's configuration.

My solution for that was adding a ForbiddenReceivers options which will help to specify which receivers are not allowed, like binding or Kernel.

The only edge case that I couldn't cover and had to add a special check when was Kernel.save_and_open_page which should be allowed even tho Kernel is not an allowed receiver and save_and_open_page is a debugger method.

@mfbmina mfbmina requested a review from bbatsov October 26, 2020 13:32
@mfbmina
Copy link
Contributor Author

mfbmina commented Oct 27, 2020

@bbatsov @robotdana what you guys think?

config/default.yml Outdated Show resolved Hide resolved
@bbatsov
Copy link
Collaborator

bbatsov commented Nov 4, 2020

I think we're pretty close to merging this. I left a couple of small remarks. You'll also need to update the cop description to mention the configuration options and the rebase on top of master.

@mfbmina mfbmina force-pushed the allowed_methods_on_lint_debugger branch from a039407 to 12bd068 Compare November 5, 2020 18:42
@mfbmina mfbmina requested a review from bbatsov November 5, 2020 18:43
lib/rubocop.rb Outdated Show resolved Hide resolved
!!node.receiver
end

def special_rule?(node)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't get what's the purpose of this special_rule? method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is an edge case that I couldn't find a better fix. Maybe you can help me:

Even tho Kernel is a receiver and save_and_open_page is a debugger method together they should not be an offense.

Here is the test case for it: https://github.com/rubocop-hq/rubocop/blob/12bd06882eaca12bd33c77f31e8d976bc3ad0bed/spec/rubocop/cop/lint/debugger_spec.rb#L164-L166

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 just drop this test case. For such a small thing people should add a local exclusion rule. It just doesn't fit with having with our goal to have a configurable cop.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool! I will drop it! It was introduced here. 83902a6

@mfbmina mfbmina force-pushed the allowed_methods_on_lint_debugger branch from 12bd068 to e96be77 Compare November 6, 2020 07:56
@mfbmina mfbmina force-pushed the allowed_methods_on_lint_debugger branch from 7e99b5a to f548669 Compare November 6, 2020 11:33
@mfbmina mfbmina requested a review from bbatsov November 6, 2020 11:34
@bbatsov bbatsov merged commit cf96730 into rubocop:master Nov 6, 2020
@mfbmina mfbmina deleted the allowed_methods_on_lint_debugger branch November 6, 2020 12:30
dvandersluis added a commit to dvandersluis/rubocop that referenced this pull request Feb 10, 2021
…vers for debug methods lead to offenses.

Prior to rubocop#8929, `Lint/Debugger` looked for a specific receiver/method pair, but that PR changed it so that any of the `DebuggerReceivers` would apply, *or also* no receiver. This caused regressions (rubocop#7636 and rubocop#9500, for instance) because method names specified as debugger by this cop would now register offenses when called with no receiver.

In order to fix it, I changed the configuration for `Lint/Debugger` a bit, but in a way that should not cause any major incompatibility. The allow list is now specified including its receiver so that the receiver can be checked. Existing configurations that just pass in a method name will be treated as expecting no receiver (there's a slight incompatibility there because previously `foo` would be matched by `Kernel.foo` but I think this is ok (and is resolvable by adding `Kernel.foo` to the configuration).

I've also grouped the debugger methods by source (it's often confusing to me where each method comes from), which also allows an end-user to disable a pre-defined group of methods by specifying `Byebug: ~` for example.

Finally I've deprecated the `DebuggerReceivers` configuration as it is no longer useful.
bbatsov pushed a commit that referenced this pull request Feb 12, 2021
…r debug methods lead to offenses.

Prior to #8929, `Lint/Debugger` looked for a specific receiver/method pair, but that PR changed it so that any of the `DebuggerReceivers` would apply, *or also* no receiver. This caused regressions (#7636 and #9500, for instance) because method names specified as debugger by this cop would now register offenses when called with no receiver.

In order to fix it, I changed the configuration for `Lint/Debugger` a bit, but in a way that should not cause any major incompatibility. The allow list is now specified including its receiver so that the receiver can be checked. Existing configurations that just pass in a method name will be treated as expecting no receiver (there's a slight incompatibility there because previously `foo` would be matched by `Kernel.foo` but I think this is ok (and is resolvable by adding `Kernel.foo` to the configuration).

I've also grouped the debugger methods by source (it's often confusing to me where each method comes from), which also allows an end-user to disable a pre-defined group of methods by specifying `Byebug: ~` for example.

Finally I've deprecated the `DebuggerReceivers` configuration as it is no longer useful.
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.

Add AllowedMethods option to Lint/Debugger cop
3 participants