Sha256: bd04c797da229224c22cc01ae52ea28a62459b9de1d6b4cc8b02d78f0ad518aa

Contents?: true

Size: 1.03 KB

Versions: 10

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

module CustomCops
  class VariableNameShadowingMethod < RuboCop::Cop::Base
    # 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

10 entries across 10 versions & 1 rubygems

Version Path
simplycop-2.12.0 lib/simplycop/custom_cops/variable_name_shadowing_method.rb
simplycop-2.11.1 lib/simplycop/custom_cops/variable_name_shadowing_method.rb
simplycop-2.11.0 lib/simplycop/custom_cops/variable_name_shadowing_method.rb
simplycop-2.10.0 lib/simplycop/custom_cops/variable_name_shadowing_method.rb
simplycop-2.9.1 lib/simplycop/custom_cops/variable_name_shadowing_method.rb
simplycop-2.9.0 lib/simplycop/custom_cops/variable_name_shadowing_method.rb
simplycop-2.8.0 lib/simplycop/custom_cops/variable_name_shadowing_method.rb
simplycop-2.7.2 lib/simplycop/custom_cops/variable_name_shadowing_method.rb
simplycop-2.7.1 lib/simplycop/custom_cops/variable_name_shadowing_method.rb
simplycop-2.7.0 lib/simplycop/custom_cops/variable_name_shadowing_method.rb