Sha256: 7770ec86eb80f6df578c3b6b199b20af282fbf9483ce2a59a07bc504a6f025b3

Contents?: true

Size: 1.42 KB

Versions: 11

Compression:

Stored size: 1.42 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
          return unless method_name == :instance_exec

          body.each_node(:alias) { |n| ignore_node(n) }
        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

11 entries across 11 versions & 2 rubygems

Version Path
rubyjobbuilderdsl-0.0.2 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/alias.rb
rubyjobbuilderdsl-0.0.1 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/alias.rb
rubocop-0.30.1 lib/rubocop/cop/style/alias.rb
rubocop-0.30.0 lib/rubocop/cop/style/alias.rb
rubocop-0.29.1 lib/rubocop/cop/style/alias.rb
rubocop-0.29.0 lib/rubocop/cop/style/alias.rb
rubocop-0.28.0 lib/rubocop/cop/style/alias.rb
rubocop-0.27.1 lib/rubocop/cop/style/alias.rb
rubocop-0.27.0 lib/rubocop/cop/style/alias.rb
rubocop-0.26.1 lib/rubocop/cop/style/alias.rb
rubocop-0.26.0 lib/rubocop/cop/style/alias.rb