Sha256: 72d7f04ca9eb13d8eadfc3152c5c32bdc8542a397f85b940933e9eb822a39e00
Contents?: true
Size: 1.42 KB
Versions: 2
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 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
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
rubocop-0.19.1 | lib/rubocop/cop/style/alias.rb |
rubocop-0.19.0 | lib/rubocop/cop/style/alias.rb |