Add custom NodeVisitor subclass for Style/VariableNames

This commit is contained in:
fn ⌃ ⌥ 2021-11-09 09:26:00 -08:00
parent bd1a1a1fd4
commit c9ba487373
2 changed files with 18 additions and 12 deletions

View file

@ -38,7 +38,6 @@ module Ameba
class Greeting class Greeting
def initialize(@badNamed = nil) def initialize(@badNamed = nil)
# ^ error: Var name should be underscore-cased: @bad_named, not @badNamed # ^ error: Var name should be underscore-cased: @bad_named, not @badNamed
# ^ error: Var name should be underscore-cased: bad_named, not badNamed
end end
end end
CRYSTAL CRYSTAL

View file

@ -35,17 +35,9 @@ module Ameba::Rule::Style
issue_for node, MSG % {expected, node.name} issue_for node, MSG % {expected, node.name}
end end
# TODO: Handle special case where instance variable is method parameter. def test(source : Source)
# For example, this: VarVisitor.new self, source
# end
# def initialize(@foo)
# end
#
# is represented in the AST as:
#
# def initialize(foo)
# @foo = foo
# end
def test(source, node : Crystal::Var) def test(source, node : Crystal::Var)
check_node source, node check_node source, node
@ -58,5 +50,20 @@ module Ameba::Rule::Style
def test(source, node : Crystal::ClassVar) def test(source, node : Crystal::ClassVar)
check_node source, node check_node source, node
end end
private class VarVisitor < AST::NodeVisitor
private getter var_locations = [] of Crystal::Location
def visit(node : Crystal::Var)
!var_locations.includes?(node.location) && super
end
def visit(node : Crystal::InstanceVar | Crystal::ClassVar)
if (location = node.location)
var_locations << location
end
super
end
end
end end
end end