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