Skip to content

Naming/MethodName crashes with NoMethodError on alias with string interpolation #14622

@Magikdidi24

Description

@Magikdidi24

Steps to reproduce the problem

Create a file with the following content:

  class Example
    attr_reader :foo_value
    attr_reader :bar_value

    def initialize(foo_name: 'foo', bar_name: 'bar')
      @foo_name = foo_name
      @bar_name = bar_name
    end

    def example_method
      alias :"get_#{@foo_name}" :foo_value
      alias :"get_#{@bar_name}" :bar_value
    end
  end

Run RuboCop

bundle exec rubocop test.rb

Expected behavior

RuboCop should either check the naming convention for alias
declarations with interpolated symbols or skip them without
crashing.

Actual behavior

RuboCop crashes with a NoMethodError. Running with --debug shows:

An error occurred while Naming/MethodName cop was inspecting
test.rb:13:4.
An error occurred while Naming/MethodName cop was inspecting
test.rb:14:4.

/path/to/rubocop-1.81.6/lib/rubocop/cop/naming/method_name.rb:150
:in 'RuboCop::Cop::Naming::MethodName#on_alias':
undefined method 'value' for an instance of RuboCop::AST::Node
(NoMethodError)

The on_alias method in lib/rubocop/cop/naming/method_name.rb:150
attempts to call .value on node.new_identifier, but when the
alias uses string interpolation (e.g., alias :"method_#{var}"
:original), the node is of type :dstr (dynamic string), which
does not have a .value method. The .value method only exists on
:str and :sym node types.

RuboCop version

$ bundle exec rubocop -V
1.81.6 (using Parser 3.3.9.0, Prism 1.6.0, rubocop-ast 1.47.1,
analyzing as Ruby 3.4, running on ruby 3.4.7) [x86_64-linux]
- rubocop-minitest 0.38.2
- rubocop-performance 1.26.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions