Sha256: 4fe99e595fb4e47e25dbce2dca220a4efdf64eb7c4945af614527da9eb3379b9

Contents?: true

Size: 1.03 KB

Versions: 86

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

module CustomCops
  class VariableNameShadowingMethod < RuboCop::Cop::Cop
    # For each source file, Rubocop calls on_new_investigation, then walks the abstract syntax
    # tree calling on_foo methods for each "foo" AST node - e.g on_begin, on_def, on_args,
    # on_int, etc.

    # We need to do two passes over the source so that we can find all the method names before
    # we start looking at the nodes that assign local variables (some methods may be defined
    # _after_ code that assigns shadowing local variables. We do the first one in
    # on_new_investigation

    def_node_search :method_names, <<~PATTERN
      (:def $_ ...)
    PATTERN

    def on_new_investigation
      ast = processed_source.ast
      @declared_method_names = ast ? method_names(processed_source.ast).to_a : []
    end

    def on_lvasgn(node)
      if @declared_method_names.include?(node.name)
        add_offense(
          node,
          message: "Shadowing method name - `#{node.name}`."
        )
      end
    end
  end
end

Version data entries

86 entries across 86 versions & 1 rubygems

Version Path
simplycop-2.6.0 lib/simplycop/custom_cops/variable_name_shadowing_method.rb
simplycop-2.5.1 lib/simplycop/custom_cops/variable_name_shadowing_method.rb
simplycop-2.5.0 lib/simplycop/custom_cops/variable_name_shadowing_method.rb
simplycop-2.3.3 lib/simplycop/custom_cops/variable_name_shadowing_method.rb
simplycop-2.3.2 lib/simplycop/custom_cops/variable_name_shadowing_method.rb
simplycop-2.3.1 lib/simplycop/custom_cops/variable_name_shadowing_method.rb
simplycop-2.3.0 lib/simplycop/custom_cops/variable_name_shadowing_method.rb
simplycop-2.2.0 lib/simplycop/custom_cops/variable_name_shadowing_method.rb
simplycop-2.1.4 lib/simplycop/custom_cops/variable_name_shadowing_method.rb
simplycop-2.1.3 lib/simplycop/custom_cops/variable_name_shadowing_method.rb
simplycop-2.1.2 lib/simplycop/custom_cops/variable_name_shadowing_method.rb
simplycop-2.1.0 lib/simplycop/custom_cops/variable_name_shadowing_method.rb
simplycop-2.0.1 lib/simplycop/custom_cops/variable_name_shadowing_method.rb
simplycop-2.0.0 lib/simplycop/custom_cops/variable_name_shadowing_method.rb
simplycop-1.19.3 lib/simplycop/custom_cops/variable_name_shadowing_method.rb
simplycop-1.19.2 lib/simplycop/custom_cops/variable_name_shadowing_method.rb
simplycop-1.19.1 lib/simplycop/custom_cops/variable_name_shadowing_method.rb
simplycop-1.19.0 lib/simplycop/custom_cops/variable_name_shadowing_method.rb
simplycop-1.17.0 lib/simplycop/custom_cops/variable_name_shadowing_method.rb
simplycop-1.16.6 lib/simplycop/custom_cops/variable_name_shadowing_method.rb