View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.ecmascript.rule;
5   
6   import java.util.Collections;
7   import java.util.List;
8   import java.util.Stack;
9   
10  import net.sourceforge.pmd.Rule;
11  import net.sourceforge.pmd.RuleContext;
12  import net.sourceforge.pmd.lang.ast.Node;
13  import net.sourceforge.pmd.lang.ecmascript.ast.EcmascriptNode;
14  import net.sourceforge.pmd.lang.ecmascript.ast.EcmascriptParserVisitor;
15  import net.sourceforge.pmd.lang.rule.AbstractRuleChainVisitor;
16  import net.sourceforge.pmd.lang.rule.XPathRule;
17  
18  public class EcmascriptRuleChainVisitor extends AbstractRuleChainVisitor {
19  
20      protected void indexNodes(List<Node> nodes, RuleContext ctx) {
21  	// Visit Nodes in DFS order
22  	Stack<Node> stack = new Stack<>();
23  	stack.addAll(nodes);
24  	Collections.reverse(stack);
25  	while (!stack.isEmpty()) {
26  	    Node node = stack.pop();
27  	    indexNode(node);
28  	    if (node.jjtGetNumChildren() > 0) {
29  		for (int i = node.jjtGetNumChildren() - 1; i >= 0; i--) {
30  		    stack.push(node.jjtGetChild(i));
31  		}
32  	    }
33  	}
34      }
35  
36      protected void visit(Rule rule, Node node, RuleContext ctx) {
37  	// Rule better either be a EcmascriptParserVisitor, or a XPathRule
38  	if (rule instanceof XPathRule) {
39  	    ((XPathRule) rule).evaluate(node, ctx);
40  	} else {
41  	    ((EcmascriptNode<?>) node).jjtAccept((EcmascriptParserVisitor) rule, ctx);
42  	}
43      }
44  }