-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
JsdocUtil.java
135 lines (123 loc) · 4.33 KB
/
JsdocUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Copyright 2017 The Closure Compiler Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.javascript.jscomp.ijs;
import com.google.javascript.jscomp.NodeUtil;
import com.google.javascript.jscomp.Var;
import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.JSDocInfo;
import com.google.javascript.rhino.JSDocInfo.Visibility;
import com.google.javascript.rhino.JSDocInfoBuilder;
import com.google.javascript.rhino.JSTypeExpression;
import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.Token;
import javax.annotation.Nullable;
/**
* Static utility methods for dealing with inspecting and constructing JSDoc objects.
*/
final class JsdocUtil {
private JsdocUtil() {}
static boolean isPrivate(@Nullable JSDocInfo jsdoc) {
return jsdoc != null && jsdoc.getVisibility().equals(Visibility.PRIVATE);
}
static JSDocInfo getUnusableTypeJSDoc(JSDocInfo oldJSDoc) {
return getConstJSDoc(oldJSDoc, new Node(Token.STAR));
}
static JSDocInfo getQmarkTypeJSDoc() {
return getConstJSDoc(null, new Node(Token.QMARK));
}
private static JSTypeExpression asTypeExpression(Node typeAst) {
return new JSTypeExpression(typeAst, "<synthetic>");
}
private static JSDocInfo getConstJSDoc(JSDocInfo oldJSDoc, String contents) {
return getConstJSDoc(oldJSDoc, Node.newString(contents));
}
private static JSDocInfo getConstJSDoc(JSDocInfo oldJSDoc, Node typeAst) {
return getConstJSDoc(oldJSDoc, asTypeExpression(typeAst));
}
private static JSDocInfo getConstJSDoc(JSDocInfo oldJSDoc, JSTypeExpression newType) {
JSDocInfoBuilder builder = JSDocInfoBuilder.maybeCopyFrom(oldJSDoc);
builder.recordType(newType);
builder.recordConstancy();
return builder.build();
}
static boolean hasAnnotatedType(JSDocInfo jsdoc) {
if (jsdoc == null) {
return false;
}
return jsdoc.hasType()
|| jsdoc.hasReturnType()
|| jsdoc.getParameterCount() > 0
|| jsdoc.isConstructorOrInterface()
|| jsdoc.hasThisType()
|| jsdoc.hasEnumParameterType();
}
static JSDocInfo getJSDocForRhs(Node rhs, JSDocInfo oldJSDoc) {
switch (NodeUtil.getKnownValueType(rhs)) {
case BOOLEAN:
return getConstJSDoc(oldJSDoc, "boolean");
case NUMBER:
return getConstJSDoc(oldJSDoc, "number");
case STRING:
return getConstJSDoc(oldJSDoc, "string");
case NULL:
return getConstJSDoc(oldJSDoc, "null");
case VOID:
return getConstJSDoc(oldJSDoc, "void");
case OBJECT:
if (rhs.isRegExp()) {
return getConstJSDoc(oldJSDoc, new Node(Token.BANG, IR.string("RegExp")));
}
break;
case UNDETERMINED:
if (oldJSDoc != null && oldJSDoc.getDescription() != null) {
return getConstJSDoc(oldJSDoc, "string");
}
break;
}
return null;
}
static JSDocInfo getJSDocForName(Var decl, JSDocInfo oldJSDoc) {
if (decl == null) {
return null;
}
JSTypeExpression expr = NodeUtil.getDeclaredTypeExpression(decl.getNameNode());
if (expr == null) {
return null;
}
switch (expr.getRoot().getToken()) {
case EQUALS:
Node typeRoot = expr.getRoot().getFirstChild().cloneTree();
if (!decl.isDefaultParam()) {
typeRoot = new Node(Token.PIPE, typeRoot, IR.string("undefined"));
}
expr = asTypeExpression(typeRoot);
break;
case ELLIPSIS:
{
Node type = new Node(Token.BANG);
Node array = IR.string("Array");
type.addChildToBack(array);
Node block = new Node(Token.BLOCK, expr.getRoot().getFirstChild().cloneTree());
array.addChildToBack(block);
expr = asTypeExpression(type);
break;
}
default:
break;
}
return getConstJSDoc(oldJSDoc, expr);
}
}