Class: RuboCop::Cop::Style::RaiseArgs
| Relationships & Source Files | |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Class Chain:
self,
::RuboCop::Cop::AutoCorrector,
::RuboCop::Cop::Base,
::RuboCop::ExcludeLimit,
NodePattern::Macros,
RuboCop::AST::Sexp
|
|
|
Instance Chain:
self,
::RuboCop::Cop::ConfigurableEnforcedStyle,
::RuboCop::Cop::Base,
::RuboCop::Cop::AutocorrectLogic,
::RuboCop::Cop::IgnoredNode,
::RuboCop::Util,
RuboCop::AST::Sexp
|
|
| Inherits: |
RuboCop::Cop::Base
|
| Defined in: | lib/rubocop/cop/style/raise_args.rb |
Overview
Checks the args passed to fail and raise.
Exploded style (default) enforces passing the exception class and message arguments separately, rather than constructing an instance of the error.
Compact style enforces constructing an error instance.
Both styles allow passing just a message, or an error instance when there is more than one argument.
The exploded style has an AllowedCompactTypes configuration
option that takes an Array of exception name Strings.
Constant Summary
-
ACCEPTABLE_ARG_TYPES =
# File 'lib/rubocop/cop/style/raise_args.rb', line 53%i[ hash forwarded_restarg splat forwarded_kwrestarg forwarded_args ].freeze
-
COMPACT_MSG =
# File 'lib/rubocop/cop/style/raise_args.rb', line 52'Provide an exception object as an argument to `%<method>s`.' -
EXPLODED_MSG =
# File 'lib/rubocop/cop/style/raise_args.rb', line 51'Provide an exception class and message as arguments to `%<method>s`.' -
RESTRICT_ON_SEND =
# File 'lib/rubocop/cop/style/raise_args.rb', line 57%i[raise fail].freeze
::RuboCop::Cop::Base - Inherited
DEPARTMENT_SEVERITIES, EMPTY_OFFENSES, RESTRICT_ON_SEND
::RuboCop::Cop::ConfigurableEnforcedStyle - Included
Class Attribute Summary
::RuboCop::Cop::AutoCorrector - Extended
::RuboCop::Cop::Base - Inherited
| .gem_requirements, .lint?, | |
| .support_autocorrect? | Returns if class supports autocorrect. |
| .support_multiple_source? | Override if your cop should be called repeatedly for multiple investigations Between calls to |
Class Method Summary
::RuboCop::Cop::Base - Inherited
| .autocorrect_incompatible_with | List of cops that should not try to autocorrect at the same time as this cop. |
| .badge | Naming. |
| .callbacks_needed, .cop_name, .department, | |
| .documentation_url | Returns a url to view this cops documentation online. |
| .exclude_from_registry | Call for abstract Cop classes. |
| .inherited, | |
| .joining_forces | Override and return the Force class(es) you need to join. |
| .match? | Returns true if the cop name or the cop namespace matches any of the given names. |
| .new, | |
| .requires_gem | Register a version requirement for the given gem name. |
| .restrict_on_send | Reserved for Commissioner. |
::RuboCop::ExcludeLimit - Extended
| exclude_limit | Sets up a configuration option to have an exclude limit tracked. |
| transform | |
Instance Attribute Summary
::RuboCop::Cop::ConfigurableEnforcedStyle - Included
::RuboCop::Cop::Base - Inherited
| #active_support_extensions_enabled?, #always_autocorrect?, #config, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #disabled_autocorrect?, | |
| #preview? | Whether the user opted in to unstable behavior, with |
| #processed_source, #project_index, #string_literals_frozen_by_default?, #target_satisfies_all_gem_version_requirements? | |
::RuboCop::Cop::AutocorrectLogic - Included
| #autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?, | |
| #skipped_unsafe_correction_with_disable_uncorrectable? | Whether a correction exists but is skipped because it is unsafe in a safe autocorrect run, while |
| #contextual_autocorrect_held_back? | When LSP is enabled or the |
Instance Method Summary
- #on_send(node)
- #acceptable_exploded_args?(args) ⇒ Boolean private
- #allowed_non_exploded_type?(arg) ⇒ Boolean private
- #check_compact(node) private
- #check_exploded(node) private
- #correct_heredoc_compact_to_exploded(corrector, node) private
- #correct_heredoc_exploded_to_compact(corrector, node) private
- #correct_raise(node) private
- #correct_to_compact(corrector, node) private
- #correct_to_exploded(corrector, node) private
- #correction_compact_to_exploded(node) private
- #correction_exploded_to_compact(node) private
- #exception_class_node(exception_node) private
- #heredoc?(node) ⇒ Boolean private
- #heredoc_message?(node) ⇒ Boolean private
- #heredoc_new_argument?(node) ⇒ Boolean private
- #inside_corrected_raise?(node) ⇒ Boolean private
- #parenthesize?(node) ⇒ Boolean private
- #range_between(left_node, right_node) private
- #requires_parens?(parent) ⇒ Boolean private
- #use_new_method?(first_arg) ⇒ Boolean private
::RuboCop::Cop::ConfigurableEnforcedStyle - Included
::RuboCop::Cop::Base - Inherited
| #add_global_offense | Adds an offense that has no particular location. |
| #add_offense | Adds an offense on the specified range (or node with an expression) Unless that offense is disabled for this range, a corrector will be yielded to provide the cop the opportunity to autocorrect the offense. |
| #begin_investigation | Called before any investigation. |
| #callbacks_needed | — grouped under the Commissioner heading above. |
| #cop_config | Configuration Helpers. |
| #cop_name, #excluded_file?, | |
| #external_dependency_checksum | This method should be overridden when a cop’s behavior depends on state that lives outside of these locations: |
| #inspect, | |
| #message | Gets called if no message is specified when calling |
| #name | Alias for Base#cop_name. |
| #offenses, | |
| #on_investigation_end | Called after all on_… |
| #on_new_investigation | Called before all on_… |
| #on_other_file | Called instead of all on_… |
| #parse | There should be very limited reasons for a Cop to do it’s own parsing. |
| #parser_engine, | |
| #ready | Called between investigations. |
| #relevant_file?, | |
| #target_gem_version | Returns the version of a gem in the target, by default its locked version (i.e. from Gemfile.lock or gems.locked). |
| #target_rails_version, #target_ruby_version, #annotate, #apply_correction, #attempt_correction, | |
| #callback_argument | Reserved for Cop::Cop. |
| #complete_investigation | Called to complete an investigation. |
| #correct, #covering_disabled_range, #current_corrector, | |
| #current_offense_locations | Reserved for Commissioner: |
| #current_offenses, #currently_disabled_lines, #custom_severity, #default_severity, #disable_uncorrectable, #enabled_line?, | |
| #enabled_lines? | A multi-line offense is suppressed by a directive on any line of its range, not only its first line, matching the intuition that the directive is attached to the offending code. |
| #file_name_matches_any?, #find_message, #find_severity, #matches_absolute_include_pattern?, #new_offense, #range_for_original, #range_from_node_or_range, | |
| #reset_investigation | Actually private methods. |
| #suppression_reason | The |
| #use_corrector | |
::RuboCop::Cop::AutocorrectLogic - Included
::RuboCop::Cop::IgnoredNode - Included
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Method Details
#acceptable_exploded_args?(args) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/raise_args.rb', line 194
def acceptable_exploded_args?(args) # Allow code like `raise Ex.new(arg1, arg2)`. return true if args.size > 1 # Disallow zero arguments. return false if args.empty? arg = args.first # Allow nodes that may forward more than one argument ACCEPTABLE_ARG_TYPES.include?(arg.type) end
#allowed_non_exploded_type?(arg) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/raise_args.rb', line 207
def allowed_non_exploded_type?(arg) type = arg.receiver.const_name Array(cop_config['AllowedCompactTypes']).include?(type) end
#check_compact(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/raise_args.rb', line 102
def check_compact(node) if node.arguments.size > 1 exception = node.first_argument return if exception.send_type? && exception.first_argument&.hash_type? return if use_new_method?(exception) && exception.arguments? add_offense(node, message: format(COMPACT_MSG, method: node.method_name)) do |corrector| correct_raise(node) { correct_to_compact(corrector, node) } end else correct_style_detected end end
#check_exploded(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/raise_args.rb', line 143
def check_exploded(node) return correct_style_detected unless node.arguments.one? first_arg = node.first_argument return if !use_new_method?(first_arg) || acceptable_exploded_args?(first_arg.arguments) return if allowed_non_exploded_type?(first_arg) add_offense(node, message: format(EXPLODED_MSG, method: node.method_name)) do |corrector| correct_raise(node) { correct_to_exploded(corrector, node) } end end
#correct_heredoc_compact_to_exploded(corrector, node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/raise_args.rb', line 172
def correct_heredoc_compact_to_exploded(corrector, node) new_call = node.first_argument corrector.replace(range_between(new_call.receiver, new_call.first_argument), ', ') corrector.remove(new_call.loc.end) if new_call.loc.end end
#correct_heredoc_exploded_to_compact(corrector, node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/raise_args.rb', line 132
def correct_heredoc_exploded_to_compact(corrector, node) exception_node, = *node.arguments exception_class = exception_class_node(exception_node) corrector.replace(range_between(exception_class, ), '.new(') corrector.insert_after(, ')') end
#correct_raise(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/raise_args.rb', line 178
def correct_raise(node) opposite_style_detected return if inside_corrected_raise?(node) yield ignore_node(node) end
#correct_to_compact(corrector, node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/raise_args.rb', line 116
def correct_to_compact(corrector, node) if (node) correct_heredoc_exploded_to_compact(corrector, node) else corrector.replace(node, correction_exploded_to_compact(node)) end end
#correct_to_exploded(corrector, node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/raise_args.rb', line 157
def correct_to_exploded(corrector, node) if heredoc_new_argument?(node) correct_heredoc_compact_to_exploded(corrector, node) else corrector.replace(node, correction_compact_to_exploded(node)) end end
#correction_compact_to_exploded(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/raise_args.rb', line 72
def correction_compact_to_exploded(node) exception_node, _new, = *node.first_argument arguments = [exception_node, ].compact.map(&:source).join(', ') if parenthesize?(node) "#{node.method_name}(#{arguments})" else "#{node.method_name} #{arguments}" end end
#correction_exploded_to_compact(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/raise_args.rb', line 84
def correction_exploded_to_compact(node) exception_node, * = *node.arguments return if .size > 1 argument = .first.source exception_class = exception_class_node(exception_node).source if parenthesize?(node) "#{node.method_name}(#{exception_class}.new(#{argument}))" else "#{node.method_name} #{exception_class}.new(#{argument})" end end
#exception_class_node(exception_node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/raise_args.rb', line 98
def exception_class_node(exception_node) use_new_method?(exception_node) ? exception_node.receiver : exception_node end
#heredoc?(node) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/raise_args.rb', line 128
def heredoc?(node) node.each_node(:any_str).any?(&:heredoc?) end
#heredoc_message?(node) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/raise_args.rb', line 124
def (node) node.parenthesized? && node.arguments.size == 2 && heredoc?(node.last_argument) end
#heredoc_new_argument?(node) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/raise_args.rb', line 165
def heredoc_new_argument?(node) new_call = node.first_argument return false unless new_call.arguments.one? && heredoc?(new_call.first_argument) node.parenthesized? || !parenthesize?(node) end
#inside_corrected_raise?(node) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/raise_args.rb', line 186
def inside_corrected_raise?(node) node.each_ancestor(:send).any? { |ancestor| ignored_node?(ancestor) } end
#on_send(node)
[ GitHub ]# File 'lib/rubocop/cop/style/raise_args.rb', line 59
def on_send(node) return unless node.command?(:raise) || node.command?(:fail) case style when :compact check_compact(node) when :exploded check_exploded(node) end end
#parenthesize?(node) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/raise_args.rb', line 213
def parenthesize?(node) return true if node.parenthesized? node.parent && requires_parens?(node.parent) end
#range_between(left_node, right_node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/raise_args.rb', line 139
def range_between(left_node, right_node) left_node.source_range.end.join(right_node.source_range.begin) end
#requires_parens?(parent) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/raise_args.rb', line 219
def requires_parens?(parent) return true if parent.operator_keyword? return true if parent.type?(:call, :super, :yield, :array, :pair) return true if parent.any_def_type? && parent.endless? parent.if_type? && parent.ternary? end
#use_new_method?(first_arg) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/raise_args.rb', line 190
def use_new_method?(first_arg) first_arg.send_type? && first_arg.receiver && first_arg.method?(:new) end