1 package de.example.custom.javascript.checks;
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;
13 import com.google.common.collect.ImmutableList;
15 @Rule(key = "GUJS0001")
16 public class AngularJSRootOnEventSubscriptionCheck extends SubscriptionVisitorCheck {
19 public List<Kind> nodesToVisit() {
20 return ImmutableList.of(Kind.CALL_EXPRESSION);
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.");