You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Rubocop (when autocorrecting) should not attempt to replace alias_method with alias when the first argument to alias_method is not a literal.
Actual behavior
Rubocop attempts (when autocorrecting) to replace alias_method with alias even the first argument to alias_method is not a literal. It converts alias_method CONSTANT_HASH[MY_CONSTANT], :'sample?' to alias (const nil :CONSTANT_HASH) sample? which is not valid code.
Steps to reproduce the problem
Full command line session (on Windows 10 in Emacs eshell using the Ruby from RubyInstaller2; this should not matter though) shown below to reproduce:
~/Projects/TMP $ cat .rubocop.yml
AllCops:
DisplayCopNames: true
DisplayStyleGuide: true
TargetRubyVersion: 2.4
~/Projects/TMP $ ruby -v
ruby 2.4.1p111 (2017-03-22 revision 58053) [x64-mingw32]
~/Projects/TMP $ cat alias_rubocop_test_case.rb
# frozen_string_literal: true
# Sample class
class SampleClass
MY_CONSTANT = 1
CONSTANT_HASH = {
MY_CONSTANT => :sample
}.freeze
class << self
def sample?
(1..100).to_a.sample
end
alias_method CONSTANT_HASH[MY_CONSTANT], :'sample?'
end
end
puts SampleClass.sample?
puts SampleClass.sample
~/Projects/TMP $ rubocop --version
0.49.0
~/Projects/TMP $ ruby alias_rubocop_test_case.rb
35
79
~/Projects/TMP $ rubocop -a alias_rubocop_test_case.rb
Inspecting 1 file
E
Offenses:
alias_rubocop_test_case.rb:15:5: C: [Corrected] Style/Alias: Use alias instead of alias_method in a class body. (https://github.com/bbatsov/ruby-style-guide#alias-method)
alias_method CONSTANT_HASH[MY_CONSTANT], :'sample?'
^^^^^^^^^^^^
alias_rubocop_test_case.rb:15:11: E: unexpected token tLPAREN2
(Using Ruby 2.4 parser; configure using TargetRubyVersion parameter, under AllCops)
alias (const nil :CONSTANT_HASH) sample?
^
1 file inspected, 2 offenses detected, 1 offense corrected
~/Projects/TMP $ cat alias_rubocop_test_case.rb
# frozen_string_literal: true
# Sample class
class SampleClass
MY_CONSTANT = 1
CONSTANT_HASH = {
MY_CONSTANT => :sample
}.freeze
class << self
def sample?
(1..100).to_a.sample
end
alias (const nil :CONSTANT_HASH) sample?
end
end
puts SampleClass.sample?
puts SampleClass.sample
~/Projects/TMP $ ruby alias_rubocop_test_case.rb
alias_rubocop_test_case.rb:15: syntax error, unexpected '('
alias (const nil :CONSTANT_HASH) samp
^
~/Projects/TMP $
…literal arguments
This cop would have a bad auto-correct when configured style is
`prefer_alias`, and the originating `#alias_method` has a non-literal
argument, e.g.:
```
class Foo
alias_method :foo, BAR
end
```
This change fixes that.
Rubocop Style/Alias cop breaks working ruby code with nonsensical fix when autocorrecting.
The following working code:
Is autocorrected to following broken code:
When using the following rubocop configuration (
.rubocop.yml
in same directory as source file):Expected behavior
Rubocop (when autocorrecting) should not attempt to replace
alias_method
withalias
when the first argument toalias_method
is not a literal.Actual behavior
Rubocop attempts (when autocorrecting) to replace
alias_method
withalias
even the first argument toalias_method
is not a literal. It convertsalias_method CONSTANT_HASH[MY_CONSTANT], :'sample?'
toalias (const nil :CONSTANT_HASH) sample?
which is not valid code.Steps to reproduce the problem
Full command line session (on Windows 10 in Emacs eshell using the Ruby from RubyInstaller2; this should not matter though) shown below to reproduce:
RuboCop version
The text was updated successfully, but these errors were encountered: