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