Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / scripts / jsdoc-validator / src / org / chromium / devtools / jsdoc / checks / RequiredThisAnnotationChecker.java
1 package org.chromium.devtools.jsdoc.checks;
2
3 import com.google.javascript.rhino.head.Token;
4 import com.google.javascript.rhino.head.ast.AstNode;
5 import com.google.javascript.rhino.head.ast.Comment;
6 import com.google.javascript.rhino.head.ast.FunctionNode;
7
8 import java.util.HashSet;
9 import java.util.Set;
10
11 public final class RequiredThisAnnotationChecker extends ContextTrackingChecker {
12
13     private final Set<FunctionRecord> functionsRequiringThisAnnotation = new HashSet<>();
14
15     @Override
16     void enterNode(AstNode node) {
17         if (node.getType() == Token.THIS) {
18             FunctionRecord function = getState().getCurrentFunctionRecord();
19             if (function == null) {
20                 return;
21             }
22             if (!function.isTopLevelFunction() && !function.isConstructor) {
23                 functionsRequiringThisAnnotation.add(function);
24             }
25             return;
26         }
27     }
28
29     @Override
30     void leaveNode(AstNode node) {
31         if (node.getType() != Token.FUNCTION) {
32             return;
33         }
34
35         ContextTrackingState state = getState();
36         FunctionRecord record = state.getCurrentFunctionRecord();
37         if (!functionsRequiringThisAnnotation.contains(record)) {
38             return;
39         }
40         FunctionNode functionNode = (FunctionNode) node;
41         AstNode functionNameNode = AstUtil.getFunctionNameNode(functionNode);
42         if (functionNameNode != null && shouldAddThisAnnotation(functionNode)) {
43             state.getContext().reportErrorInNode(functionNameNode, 0,
44                     "@this annotation is required for functions referencing 'this'");
45         }
46     }
47
48     private boolean shouldAddThisAnnotation(FunctionNode node) {
49         Comment comment = AstUtil.getJsDocNode(node);
50         return comment == null || !getContext().getNodeText(comment).contains("@this");
51     }
52 }