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;
9 import com.google.javascript.rhino.head.ast.VariableInitializer;
11 public class AstUtil {
13 private static final String PROTOTYPE_SUFFIX = ".prototype";
15 static AstNode parentOfType(AstNode node, int tokenType) {
16 AstNode parent = node.getParent();
17 return (parent == null || parent.getType() != tokenType) ? null : parent;
20 static AstNode getFunctionNameNode(FunctionNode functionNode) {
21 AstNode nameNode = functionNode.getFunctionName();
22 if (nameNode != null) {
26 AstNode parentNode = functionNode.getParent();
27 if (parentNode == null) {
31 switch (parentNode.getType()) {
33 return ((ObjectProperty) parentNode).getLeft();
35 Assignment assignment = (Assignment) parentNode;
36 if (assignment.getRight() == functionNode) {
37 return assignment.getLeft();
41 return ((VariableInitializer) parentNode).getTarget();
47 static String getTypeNameFromPrototype(String value) {
48 return value.substring(0, value.length() - PROTOTYPE_SUFFIX.length());
51 static boolean isPrototypeName(String typeName) {
52 return typeName.endsWith(PROTOTYPE_SUFFIX);
55 static AstNode getAssignedTypeNameNode(Assignment assignment) {
56 AstNode typeNameNode = assignment.getLeft();
57 if (typeNameNode.getType() != Token.GETPROP && typeNameNode.getType() != Token.NAME) {
63 static Comment getJsDocNode(AstNode node) {
64 if (node.getType() == Token.FUNCTION) {
65 return getJsDocNode((FunctionNode) node);
67 return node.getJsDocNode();
71 static Comment getJsDocNode(FunctionNode functionNode) {
72 Comment jsDocNode = functionNode.getJsDocNode();
73 if (jsDocNode != null) {
77 // reader.onloadend = function() {...}
78 AstNode parentNode = functionNode.getParent();
79 if (parentNode == null) {
83 switch (parentNode.getType()) {
85 return ((ObjectProperty) parentNode).getLeft().getJsDocNode();
87 Assignment assignment = (Assignment) parentNode;
88 if (assignment.getRight() == functionNode) {
89 return assignment.getJsDocNode();
93 return parentNode.getParent().getJsDocNode();