Skip to content

Conversation

anthony-robin
Copy link
Contributor

Follows rubocop/rubocop#7122
Closes #49

Inspired from @pocke refactoring in the Naming/RescuedExceptionsVariableName cop, this PR
handle the rescue_from Rails version.

Obstructing points:

  • As discussed in this comment, the PreferredName config should not be duplicated but read from the Naming/RescuedExceptionsVariableName cop.
    How can I read the config from another cop ? Is there already some example of cop working this way ?

  • I haven't been able to make the parser to not take the || of the block arguments which make the offending message being |my_var| instead of my_var.
    What is the way to avoid this ?


Before submitting the PR make sure the following are checked:

  • 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.

Follows rubocop/rubocop#7122
Closes rubocop#49

Inspired from @pocke [refactoring][1] in the `Naming/RescuedExceptionsVariableName` cop, this PR
handle the `rescue_from` Rails version.

Obstructing points:

- As discussed [in this comment][2], the `PreferredName` config should not be duplicated but read from the
`Naming/RescuedExceptionsVariableName` cop.
 How can I read the config from another cop ? Is there already some example of cop working this way ?

- I haven't been able to make the parser to not take the `||` of the block arguments which make the
offending message being `|my_var|` instead of `my_var`.
 What is the way to avoid this ?

[1]: rubocop/rubocop#7122
[2]: rubocop#49 (comment)
@anthony-robin anthony-robin force-pushed the rescued_exception_variable_name branch from 96821f8 to 9ee85ae Compare October 13, 2019 12:47
Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

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

Do you mind rebasing to trigger the build?

Comment on lines +9 to +12
# The `PreferredName` config option takes a `String`. It represents
# the required name of the variable. Its default is `e` that is read
# from `Naming/RescuedExceptionsVariableName` cop in the main Rubocop
# repository.
Copy link
Member

Choose a reason for hiding this comment

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

WDYT of:

The PreferredName config option sets the preferred name for the exception variable, defaults to e.

Comment on lines +59 to +65
def autocorrect(node)
lambda do |corrector|
offending_name = variable_name(node)
preferred_name = preferred_name(offending_name)
corrector.replace(offense_range(node), "|#{preferred_name}|")
end
end
Copy link
Member

Choose a reason for hiding this comment

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

There's a new interface for auto-correction, see https://docs.rubocop.org/rubocop/1.22/development.html#auto-correct


private

def offense_range(resbody)
Copy link
Member

Choose a reason for hiding this comment

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

It feels like this would better be expressed with node pattern. Do you need any guidance with that?
Docs: https://docs.rubocop.org/rubocop-ast/node_pattern.html

@pirj
Copy link
Member

pirj commented Dec 25, 2021

@anthony-robin Ping

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.

Handle RescuedExceptionsVariableName with rescue_from

2 participants