From 3f7ade573a7b8a26283570d31f1acae0742f43d7 Mon Sep 17 00:00:00 2001 From: Sijawusz Pur Rahnama Date: Mon, 4 Apr 2022 01:34:34 +0200 Subject: [PATCH] Add ability to skip some of the nodes in `AST::ScopeVisitor` --- src/ameba/ast/visitors/scope_visitor.cr | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/ameba/ast/visitors/scope_visitor.cr b/src/ameba/ast/visitors/scope_visitor.cr index aaae84ea..e6f55394 100644 --- a/src/ameba/ast/visitors/scope_visitor.cr +++ b/src/ameba/ast/visitors/scope_visitor.cr @@ -8,14 +8,17 @@ module Ameba::AST @scope_queue = [] of Scope @current_scope : Scope + @skip : Array(Crystal::ASTNode.class)? - def initialize(@rule, @source) + def initialize(@rule, @source, skip = nil) + @skip = skip.try &.map(&.as(Crystal::ASTNode.class)) @current_scope = Scope.new(@source.ast) # top level scope @source.ast.accept self @scope_queue.each { |scope| @rule.test @source, scope.node, scope } end private def on_scope_enter(node) + return if skip?(node) @current_scope = Scope.new(node, @current_scope) end @@ -182,5 +185,9 @@ module Ameba::AST private def record_macro?(node) node.name == RECORD_NODE_NAME && node.args.first?.is_a?(Crystal::Path) end + + private def skip?(node) + !!@skip.try(&.includes?(node.class)) + end end end