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