Class: RuboCop::Cop::Lint::RedundantSplatExpansion
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/lint/redundant_splat_expansion.rb |
Overview
Checks for unneeded usages of splat expansion.
Constant Summary
-
ARRAY_PARAM_MSG =
# File 'lib/rubocop/cop/lint/redundant_splat_expansion.rb', line 75'Pass array contents as separate arguments.'
-
ASSIGNMENT_TYPES =
# File 'lib/rubocop/cop/lint/redundant_splat_expansion.rb', line 80%i[lvasgn ivasgn cvasgn gvasgn].freeze
-
MSG =
# File 'lib/rubocop/cop/lint/redundant_splat_expansion.rb', line 74'Replace splat expansion with comma separated values.'
-
PERCENT_CAPITAL_I =
# File 'lib/rubocop/cop/lint/redundant_splat_expansion.rb', line 79'%I'
-
PERCENT_CAPITAL_W =
# File 'lib/rubocop/cop/lint/redundant_splat_expansion.rb', line 77'%W'
-
PERCENT_I =
# File 'lib/rubocop/cop/lint/redundant_splat_expansion.rb', line 78'%i'
-
PERCENT_W =
# File 'lib/rubocop/cop/lint/redundant_splat_expansion.rb', line 76'%w'
::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 |
::RuboCop::ExcludeLimit
- Extended
exclude_limit | Sets up a configuration option to have an exclude limit tracked. |
transform |
Instance Attribute Summary
- #allow_percent_literal_array_argument? ⇒ Boolean readonly private
::RuboCop::Cop::Base
- Inherited
::RuboCop::Cop::AutocorrectLogic
- Included
Instance Method Summary
- #array_new?(node)
- #literal_expansion(node)
- #on_splat(node)
- #array_new_inside_array_literal?(array_new_node) ⇒ Boolean private
- #array_splat?(node) ⇒ Boolean private
- #autocorrect(corrector, node) private
- #method_argument?(node) ⇒ Boolean private
- #part_of_an_array?(node) ⇒ Boolean private
- #redundant_brackets?(node) ⇒ Boolean private
- #redundant_splat_expansion(node) private
- #remove_brackets(array) private
- #replacement_range_and_content(node) private
- #use_percent_literal_array_argument?(node) ⇒ Boolean 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, #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 Attribute Details
#allow_percent_literal_array_argument? ⇒ Boolean
(readonly, private)
[ GitHub ]
# File 'lib/rubocop/cop/lint/redundant_splat_expansion.rb', line 204
def allow_percent_literal_array_argument? cop_config.fetch('AllowPercentLiteralArrayArgument', true) end
Instance Method Details
#array_new?(node)
[ GitHub ]# File 'lib/rubocop/cop/lint/redundant_splat_expansion.rb', line 83
def_node_matcher :array_new?, <<~PATTERN { $(send (const {nil? cbase} :Array) :new ...) $(block (send (const {nil? cbase} :Array) :new ...) ...) } PATTERN
#array_new_inside_array_literal?(array_new_node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/lint/redundant_splat_expansion.rb', line 131
def array_new_inside_array_literal?(array_new_node) return false unless array_new?(array_new_node) grandparent = array_new_node.parent.parent grandparent.array_type? && grandparent.children.size > 1 end
#array_splat?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/lint/redundant_splat_expansion.rb', line 156
def array_splat?(node) node.children.first.array_type? end
#autocorrect(corrector, node) (private)
[ GitHub ]# File 'lib/rubocop/cop/lint/redundant_splat_expansion.rb', line 112
def autocorrect(corrector, node) range, content = replacement_range_and_content(node) corrector.replace(range, content) end
#literal_expansion(node)
[ GitHub ]# File 'lib/rubocop/cop/lint/redundant_splat_expansion.rb', line 91
def_node_matcher :literal_expansion, <<~PATTERN (splat {$({str dstr int float array} ...) (block $#array_new? ...) $#array_new?} ...) PATTERN
#method_argument?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/lint/redundant_splat_expansion.rb', line 160
def method_argument?(node) node.parent.call_type? end
#on_splat(node)
[ GitHub ]# File 'lib/rubocop/cop/lint/redundant_splat_expansion.rb', line 95
def on_splat(node) redundant_splat_expansion(node) do if array_splat?(node) && (method_argument?(node) || part_of_an_array?(node)) return if allow_percent_literal_array_argument? && use_percent_literal_array_argument?(node) add_offense(node, message: ARRAY_PARAM_MSG) do |corrector| autocorrect(corrector, node) end else add_offense(node) { |corrector| autocorrect(corrector, node) } end end end
#part_of_an_array?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/lint/redundant_splat_expansion.rb', line 164
def part_of_an_array?(node) # The parent of a splat expansion is an array that does not have # `begin` or `end` parent = node.parent parent.array_type? && parent.loc.begin && parent.loc.end end
#redundant_brackets?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/lint/redundant_splat_expansion.rb', line 171
def redundant_brackets?(node) parent = node.parent grandparent = node.parent.parent parent.when_type? || method_argument?(node) || part_of_an_array?(node) || grandparent&.resbody_type? end
#redundant_splat_expansion(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/lint/redundant_splat_expansion.rb', line 118
def redundant_splat_expansion(node) literal_expansion(node) do || if .send_type? return if array_new_inside_array_literal?( ) grandparent = node.parent.parent return if grandparent && !ASSIGNMENT_TYPES.include?(grandparent.type) end yield end end
#remove_brackets(array) (private)
[ GitHub ]# File 'lib/rubocop/cop/lint/redundant_splat_expansion.rb', line 179
def remove_brackets(array) array_start = array.loc.begin.source elements = *array elements = elements.map(&:source) if array_start.start_with?(PERCENT_W) "'#{elements.join("', '")}'" elsif array_start.start_with?(PERCENT_CAPITAL_W) %("#{elements.join('", "')}") elsif array_start.start_with?(PERCENT_I) ":#{elements.join(', :')}" elsif array_start.start_with?(PERCENT_CAPITAL_I) %(:"#{elements.join('", :"')}") else elements.join(', ') end end
#replacement_range_and_content(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/lint/redundant_splat_expansion.rb', line 139
def replacement_range_and_content(node) variable = node.children.first expression = node.source_range if array_new?(variable) expression = node.parent.source_range if node.parent.array_type? [expression, variable.source] elsif !variable.array_type? [expression, "[#{variable.source}]"] elsif redundant_brackets?(node) [expression, remove_brackets(variable)] else [node.loc.operator, ''] end end
#use_percent_literal_array_argument?(node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/lint/redundant_splat_expansion.rb', line 197
def use_percent_literal_array_argument?(node) argument = node.children.first method_argument?(node) && (argument.percent_literal?(:string) || argument.percent_literal?(:symbol)) end