-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new Naming/RescuedExceptionsVariableName cop
Use a consistent naming of the rescued exceptions variable. Prevent naming rescued `Exception` variable other than the preferred configured one. The default naming configuration is "ex".
- Loading branch information
Showing
19 changed files
with
298 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Naming | ||
# This cop makes sure that rescued exceptions variables are named as | ||
# expected. | ||
# | ||
# The `PreferredName` config option takes a `String`. It represents | ||
# the required name of the variable. Its default is `e`. | ||
# | ||
# @example PreferredName: e (default) | ||
# # bad | ||
# begin | ||
# # do something | ||
# rescue MyException => exc | ||
# # do something | ||
# end | ||
# | ||
# # good | ||
# begin | ||
# # do something | ||
# rescue MyException => e | ||
# # do something | ||
# end | ||
# | ||
# @example PreferredName: exception | ||
# # bad | ||
# begin | ||
# # do something | ||
# rescue MyException => e | ||
# # do something | ||
# end | ||
# | ||
# # good | ||
# begin | ||
# # do something | ||
# rescue MyException => ex | ||
# # do something | ||
# end | ||
# | ||
class RescuedExceptionsVariableName < Cop | ||
MSG = 'Use `%<preferred>s` instead of `%<bad>s`.'.freeze | ||
|
||
def on_resbody(node) | ||
exception_type, @exception_name = *node | ||
return unless exception_type || @exception_name | ||
|
||
@exception_name ||= exception_type.children.first | ||
return if @exception_name.const_type? || | ||
variable_name == preferred_name | ||
|
||
add_offense(node, location: location) | ||
end | ||
|
||
private | ||
|
||
def preferred_name | ||
@preferred_name ||= cop_config.fetch('PreferredName', 'e') | ||
end | ||
|
||
def variable_name | ||
@variable_name ||= location.source | ||
end | ||
|
||
def location | ||
@location ||= @exception_name.loc.expression | ||
end | ||
|
||
def message(_node = nil) | ||
format(MSG, preferred: preferred_name, bad: variable_name) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.