-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
CheckRequiresAndProvidesSortedTest.java
357 lines (328 loc) · 11.3 KB
/
CheckRequiresAndProvidesSortedTest.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
* Copyright 2015 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.lint;
import static com.google.javascript.jscomp.lint.CheckRequiresAndProvidesSorted.DUPLICATE_REQUIRE;
import static com.google.javascript.jscomp.lint.CheckRequiresAndProvidesSorted.PROVIDES_AFTER_REQUIRES;
import static com.google.javascript.jscomp.lint.CheckRequiresAndProvidesSorted.PROVIDES_NOT_SORTED;
import static com.google.javascript.jscomp.lint.CheckRequiresAndProvidesSorted.REQUIRES_NOT_SORTED;
import com.google.javascript.jscomp.Compiler;
import com.google.javascript.jscomp.CompilerOptions;
import com.google.javascript.jscomp.CompilerOptions.LanguageMode;
import com.google.javascript.jscomp.CompilerPass;
import com.google.javascript.jscomp.CompilerTestCase;
public final class CheckRequiresAndProvidesSortedTest extends CompilerTestCase {
@Override
protected CompilerPass getProcessor(Compiler compiler) {
return new CheckRequiresAndProvidesSorted(compiler);
}
@Override
protected int getNumRepetitions() {
return 1;
}
@Override
protected void setUp() throws Exception {
super.setUp();
setAcceptedLanguage(LanguageMode.ECMASCRIPT_2017);
}
@Override
protected CompilerOptions getOptions() {
CompilerOptions options = super.getOptions();
options.setPrettyPrint(true);
options.setPreserveTypeAnnotations(true);
return options;
}
public void testNoWarning_require() {
testNoWarning("goog.require('a.b');\ngoog.require('a.c')");
testNoWarning(
lines(
"goog.require('namespace');",
"goog.require('namespace.ExampleClass');",
"goog.require('namespace.ExampleClass.ExampleInnerClass');"));
testNoWarning(
lines(
"goog.require('namespace.Example');",
"goog.require('namespace.example');"));
}
public void testNoWarning_provide() {
testNoWarning("goog.provide('a.b');\ngoog.provide('a.c')");
testNoWarning(
lines(
"goog.provide('namespace');",
"goog.provide('namespace.ExampleClass');",
"goog.provide('namespace.ExampleClass.ExampleInnerClass');"));
testNoWarning(
lines(
"goog.provide('namespace.Example');",
"goog.provide('namespace.example');"));
}
public void testWarning_require() {
test(
srcs("goog.require('a.c');\ngoog.require('a.b')"),
warning(REQUIRES_NOT_SORTED).withMessageContaining(lines(
"The correct order is:",
"",
"goog.require('a.b');",
"goog.require('a.c');")));
test(
srcs("goog.require('a.c');\ngoog.require('a')"),
warning(REQUIRES_NOT_SORTED).withMessageContaining(lines(
"The correct order is:",
"",
"goog.require('a');",
"goog.require('a.c');")));
}
public void testWarning_requireWithJSDoc() {
test(
srcs(lines(
"goog.require('a.c');",
"/** @suppress {extraRequire} */",
"goog.require('a.b')")),
warning(REQUIRES_NOT_SORTED).withMessageContaining(lines(
"The correct order is:",
"",
"/**",
" @suppress {extraRequire}",
" */",
"goog.require('a.b');",
"goog.require('a.c');")));
}
public void testWarning_provide() {
test(
srcs("goog.provide('a.c');\ngoog.provide('a.b')"),
warning(PROVIDES_NOT_SORTED).withMessageContaining(lines(
"The correct order is:",
"",
"goog.provide('a.b');",
"goog.provide('a.c');")));
test(
srcs("goog.provide('a.c');\ngoog.provide('a')"),
warning(PROVIDES_NOT_SORTED).withMessageContaining(lines(
"The correct order is:",
"",
"goog.provide('a');",
"goog.provide('a.c');")));
}
public void testWarning_requiresFirst() {
test(
srcs("goog.require('a');\ngoog.provide('b')"),
warning(PROVIDES_AFTER_REQUIRES));
}
public void testB3473189() {
testNoWarning(
lines(
"goog.provide('abc');",
"if (typeof goog != 'undefined' && typeof goog.provide == 'function') {",
" goog.provide('MyLib.Base');",
"}"));
}
public void testGoogModule_shorthandAndStandalone() {
test(
srcs(lines(
"goog.module('m');",
"",
"goog.require('a.c');",
"const d = goog.require('a.b.d');",
"goog.require('a.b.c');",
"",
"alert(1);")),
warning(REQUIRES_NOT_SORTED).withMessageContaining(lines(
"The correct order is:",
"",
"const d = goog.require('a.b.d');",
"goog.require('a.b.c');",
"goog.require('a.c');")));
}
public void testGoogModule_destructuring() {
test(
srcs(lines(
"goog.module('m');",
"",
"goog.require('z');",
"goog.require('a');",
"var {someFunction, anotherFunction} = goog.require('example.utils');",
"var {A_CONST, ANOTHER_CONST} = goog.require('example.constants');",
"",
"alert(1);")),
warning(REQUIRES_NOT_SORTED).withMessageContaining(lines(
"The correct order is:",
"",
"var {A_CONST, ANOTHER_CONST} = goog.require('example.constants');",
"var {someFunction, anotherFunction} = goog.require('example.utils');",
"goog.require('a');",
"goog.require('z');")));
}
public void testGoogModule_emptyDestructuring() {
test(
srcs(lines(
"goog.module('m');",
"",
"const {FOO} = goog.require('example.constants');",
"const {} = goog.require('just.forthe.side.effects');",
"",
"alert(1);")),
warning(REQUIRES_NOT_SORTED).withMessageContaining(lines(
"The correct order is:",
"",
"const {} = goog.require('just.forthe.side.effects');",
"const {FOO} = goog.require('example.constants');")));
testNoWarning(
lines(
"goog.module('m');",
"",
"const {} = goog.require('just.forthe.side.effects');",
"const {FOO} = goog.require('example.constants');",
"",
"alert(1);"));
}
public void testGoogModule_allThreeStyles() {
test(
srcs(
lines(
"/** @fileoverview @suppress {extraRequire} */",
"goog.module('m');",
"",
"const shorthand2 = goog.require('a');",
"goog.require('standalone.two');",
"const {destructuring} = goog.require('c');",
"goog.require('standalone.one');",
"const shorthand1 = goog.require('b');")),
warning(REQUIRES_NOT_SORTED).withMessageContaining(lines(
"The correct order is:",
"",
"const shorthand1 = goog.require('b');",
"const shorthand2 = goog.require('a');",
"const {destructuring} = goog.require('c');",
"goog.require('standalone.one');",
"goog.require('standalone.two');")));
}
public void testGoogModule_shorthand() {
test(
srcs(lines(
"goog.module('m');",
"",
"var d = goog.require('a.b.d');",
"var c = goog.require('a.c');",
"",
"alert(1);")),
warning(REQUIRES_NOT_SORTED).withMessageContaining(lines(
"The correct order is:",
"",
"var c = goog.require('a.c');",
"var d = goog.require('a.b.d');")));
}
public void testGoogModule_shorthand_destructuring() {
test(
srcs(lines(
"goog.module('m');",
"",
"var a = goog.require('a.b.d');",
"var {b} = goog.require('a.a.a');",
"var c = goog.require('a.c');",
"",
"alert(1);")),
warning(REQUIRES_NOT_SORTED).withMessageContaining(lines(
"The correct order is:",
"",
"var a = goog.require('a.b.d');",
"var c = goog.require('a.c');",
"var {b} = goog.require('a.a.a');")));
}
public void testGoogModule_standalone() {
test(
srcs(
lines(
"goog.module('m');",
"",
"goog.require('a.c');",
"goog.require('a.b.d');",
"goog.require('a.b.c');",
"",
"alert(1);")),
warning(REQUIRES_NOT_SORTED).withMessageContaining(lines(
"The correct order is:",
"",
"goog.require('a.b.c');",
"goog.require('a.b.d');",
"goog.require('a.c');")));
}
public void testGoogModule_forwardDeclares() {
test(
srcs(lines(
"goog.module('x');",
"",
"const s = goog.require('s');",
"const f = goog.forwardDeclare('f');",
"const r = goog.require('r');")),
warning(REQUIRES_NOT_SORTED).withMessageContaining(lines(
"The correct order is:",
"",
"const r = goog.require('r');",
"const s = goog.require('s');",
"const f = goog.forwardDeclare('f');")));
}
public void testForwardDeclares() {
test(
srcs(
lines(
"goog.provide('x');",
"",
"goog.require('s');",
"goog.forwardDeclare('f');",
"goog.require('r');")),
warning(REQUIRES_NOT_SORTED).withMessageContaining(lines(
"The correct order is:",
"",
"goog.require('r');",
"goog.require('s');",
"goog.forwardDeclare('f');")));
}
public void testDuplicate() {
testWarning(
lines(
"goog.require('Bar');",
"goog.require('Bar');"),
DUPLICATE_REQUIRE);
}
public void testDuplicate_shorthand() {
testWarning(
lines(
"const Bar1 = goog.require('Bar');",
"const Bar2 = goog.require('Bar');"),
DUPLICATE_REQUIRE);
}
public void testDuplicate_destructuring() {
testWarning(
lines(
"const Bar = goog.require('Bar');",
"const {Foo} = goog.require('Bar');"),
DUPLICATE_REQUIRE);
}
// Just make sure we don't crash.
public void testEmptyRequire() {
testNoWarning("goog.require();");
}
// Compiler doesn't sort ES6 modules yet, because semantics not yet finalized.
// Simple test to make sure compiler does not crash.
public void testES6Modules() {
testNoWarning(
lines(
"import foo from 'bar';",
"import bar from 'foo';",
"import * as a from 'b';",
"import {a, b} from 'c';",
"import 'foobar';"));
}
}