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 / AstUtil.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.Assignment;
5 import com.google.javascript.rhino.head.ast.AstNode;
6 import com.google.javascript.rhino.head.ast.Comment;
7 import com.google.javascript.rhino.head.ast.FunctionNode;
8 import com.google.javascript.rhino.head.ast.ObjectProperty;
9
10 public class AstUtil {
11
12     private static final String PROTOTYPE_SUFFIX = ".prototype";
13
14     static boolean hasParentOfType(AstNode node, int tokenType) {
15         AstNode parent = node.getParent();
16         if (parent == null) {
17             return false;
18         }
19         return parent.getType() == tokenType;
20     }
21
22     static AstNode getFunctionNameNode(FunctionNode functionNode) {
23         AstNode nameNode = functionNode.getFunctionName();
24         if (nameNode != null) {
25             return nameNode;
26         }
27
28         if (AstUtil.hasParentOfType(functionNode, Token.COLON)) {
29             return ((ObjectProperty) functionNode.getParent()).getLeft();
30         }
31
32         if (AstUtil.hasParentOfType(functionNode, Token.ASSIGN)) {
33             Assignment assignment = (Assignment) functionNode.getParent();
34             if (assignment.getRight() == functionNode) {
35                 return assignment.getLeft();
36             }
37         }
38         return null;
39     }
40
41     static String getTypeNameFromPrototype(String value) {
42         return value.substring(0, value.length() - PROTOTYPE_SUFFIX.length());
43     }
44
45     static boolean isPrototypeName(String typeName) {
46         return typeName.endsWith(PROTOTYPE_SUFFIX);
47     }
48
49     static AstNode getAssignedTypeNameNode(Assignment assignment) {
50         AstNode typeNameNode = assignment.getLeft();
51         if (typeNameNode.getType() != Token.GETPROP && typeNameNode.getType() != Token.NAME) {
52             return null;
53         }
54         return typeNameNode;
55     }
56
57     static Comment getJsDocNode(AstNode node) {
58         if (node.getType() == Token.FUNCTION) {
59             return getJsDocNode((FunctionNode) node);
60         } else {
61             return node.getJsDocNode();
62         }
63     }
64
65     static Comment getJsDocNode(FunctionNode functionNode) {
66         Comment jsDocNode = functionNode.getJsDocNode();
67         if (jsDocNode != null) {
68             return jsDocNode;
69         }
70
71         // reader.onloadend = function() {...}
72         if (hasParentOfType(functionNode, Token.ASSIGN)) {
73             Assignment assignment = (Assignment) functionNode.getParent();
74             if (assignment.getRight() == functionNode) {
75                 jsDocNode = assignment.getJsDocNode();
76                 if (jsDocNode != null) {
77                     return jsDocNode;
78                 }
79             }
80         }
81
82         if (hasParentOfType(functionNode, Token.COLON)) {
83             jsDocNode = ((ObjectProperty) functionNode.getParent()).getLeft().getJsDocNode();
84             if (jsDocNode != null) {
85                 return jsDocNode;
86             }
87         }
88         return null;
89     }
90
91     private AstUtil() {}
92 }