Sha256: 4f94e9f38b5a2c9706f320e7013f39f8480dea122358302113d1fa5625608c6e
Contents?: true
Size: 1.41 KB
Versions: 4
Compression:
Stored size: 1.41 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) 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
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
rubocop-0.33.0 | lib/rubocop/cop/style/alias.rb |
rubocop-0.32.1 | lib/rubocop/cop/style/alias.rb |
rubocop-0.32.0 | lib/rubocop/cop/style/alias.rb |
rubocop-0.31.0 | lib/rubocop/cop/style/alias.rb |