-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
InferConstsTest.java
252 lines (213 loc) · 7.76 KB
/
InferConstsTest.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
* Copyright 2014 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;
import static com.google.common.truth.Truth.assertThat;
import com.google.javascript.jscomp.CompilerOptions.LanguageMode;
import com.google.javascript.rhino.Node;
import java.util.HashSet;
import java.util.Set;
/**
* Tests for {@link InferConsts}.
* @author tbreisacher@google.com (Tyler Breisacher)
*/
public final class InferConstsTest extends CompilerTestCase {
private FindConstants constFinder;
private String[] names;
@Override
public void setUp() {
setAcceptedLanguage(LanguageMode.ECMASCRIPT_2015);
}
@Override
public CompilerPass getProcessor(final Compiler compiler) {
constFinder = new FindConstants(names);
return new CompilerPass() {
@Override
public void process(Node externs, Node root) {
new InferConsts(compiler).process(externs, root);
NodeTraversal.traverseEs6(compiler, root, constFinder);
}
};
}
public void testSimple() {
testConsts("var x = 3;", "x");
testConsts("/** @const */ var x;", "x");
testConsts("var x = 3, y = 4;", "x", "y");
testConsts("var x = 3, y;", "x");
testConsts("var x = 3; function f(){x;}", "x");
}
public void testSimpleLetConst() {
testConsts("let x = 3, y", "x");
testConsts("let x = 3; let y = 4;", "x", "y");
testConsts("let x = 3, y = 4; x++;", "y");
testConsts("let x = 3; function f(){let x = 4;}", "x");
testConsts("/** @const */ let x;", "x");
testConsts("const x = 1;", "x");
}
public void testUnfound() {
testNotConsts("var x = 2; x++;", "x");
testNotConsts("var x = 2; x = 3;", "x");
testNotConsts("var x = 3; function f(){x++;}", "x");
testNotConsts("let x = 3; x++;", "x");
testNotConsts("let x = 3; x = 2;", "x", "y");
testNotConsts("/** @const */let x; let y;", "y");
testNotConsts("let x = 3; function f() {let x = 4; x++;} x++;", "x");
}
public void testForOf() {
testNotConsts("var x = 0; for (x of [1, 2, 3]) {}", "x");
testNotConsts("var x = 0; for (x of {a, b, c}) {}", "x");
}
public void testForIn() {
testNotConsts("var x = 0; for (x in {a, b}) {}", "x");
}
public void testForVar() {
testNotConsts("for (var x = 0; x < 2; x++) {}", "x");
testNotConsts("for (var x in [1, 2, 3]) {}", "x");
testNotConsts("for (var x of {a, b, c}) {}", "x");
}
public void testForLet() {
testNotConsts("for (let x = 0; x < 2; x++) {}", "x");
testNotConsts("for (let x in [1, 2, 3]) {}", "x");
testNotConsts("for (let x of {a, b, c}) {}", "x");
}
public void testForConst() {
// Using 'const' here is not allowed, and ConstCheck will warn for this
testConsts("for (const x = 0; x < 2; x++) {}", "x");
}
public void testForConst1() {
testConsts("for (const x in [1, 2, 3]) {}", "x");
testConsts("for (const x of {a, b, c}) {}", "x");
}
public void testForConstJSDoc() {
testConsts("for (/** @const */ let x = 0; x < 2; x++) {}", "x");
testConsts("for (/** @const */ let x in [1, 2, 3]) {}", "x");
testConsts("for (/** @const */ let x of {a, b, c}) {}", "x");
}
public void testFunctionParam() {
testConsts("var x = function(){};", "x");
testConsts("var x = ()=>{};", "x");
testConsts("const x = ()=>{};", "x");
testConsts("/** @const */ let x = ()=>{};", "x");
testConsts("function fn(a){var b = a + 1}; ", "a", "b");
testConsts("function fn(a = 1){var b = a + 1}; ", "a", "b");
testConsts("function fn(a, {b, c}){var d = a + 1}; ", "a", "b", "c", "d");
}
public void testClass() {
testConsts("var Foo = class {}", "Foo");
testConsts("const Foo = class {}", "Foo");
testConsts("class Foo {}", "Foo");
testConsts("var Foo = function() {};", "Foo");
testConsts("const Foo = function() {}", "Foo");
testConsts("function Foo() {}", "Foo");
}
public void testVarArguments() {
testConsts("var arguments = 3;", "arguments");
}
public void testConstArguments() {
testConsts("const arguments = 4;", "arguments");
}
public void testArgumentsJSDoc() {
testConsts("/** @const */let arguments = 5;", "arguments");
}
public void testDestructuring() {
testConsts("var [a, b, c] = [1, 2, 3];", "a", "b", "c");
testConsts("const [a, b, c] = [1, 2, 3];", "a", "b", "c");
testNotConsts("var [a, b, c] = obj;", "obj");
testNotConsts(""
+ "var [a, b, c] = [1, 2, 3];"
+ "[a, b, c] = [1, 2, 3];", "a", "b", "c");
testConsts(""
+ "var [a, b, c] = [1, 2, 3];"
+ "[a, b]= [1, 2];", "c");
testNotConsts("var [a, b, c] = [1, 2, 3]; [a, b] = [1, 2];", "a", "b");
testConsts("var [a, b, c] = [1, 2, 3]; [a, b] = [1, 2];", "c");
testConsts("var {a: b} = {a: 1}", "b");
testNotConsts("var {a: b} = {a: 1}", "a");
// Note that this "a" looks for the destructured "a"
testConsts("var obj = {a: 1}; var {a} = obj", "a");
testNotConsts(""
+ "var [{a: x} = {a: 'y'}] = [{a: 'x'}];"
+ "[{a: x} = {a: 'x'}] = {};", "x");
testNotConsts(""
+ "let fg = '', bg = '';"
+ "({fg, bg} = pal[val - 1]);", "fg", "bg");
testConsts("var [a, , b] = [1, 2, 3];", "a", "b");
testConsts("const [a, , b] = [1, 2, 3];", "a", "b");
}
public void testDefaultValue() {
testConsts("function fn(a = 1){}", "a");
testNotConsts("function fn(a = 1){a = 2}", "a");
testConsts("function fn({b, c} = {b:1, c:2}){}", "b", "c");
testNotConsts("function fn({b, c} = {b:1, c:2}){c = 1}", "c");
}
public void testVarInBlock() {
testConsts("function f() { if (true) { var x = function() {}; x(); } }", "x");
}
public void testGeneratorFunctionVar() {
testNotConsts(
LINE_JOINER.join(
"function *gen() {",
" var x = 0; ",
" while (x < 3)",
" yield x++;",
"}"),
"x");
}
public void testGeneratorFunctionConst() {
testConsts(
LINE_JOINER.join(
"function *gen() {",
" var x = 0;",
" yield x;",
"}"),
"x");
}
private void testConsts(String js, String... constants) {
testInferConstsHelper(true, js, constants);
}
private void testNotConsts(String js, String... constants) {
testInferConstsHelper(false, js, constants);
}
private void testInferConstsHelper(boolean constExpected, String js, String... constants) {
names = constants;
testSame(js);
for (String name : constants) {
if (constExpected) {
assertThat(constFinder.foundNodes).contains(name);
} else {
assertThat(constFinder.foundNodes).doesNotContain(name);
}
}
}
private static class FindConstants extends NodeTraversal.AbstractPostOrderCallback {
final String[] names;
final Set<String> foundNodes;
FindConstants(String[] names) {
this.names = names;
foundNodes = new HashSet<>();
}
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
for (String name : names) {
if ((n.matchesQualifiedName(name)
|| ((n.isStringKey() || n.isMemberFunctionDef())
&& n.getString().equals(name)))
&& n.getBooleanProp(Node.IS_CONSTANT_VAR)) {
foundNodes.add(name);
}
}
}
}
}