Class: RuboCop::Cop::Style::MultipleComparison
| 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::Base,
::RuboCop::Cop::AutocorrectLogic,
::RuboCop::Cop::IgnoredNode,
::RuboCop::Util,
RuboCop::AST::Sexp
|
|
| Inherits: |
RuboCop::Cop::Base
|
| Defined in: | lib/rubocop/cop/style/multiple_comparison.rb |
Overview
Checks against comparing a variable with multiple items, where
Array#include?, Set#include? or a case could be used instead
to avoid code repetition.
It accepts comparisons of multiple method calls to avoid unnecessary method calls
by default. It can be configured by AllowMethodComparison option.
Constant Summary
-
MSG =
# File 'lib/rubocop/cop/style/multiple_comparison.rb', line 55'Avoid comparing a variable with multiple items ' \ 'in a conditional, use `Array#include?` instead.'
::RuboCop::Cop::Base - Inherited
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
- #allow_method_comparison? ⇒ Boolean readonly private
::RuboCop::Cop::Base - Inherited
::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 |
Instance Method Summary
- #on_or(node)
- #simple_comparison_lhs(node)
- #simple_comparison_rhs(node)
- #simple_double_comparison?(node)
- #collect_comparison(node, comparison, variables, values, skipped) private
- #comparison?(node) ⇒ Boolean private
- #comparisons_threshold private
- #find_offending_var(node, variables = Set.new, values = [], skipped = []) private
- #nested_comparison?(node) ⇒ Boolean private
- #offense_range(values) private
- #preferred_method(variable, values) private
- #root_of_or_node(or_node) private
- #simple_comparison(node) private
- #skipped_within_range?(skipped, range) ⇒ Boolean private
- #variable_name(node) private
::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, | |
| #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 a gems locked versions (i.e. |
| #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?, #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 Attribute Details
#allow_method_comparison? ⇒ Boolean (readonly, private)
[ GitHub ]
# File 'lib/rubocop/cop/style/multiple_comparison.rb', line 165
def allow_method_comparison? cop_config.fetch('AllowMethodComparison', true) end
Instance Method Details
#collect_comparison(node, comparison, variables, values, skipped) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/multiple_comparison.rb', line 114
def collect_comparison(node, comparison, variables, values, skipped) var, obj = comparison if allow_method_comparison? && obj.call_type? skipped << node return end variables << var return if variables.size > 1 values << obj end
#comparison?(node) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/multiple_comparison.rb', line 143
def comparison?(node) !!simple_comparison(node) || nested_comparison?(node) end
#comparisons_threshold (private)
[ GitHub ]# File 'lib/rubocop/cop/style/multiple_comparison.rb', line 169
def comparisons_threshold cop_config.fetch('ComparisonsThreshold', 2) end
#find_offending_var(node, variables = Set.new, values = [], skipped = []) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/multiple_comparison.rb', line 101
def find_offending_var(node, variables = Set.new, values = [], skipped = []) if node.or_type? find_offending_var(node.lhs, variables, values, skipped) find_offending_var(node.rhs, variables, values, skipped) elsif simple_double_comparison?(node) return elsif (comparison = simple_comparison(node)) collect_comparison(node, comparison, variables, values, skipped) end [variables.first, values, skipped] if variables.any? end
#nested_comparison?(node) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/multiple_comparison.rb', line 135
def nested_comparison?(node) if node.or_type? node.node_parts.all? { |node_part| comparison? node_part } else false end end
#offense_range(values) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/multiple_comparison.rb', line 127
def offense_range(values) values.first.parent.source_range.begin.join(values.last.parent.source_range.end) end
#on_or(node)
[ GitHub ]# File 'lib/rubocop/cop/style/multiple_comparison.rb', line 73
def on_or(node) return unless node == root_of_or_node(node) return unless nested_comparison?(node) return unless (variable, values, skipped = find_offending_var(node)) return if values.size < comparisons_threshold range = offense_range(values) # Bail when an allowed method comparison sits between the collected # values: collapsing them into a single `include?` would drop it. return if skipped_within_range?(skipped, range) add_offense(range) do |corrector| corrector.replace(range, preferred_method(variable, values)) end end
#preferred_method(variable, values) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/multiple_comparison.rb', line 95
def preferred_method(variable, values) elements = values.map(&:source).join(', ') argument = variable.lvar_type? ? variable_name(variable) : variable.source "[#{elements}].include?(#{argument})" end
#root_of_or_node(or_node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/multiple_comparison.rb', line 155
def root_of_or_node(or_node) return or_node unless or_node.parent if or_node.parent.or_type? root_of_or_node(or_node.parent) else or_node end end
#simple_comparison(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/multiple_comparison.rb', line 147
def simple_comparison(node) if (var, obj = simple_comparison_lhs(node)) || (obj, var = simple_comparison_rhs(node)) return if var.call_type? && !allow_method_comparison? [var, obj] end end
#simple_comparison_lhs(node)
[ GitHub ]# File 'lib/rubocop/cop/style/multiple_comparison.rb', line 64
def_node_matcher :simple_comparison_lhs, <<~PATTERN (send ${lvar call} :== $_) PATTERN
#simple_comparison_rhs(node)
[ GitHub ]# File 'lib/rubocop/cop/style/multiple_comparison.rb', line 69
def_node_matcher :simple_comparison_rhs, <<~PATTERN (send $_ :== ${lvar call}) PATTERN
#simple_double_comparison?(node)
[ GitHub ]# File 'lib/rubocop/cop/style/multiple_comparison.rb', line 59
def_node_matcher :simple_double_comparison?, <<~PATTERN (send lvar :== lvar) PATTERN
#skipped_within_range?(skipped, range) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/multiple_comparison.rb', line 91
def skipped_within_range?(skipped, range) skipped.any? { |node_part| range.contains?(node_part.source_range) } end
#variable_name(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/multiple_comparison.rb', line 131
def variable_name(node) node.children[0] end