123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::Style::Alias

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:
Inherits: RuboCop::Cop::Base
Defined in: lib/rubocop/cop/style/alias.rb

Overview

Enforces the use of either #alias or #alias_method depending on configuration. It also flags uses of alias :symbol rather than alias bareword.

However, it will always enforce method_alias when used alias in an instance method definition and in a singleton method definition. If used in a block, always enforce alias_method unless it is an instance_eval block.

Examples:

EnforcedStyle: prefer_alias (default)

# bad
alias_method :bar, :foo
alias :bar :foo

# good
alias bar foo

EnforcedStyle: prefer_alias_method

# bad
alias :bar :foo
alias bar foo

# good
alias_method :bar, :foo

Constant Summary

::RuboCop::Cop::Base - Inherited

EMPTY_OFFENSES, RESTRICT_ON_SEND

::RuboCop::Cop::ConfigurableEnforcedStyle - Included

SYMBOL_TO_STRING_CACHE

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 on_new_investigation and on_investigation_end, the result of processed_source will remain constant.

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

Instance Method Summary

::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,
#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 add_offense or add_global_offense Cops are discouraged to override this; instead pass your message directly.

#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_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

::RuboCop::Cop::IgnoredNode - Included

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#add_offense_for_args(node, &block) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/alias.rb', line 86

def add_offense_for_args(node, &block)
  existing_args  = node.children.map(&:source).join(' ')
  preferred_args = node.children.map { |a| a.source[1..] }.join(' ')
  arg_ranges     = node.children.map(&:source_range)
  msg            = format(MSG_SYMBOL_ARGS, prefer: preferred_args, current: existing_args)
  add_offense(arg_ranges.reduce(&:join), message: msg, &block)
end

#alias_keyword_possible?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/alias.rb', line 76

def alias_keyword_possible?(node)
  scope_type(node) != :dynamic && node.arguments.all?(&:sym_type?)
end

#alias_method_possible?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/alias.rb', line 80

def alias_method_possible?(node)
  scope_type(node) != :instance_eval &&
    node.children.none?(&:gvar_type?) &&
    node.each_ancestor(:def).none?
end

#autocorrect(corrector, node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/alias.rb', line 66

def autocorrect(corrector, node)
  if node.send_type?
    correct_alias_method_to_alias(corrector, node)
  elsif scope_type(node) == :dynamic || style == :prefer_alias_method
    correct_alias_to_alias_method(corrector, node)
  else
    correct_alias_with_symbol_args(corrector, node)
  end
end

#bareword?(sym_node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/alias.rb', line 125

def bareword?(sym_node)
  !sym_node.source.start_with?(':') || sym_node.dsym_type?
end

#correct_alias_method_to_alias(corrector, send_node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/alias.rb', line 129

def correct_alias_method_to_alias(corrector, send_node)
  new, old = *send_node.arguments
  replacement = "alias #{identifier(new)} #{identifier(old)}"

  corrector.replace(send_node, replacement)
end

#correct_alias_to_alias_method(corrector, node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/alias.rb', line 136

def correct_alias_to_alias_method(corrector, node)
  replacement =
    "alias_method #{identifier(node.new_identifier)}, #{identifier(node.old_identifier)}"

  corrector.replace(node, replacement)
end

#correct_alias_with_symbol_args(corrector, node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/alias.rb', line 143

def correct_alias_with_symbol_args(corrector, node)
  corrector.replace(node.new_identifier, node.new_identifier.source[1..])
  corrector.replace(node.old_identifier, node.old_identifier.source[1..])
end

#identifier(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/alias.rb', line 148

def identifier(node)
  if node.sym_type?
    ":#{node.children.first}"
  else
    node.source
  end
end

#lexical_scope_type(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/alias.rb', line 114

def lexical_scope_type(node)
  ancestor = node.each_ancestor(:class, :module).first
  if ancestor.nil?
    'at the top level'
  elsif ancestor.class_type?
    'in a class body'
  else
    'in a module body'
  end
end

#on_alias(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/alias.rb', line 52

def on_alias(node)
  return unless alias_method_possible?(node)

  if scope_type(node) == :dynamic || style == :prefer_alias_method
    add_offense(node.loc.keyword, message: MSG_ALIAS) do |corrector|
      autocorrect(corrector, node)
    end
  elsif node.children.none? { |arg| bareword?(arg) }
    add_offense_for_args(node) { |corrector| autocorrect(corrector, node) }
  end
end

#on_send(node)

[ GitHub ]

  
# File 'lib/rubocop/cop/style/alias.rb', line 41

def on_send(node)
  return unless node.command?(:alias_method)
  return unless style == :prefer_alias && alias_keyword_possible?(node)
  return unless node.arguments.count == 2

  msg = format(MSG_ALIAS_METHOD, current: lexical_scope_type(node))
  add_offense(node.loc.selector, message: msg) do |corrector|
    autocorrect(corrector, node)
  end
end

#scope_type(node) (private)

In this expression, will self be the same as the innermost enclosing class or module block (:lexical)? Or will it be something else (:dynamic)? If we’re in an instance_eval block, return that.

[ GitHub ]

  
# File 'lib/rubocop/cop/style/alias.rb', line 97

def scope_type(node)
  while (parent = node.parent)
    case parent.type
    when :class, :module
      return :lexical
    when :def, :defs
      return :dynamic
    when :block
      return :instance_eval if parent.method?(:instance_eval)

      return :dynamic
    end
    node = parent
  end
  :lexical
end