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

Reimplement CallbackConditionalsBinding cop & specs #204

Merged
merged 3 commits into from
Mar 19, 2024

Conversation

sambostock
Copy link
Contributor

This (in three separate commits, for ease of review)

  • Improves the CallbackConditionalsBinding specs

    Specs are consolidated by using expect_correction to test offenses and correction in the same example.

    Doing this reveals that we actually have corrections which do not handle whitespace correctly. For now, the incorrect whitespace has been included in the specs, so they reflect the existing behavior.

    In addition, some examples are added covering edge cases, to capture the existing behavior ahead of changing it.

  • Refactors the CallbackConditionalsBinding cop

    This rewrites the CallbackConditionalsBinding cop, making the following changes:

    • The cop no longer inherits from the deprecated RuboCop::Cop::Cop class.
    • The CALLBACKS constant is renamed to RESTRICT_ON_SEND, which allows RuboCop to use it to limit which methods on_send is called for.
    • Offense detection is simplified by leveraging a declarative node pattern describing offending conditional callbacks.
    • Correction is simplified by better leveraging the AST API.
  • Changes the node to which CallbackConditionalsBinding offenses are added

    This changes which node CallbackConditionalsBinding offenses are attached to from the entire "macro" node to just the keyword argument pair node containing the if:/unless: keyword, and the offending block.

For changelog purposes, these changes could be summarized as

  • Improve handling of edge cases in CallbackConditionalsBinding.
  • Improve CallbackConditionalsBinding offense node accuracy.

Specs are consolidated by using `expect_correction` to test offenses and
correction in the same example.

Doing this reveals that we actually have corrections which do not handle
whitespace correctly. For now, the incorrect whitespace has been
included in the specs, so they reflect the existing behavior.

In addition, some examples are added covering edge cases, to capture the
existing behavior ahead of changing it.
This rewrites the `CallbackConditionalsBinding` cop, making the
following changes:

- The cop no longer inherits from the deprecated `RuboCop::Cop::Cop`
  class.
- The `CALLBACKS` constant is renamed to `RESTRICT_ON_SEND`, which
  allows RuboCop to use it to limit which methods `on_send` is called
  for.
- Offense detection is simplified by leveraging a declarative node
  pattern describing offending conditional callbacks.
- Correction is simplified by better leveraging the AST API.
This changes which node  `CallbackConditionalsBinding` offenses are
attached to from the entire "macro" node to just the keyword argument
pair node containing the `if:/unless:` keyword, and the offending block.
@sambostock sambostock requested a review from a team as a code owner March 19, 2024 03:04
Copy link
Member

@paracycle paracycle left a comment

Choose a reason for hiding this comment

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

This looks great, thank you!

@sambostock sambostock merged commit 5553292 into main Mar 19, 2024
11 checks passed
@sambostock sambostock deleted the refactor-callback-conditionals-binding branch March 19, 2024 19:39
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.

3 participants