-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
PotentialDeclaration.java
231 lines (205 loc) · 7.52 KB
/
PotentialDeclaration.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* 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 static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.javascript.jscomp.AbstractCompiler;
import com.google.javascript.jscomp.NodeUtil;
import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.JSDocInfo;
import com.google.javascript.rhino.Node;
import javax.annotation.Nullable;
/**
* Encapsulates something that could be a declaration.
*
* This includes:
* var/let/const declarations,
* function/class declarations,
* method declarations,
* assignments,
* goog.define calls,
* and even valueless property accesses (e.g. `/** @type {number} * / Foo.prototype.bar`)
*/
abstract class PotentialDeclaration {
// The fully qualified name of the declaration.
private final String fullyQualifiedName;
// The LHS node of the declaration.
private final Node lhs;
// The RHS node of the declaration, if it exists.
private final @Nullable Node rhs;
private PotentialDeclaration(String fullyQualifiedName, Node lhs, @Nullable Node rhs) {
this.fullyQualifiedName = checkNotNull(fullyQualifiedName);
this.lhs = checkNotNull(lhs);
this.rhs = rhs;
}
static PotentialDeclaration fromName(Node nameNode) {
checkArgument(nameNode.isQualifiedName(), nameNode);
Node rhs = NodeUtil.getRValueOfLValue(nameNode);
String name =
ClassUtil.isThisProp(nameNode)
? ClassUtil.getPrototypeNameOfThisProp(nameNode)
: nameNode.getQualifiedName();
return new NameDeclaration(name, nameNode, rhs);
}
static PotentialDeclaration fromMethod(Node functionNode) {
checkArgument(ClassUtil.isClassMethod(functionNode));
String name = ClassUtil.getPrototypeNameOfMethod(functionNode);
return new MethodDeclaration(name, functionNode);
}
static PotentialDeclaration fromDefine(Node callNode) {
checkArgument(NodeUtil.isCallTo(callNode, "goog.define"));
return new DefineDeclaration(callNode);
}
String getFullyQualifiedName() {
return fullyQualifiedName;
}
Node getLhs() {
return lhs;
}
@Nullable
Node getRhs() {
return rhs;
}
Node getStatement() {
return NodeUtil.getEnclosingStatement(lhs);
}
JSDocInfo getJsDoc() {
return NodeUtil.getBestJSDocInfo(lhs);
}
private boolean isDetached() {
for (Node current = lhs; current != null; current = current.getParent()) {
if (current.isScript()) {
return false;
}
}
return true;
}
/**
* Remove this "potential declaration" completely.
* Usually, this is because the same symbol has already been declared in this file.
*/
void remove(AbstractCompiler compiler) {
if (isDetached()) {
return;
}
Node statement = getStatement();
NodeUtil.deleteNode(statement, compiler);
statement.removeChildren();
}
/**
* Simplify this declaration to only include what's necessary for typing.
* Usually, this means removing the RHS and leaving a type annotation.
*/
abstract void simplify(AbstractCompiler compiler);
/**
* A potential declaration that has a fully qualified name to describe it.
* This includes things like:
* var/let/const/function/class declarations,
* assignments to a fully qualfied name,
* and goog.module exports
* This is the most common type of potential declaration.
*/
static class NameDeclaration extends PotentialDeclaration {
NameDeclaration(String fullyQualifiedName, Node lhs, Node rhs) {
super(fullyQualifiedName, lhs, rhs);
}
@Override
void simplify(AbstractCompiler compiler) {
Node nameNode = getLhs();
JSDocInfo jsdoc = getJsDoc();
if (jsdoc != null && jsdoc.hasEnumParameterType()) {
// Remove values from enums
if (getRhs().isObjectLit() && getRhs().hasChildren()) {
for (Node key : getRhs().children()) {
removeStringKeyValue(key);
}
compiler.reportChangeToEnclosingScope(getRhs());
}
return;
}
if (NodeUtil.isNamespaceDecl(nameNode)) {
Node objLit = getRhs();
if (getRhs().isOr()) {
objLit = getRhs().getLastChild().detach();
getRhs().replaceWith(objLit);
compiler.reportChangeToEnclosingScope(nameNode);
}
if (objLit.hasChildren()) {
for (Node key : objLit.children()) {
if (!isTypedRhs(key.getLastChild())) {
removeStringKeyValue(key);
JsdocUtil.updateJsdoc(compiler, key);
compiler.reportChangeToEnclosingScope(key);
}
}
}
return;
}
if (nameNode.matchesQualifiedName("exports")) {
// Replace the RHS of a default goog.module export with Unknown
replaceRhsWithUnknown(getRhs());
compiler.reportChangeToEnclosingScope(nameNode);
return;
}
// Just completely remove the RHS, and replace with a getprop.
Node newStatement =
NodeUtil.newQNameDeclaration(compiler, nameNode.getQualifiedName(), null, jsdoc);
newStatement.useSourceInfoIfMissingFromForTree(nameNode);
Node oldStatement = getStatement();
NodeUtil.deleteChildren(oldStatement, compiler);
oldStatement.replaceWith(newStatement);
compiler.reportChangeToEnclosingScope(newStatement);
}
private static void replaceRhsWithUnknown(Node rhs) {
rhs.replaceWith(IR.cast(IR.number(0), JsdocUtil.getQmarkTypeJSDoc()).srcrefTree(rhs));
}
private static void removeStringKeyValue(Node stringKey) {
Node value = stringKey.getOnlyChild();
Node replacementValue = IR.number(0).srcrefTree(value);
stringKey.replaceChild(value, replacementValue);
}
}
/**
* A declaration declared by a call to `goog.define`. Note that a let, const, or var declaration
* annotated with @define in its JSDoc would be a NameDeclaration instead.
*/
private static class DefineDeclaration extends PotentialDeclaration {
DefineDeclaration(Node callNode) {
super(callNode.getSecondChild().getString(), callNode, callNode.getLastChild());
}
@Override
void simplify(AbstractCompiler compiler) {
NodeUtil.deleteNode(getLhs().getLastChild(), compiler);
}
}
/**
* A declaration of a method defined using the ES6 method syntax or goog.defineClass. Note that
* a method defined as an assignment to a prototype property would be a NameDeclaration instead.
*/
private static class MethodDeclaration extends PotentialDeclaration {
MethodDeclaration(String name, Node functionNode) {
super(name, functionNode.getParent(), functionNode);
}
@Override
void simplify(AbstractCompiler compiler) {}
}
static boolean isTypedRhs(Node rhs) {
return rhs.isFunction()
|| rhs.isClass()
|| (rhs.isQualifiedName() && rhs.matchesQualifiedName("goog.abstractMethod"))
|| (rhs.isQualifiedName() && rhs.matchesQualifiedName("goog.nullFunction"));
}
}