Class: RuboCop::Cop::Lint::LiteralAsCondition
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
::RuboCop::Cop::Base ,
::RuboCop::ExcludeLimit ,
NodePattern::Macros,
RuboCop::AST::Sexp
|
|
Instance Chain:
self,
::RuboCop::Cop::RangeHelp ,
::RuboCop::Cop::Base ,
::RuboCop::Cop::AutocorrectLogic ,
::RuboCop::Cop::IgnoredNode ,
::RuboCop::Util ,
RuboCop::AST::Sexp
|
|
Inherits: |
RuboCop::Cop::Base
|
Defined in: | lib/rubocop/cop/lint/literal_as_condition.rb |
Overview
Checks for literals used as the conditions or as operands in and/or expressions serving as the conditions of if/while/until/case-when/case-in.
Note
|
Literals in case-in condition where the match variable is used in
in are accepted as a pattern matching.
|
Constant Summary
-
MSG =
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 38'Literal `%<literal>s` appeared as a condition.'
-
RESTRICT_ON_SEND =
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 39[:!].freeze
::RuboCop::Cop::Base
- Inherited
EMPTY_OFFENSES, RESTRICT_ON_SEND
::RuboCop::Cop::RangeHelp
- Included
Class Attribute Summary
::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 |
::RuboCop::ExcludeLimit
- Extended
exclude_limit | Sets up a configuration option to have an exclude limit tracked. |
transform |
Instance Attribute Summary
::RuboCop::Cop::Base
- Inherited
::RuboCop::Cop::AutocorrectLogic
- Included
Instance Method Summary
- #message(node)
- #on_case(case_node)
- #on_case_match(case_match_node)
- #on_if(node)
- #on_send(node)
- #on_until(node) (also: #on_until_post)
-
#on_until_post(node)
Alias for #on_until.
- #on_while(node) (also: #on_while_post)
-
#on_while_post(node)
Alias for #on_while.
- #basic_literal?(node) ⇒ Boolean private
- #check_case(case_node) private
- #check_for_literal(node) private
- #check_node(node) private
- #condition(node) private
- #handle_node(node) private
- #primitive_array?(node) ⇒ Boolean private
- #when_conditions_range(when_node) private
::RuboCop::Cop::RangeHelp
- Included
#add_range, #column_offset_between, | |
#contents_range | A range containing only the contents of a literal with delimiters (e.g. |
#directions, | |
#effective_column | Returns the column attribute of the range, except if the range is on the first line and there’s a byte order mark at the beginning of that line, in which case 1 is subtracted from the column value. |
#final_pos, #move_pos, #move_pos_str, #range_between, #range_by_whole_lines, #range_with_comments, #range_with_comments_and_lines, #range_with_surrounding_comma, #range_with_surrounding_space, #source_range |
::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, #current_corrector, | |
#current_offense_locations | Reserved for Commissioner: |
#current_offenses, #currently_disabled_lines, #custom_severity, #default_severity, #disable_uncorrectable, #enabled_line?, #file_name_matches_any?, #find_message, #find_severity, #range_for_original, #range_from_node_or_range, | |
#reset_investigation | Actually private methods. |
#use_corrector |
::RuboCop::Cop::AutocorrectLogic
- Included
#disable_offense, #disable_offense_at_end_of_line, #disable_offense_before_and_after, #disable_offense_with_eol_or_surround_comment, #heredoc_range, #max_line_length, #multiline_ranges, #multiline_string?, | |
#range_by_lines | Expand the given range to include all of any lines it covers. |
#range_of_first_line, #range_overlaps_offense?, #string_continuation?, #surrounding_heredoc?, #surrounding_percent_array? |
::RuboCop::Cop::IgnoredNode
- Included
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Method Details
#basic_literal?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 109
def basic_literal?(node) if node.array_type? primitive_array?(node) else node.basic_literal? end end
#check_case(case_node) (private)
[ GitHub ]# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 139
def check_case(case_node) condition = case_node.condition return if condition.array_type? && !primitive_array?(condition) return if condition.dstr_type? handle_node(condition) end
#check_for_literal(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 100
def check_for_literal(node) cond = condition(node) if cond.literal? add_offense(cond) else check_node(cond) end end
#check_node(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 121
def check_node(node) if node.send_type? && node.prefix_bang? handle_node(node.receiver) elsif node.operator_keyword? node.each_child_node { |op| handle_node(op) } elsif node.begin_type? && node.children.one? handle_node(node.children.first) end end
#condition(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 148
def condition(node) if node.send_type? node.receiver else node.condition end end
#handle_node(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 131
def handle_node(node) if node.literal? add_offense(node) elsif %i[send and or begin].include?(node.type) check_node(node) end end
#message(node)
[ GitHub ]# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 94
def (node) format(MSG, literal: node.source) end
#on_case(case_node)
[ GitHub ]# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 59
def on_case(case_node) if case_node.condition check_case(case_node) else case_node.when_branches.each do |when_node| next unless when_node.conditions.all?(&:literal?) range = when_conditions_range(when_node) = (range) add_offense(range, message: ) end end end
#on_case_match(case_match_node)
[ GitHub ]# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 74
def on_case_match(case_match_node) if case_match_node.condition return if case_match_node.descendants.any?(&:match_var_type?) check_case(case_match_node) else case_match_node.each_in_pattern do |in_pattern_node| next unless in_pattern_node.condition.literal? add_offense(in_pattern_node) end end end
#on_if(node)
[ GitHub ]# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 41
def on_if(node) check_for_literal(node) end
#on_send(node)
[ GitHub ]# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 88
def on_send(node) return unless node.negation_method? check_for_literal(node) end
#on_until(node) Also known as: #on_until_post
[ GitHub ]# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 52
def on_until(node) return if condition(node).false_type? check_for_literal(node) end
#on_until_post(node)
Alias for #on_until.
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 57
alias on_until_post on_until
#on_while(node) Also known as: #on_while_post
[ GitHub ]# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 45
def on_while(node) return if condition(node).true_type? check_for_literal(node) end
#on_while_post(node)
Alias for #on_while.
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 50
alias on_while_post on_while
#primitive_array?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 117
def primitive_array?(node) node.children.all? { |n| basic_literal?(n) } end
#when_conditions_range(when_node) (private)
[ GitHub ]# File 'lib/rubocop/cop/lint/literal_as_condition.rb', line 156
def when_conditions_range(when_node) range_between( when_node.conditions.first.source_range.begin_pos, when_node.conditions.last.source_range.end_pos ) end