Sha256: 72cac2a7012a2932a0edbb1c5e747f6c1d415d9f44e12caed12f48c35428af0c

Contents?: true

Size: 1.43 KB

Versions: 3

Compression:

Stored size: 1.43 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Style
      # The purpose of the this cop is advise the use of
      # alias_method over the alias keyword whenever possible.
      class Alias < Cop
        MSG = 'Use `alias_method` instead of `alias`.'

        def on_block(node)
          method, _args, body = *node
          _receiver, method_name = *method

          # using alias is the only option in certain scenarios
          # in such scenarios we don't want to report an offense
          if method_name == :instance_exec
            on_node(:alias, body) { |n| ignore_node(n) }
          end
        end

        def on_alias(node)
          return if ignored_node?(node)

          # alias_method can't be used with global variables
          new, old = *node

          return if new.type == :gvar && old.type == :gvar

          add_offense(node, :keyword)
        end

        def autocorrect(node)
          @corrections << lambda do |corrector|
            # replace alias with alias_method
            corrector.replace(node.loc.keyword, 'alias_method')
            # insert a comma
            new, old = *node
            corrector.insert_after(new.loc.expression, ',')
            # convert bareword arguments to symbols
            corrector.replace(new.loc.expression, ":#{new.children.first}")
            corrector.replace(old.loc.expression, ":#{old.children.first}")
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rubocop-0.21.0 lib/rubocop/cop/style/alias.rb
rubocop-0.20.1 lib/rubocop/cop/style/alias.rb
rubocop-0.20.0 lib/rubocop/cop/style/alias.rb