mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
Refactor AST::ScopeVisitor
similarly to AST::NodeVisitor
This commit is contained in:
parent
3f7ade573a
commit
a38cfd1661
1 changed files with 23 additions and 61 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue