1 package org.chromium.devtools.jsdoc.checks;
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;
10 public class AstUtil {
12 private static final String PROTOTYPE_SUFFIX = ".prototype";
14 static boolean hasParentOfType(AstNode node, int tokenType) {
15 AstNode parent = node.getParent();
19 return parent.getType() == tokenType;
22 static AstNode getFunctionNameNode(FunctionNode functionNode) {
23 AstNode nameNode = functionNode.getFunctionName();
24 if (nameNode != null) {
28 if (AstUtil.hasParentOfType(functionNode, Token.COLON)) {
29 return ((ObjectProperty) functionNode.getParent()).getLeft();
32 if (AstUtil.hasParentOfType(functionNode, Token.ASSIGN)) {
33 Assignment assignment = (Assignment) functionNode.getParent();
34 if (assignment.getRight() == functionNode) {
35 return assignment.getLeft();
41 static String getTypeNameFromPrototype(String value) {
42 return value.substring(0, value.length() - PROTOTYPE_SUFFIX.length());
45 static boolean isPrototypeName(String typeName) {
46 return typeName.endsWith(PROTOTYPE_SUFFIX);
49 static AstNode getAssignedTypeNameNode(Assignment assignment) {
50 AstNode typeNameNode = assignment.getLeft();
51 if (typeNameNode.getType() != Token.GETPROP && typeNameNode.getType() != Token.NAME) {
57 static Comment getJsDocNode(AstNode node) {
58 if (node.getType() == Token.FUNCTION) {
59 return getJsDocNode((FunctionNode) node);
61 return node.getJsDocNode();
65 static Comment getJsDocNode(FunctionNode functionNode) {
66 Comment jsDocNode = functionNode.getJsDocNode();
67 if (jsDocNode != null) {
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) {
82 if (hasParentOfType(functionNode, Token.COLON)) {
83 jsDocNode = ((ObjectProperty) functionNode.getParent()).getLeft().getJsDocNode();
84 if (jsDocNode != null) {