daf5f765fbad7d6a7b712dbaab65bdc8cfba4bdd
[JavaForFun] /
1 package de.example.custom.javascript.checks;
2
3 import java.util.List;
4
5 import org.sonar.check.Rule;
6 import org.sonar.javascript.tree.impl.expression.CallExpressionTreeImpl;
7 import org.sonar.javascript.tree.impl.expression.DotMemberExpressionTreeImpl;
8 import org.sonar.javascript.tree.impl.expression.IdentifierTreeImpl;
9 import org.sonar.plugins.javascript.api.tree.Tree;
10 import org.sonar.plugins.javascript.api.tree.Tree.Kind;
11 import org.sonar.plugins.javascript.api.visitors.SubscriptionVisitorCheck;
12
13 import com.google.common.collect.ImmutableList;
14
15 @Rule(key = "GUJS0001")
16 public class AngularJSRootOnEventSubscriptionCheck extends SubscriptionVisitorCheck {
17         
18         @Override
19         public List<Kind> nodesToVisit() {
20                 return ImmutableList.of(Kind.CALL_EXPRESSION);
21         }
22
23         @Override
24         public void visitNode(Tree tree) {
25                 CallExpressionTreeImpl callExpression = (CallExpressionTreeImpl) tree;
26                 if (callExpression.callee() instanceof DotMemberExpressionTreeImpl) {
27                         DotMemberExpressionTreeImpl callee = (DotMemberExpressionTreeImpl) callExpression.callee();
28                         if (callee.object() instanceof IdentifierTreeImpl) {
29                                 IdentifierTreeImpl object = (IdentifierTreeImpl) callee.object();
30                                 String objectName = object.name();
31                                 String calleeName = callee.property().name();
32                                 if ("$rootScope".equals(objectName) && "$on".equals(calleeName)) {
33                                         addIssue(callExpression.getFirstToken(), "Do not use $rootScope.$on because it leaks the Controller instance.");
34                                 }
35                         }
36                 }
37
38         }
39
40 }