Refactor AST::ScopeVisitor similarly to AST::NodeVisitor

This commit is contained in:
Sijawusz Pur Rahnama 2022-04-04 01:35:32 +02:00
parent 3f7ade573a
commit a38cfd1661

View file

@ -3,11 +3,28 @@ require "./base_visitor"
module Ameba::AST
# AST Visitor that traverses the source and constructs scopes.
class ScopeVisitor < BaseVisitor
# Non-exhaustive list of nodes to be visited by Ameba's rules.
NODES = [
ClassDef,
ModuleDef,
EnumDef,
LibDef,
FunDef,
TypeDef,
TypeOf,
CStructOrUnionDef,
ProcLiteral,
Block,
Macro,
MacroFor,
]
SUPER_NODE_NAME = "super"
RECORD_NODE_NAME = "record"
@scope_queue = [] of Scope
@current_scope : Scope
@current_assign : Crystal::ASTNode?
@skip : Array(Crystal::ASTNode.class)?
def initialize(@rule, @source, skip = nil)
@ -39,73 +56,18 @@ module Ameba::AST
on_scope_end(node) if @current_scope.eql?(node)
end
# :nodoc:
def visit(node : Crystal::ClassDef)
on_scope_enter(node)
end
# :nodoc:
def visit(node : Crystal::ModuleDef)
on_scope_enter(node)
end
# :nodoc:
def visit(node : Crystal::EnumDef)
on_scope_enter(node)
end
# :nodoc:
def visit(node : Crystal::LibDef)
on_scope_enter(node)
end
# :nodoc:
def visit(node : Crystal::FunDef)
on_scope_enter(node)
end
# :nodoc:
def visit(node : Crystal::TypeDef)
on_scope_enter(node)
end
# :nodoc:
def visit(node : Crystal::TypeOf)
on_scope_enter(node)
end
# :nodoc:
def visit(node : Crystal::CStructOrUnionDef)
on_scope_enter(node)
end
{% for name in NODES %}
# :nodoc:
def visit(node : Crystal::{{name}})
on_scope_enter(node)
end
{% end %}
# :nodoc:
def visit(node : Crystal::Def)
node.name == "->" || on_scope_enter(node)
end
# :nodoc:
def visit(node : Crystal::ProcLiteral)
on_scope_enter(node)
end
# :nodoc:
def visit(node : Crystal::Block)
on_scope_enter(node)
end
# :nodoc:
def visit(node : Crystal::Macro)
on_scope_enter(node)
end
# :nodoc:
def visit(node : Crystal::MacroFor)
on_scope_enter(node)
end
@current_assign : Crystal::ASTNode?
# :nodoc:
def visit(node : Crystal::Assign | Crystal::OpAssign | Crystal::MultiAssign | Crystal::UninitializedVar)
@current_assign = node