Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rubocop Style/Alias cop breaks code with nonsensical fix when autocorrecting #4434

Closed
preetpalS opened this issue May 28, 2017 · 0 comments
Closed
Labels

Comments

@preetpalS
Copy link

Rubocop Style/Alias cop breaks working ruby code with nonsensical fix when autocorrecting.

The following working code:

# 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

Is autocorrected to following broken code:

# 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

When using the following rubocop configuration (.rubocop.yml in same directory as source file):

AllCops:
  DisplayCopNames: true
  DisplayStyleGuide: true
  TargetRubyVersion: 2.4

Expected behavior

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 $ 

RuboCop version

~/Projects/TMP $ rubocop -V
0.49.0 (using Parser 2.4.0.0, running on ruby 2.4.1 x64-mingw32)
@bbatsov bbatsov added the bug label May 28, 2017
Drenmi added a commit to Drenmi/rubocop that referenced this issue Jun 2, 2017
…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.
@bbatsov bbatsov closed this as completed in 0bb3d3e Jun 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants