-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ErrorToFixMapperTest.java
729 lines (679 loc) · 21.2 KB
/
ErrorToFixMapperTest.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
/*
* 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.refactoring;
import static com.google.common.collect.ObjectArrays.concat;
import static com.google.common.truth.Truth.assertThat;
import static com.google.javascript.jscomp.CheckLevel.ERROR;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.javascript.jscomp.Compiler;
import com.google.javascript.jscomp.CompilerOptions;
import com.google.javascript.jscomp.DiagnosticGroups;
import com.google.javascript.jscomp.JSError;
import com.google.javascript.jscomp.SourceFile;
import java.util.Collection;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/**
* Test case for {@link ErrorToFixMapper}.
*/
@RunWith(JUnit4.class)
public class ErrorToFixMapperTest {
private static final Joiner LINE_JOINER = Joiner.on('\n');
private FixingErrorManager errorManager;
private CompilerOptions options;
private Compiler compiler;
@Before
public void setUp() {
errorManager = new FixingErrorManager();
compiler = new Compiler(errorManager);
compiler.disableThreads();
errorManager.setCompiler(compiler);
options = RefactoringDriver.getCompilerOptions();
options.setWarningLevel(DiagnosticGroups.CHECK_VARIABLES, ERROR);
options.setWarningLevel(DiagnosticGroups.DEBUGGER_STATEMENT_PRESENT, ERROR);
options.setWarningLevel(DiagnosticGroups.LINT_CHECKS, ERROR);
options.setWarningLevel(DiagnosticGroups.STRICT_MISSING_REQUIRE, ERROR);
options.setWarningLevel(DiagnosticGroups.EXTRA_REQUIRE, ERROR);
}
@Test
public void testDebugger() {
String code = LINE_JOINER.join(
"function f() {",
" debugger;",
"}");
String expectedCode = LINE_JOINER.join(
"function f() {",
" ",
"}");
assertChanges(code, expectedCode);
}
@Test
public void testRedeclaration() {
String code = "function f() { var x; var x; }";
String expectedCode = "function f() { var x; }";
assertChanges(code, expectedCode);
}
@Test
public void testRedeclaration_multipleVars1() {
String code = "function f() { var x; var x, y; }";
String expectedCode = "function f() { var x; var y; }";
assertChanges(code, expectedCode);
}
@Test
public void testRedeclaration_multipleVars2() {
String code = "function f() { var x; var y, x; }";
String expectedCode = "function f() { var x; var y; }";
assertChanges(code, expectedCode);
}
@Test
public void testRedeclaration_withValue() {
String code = LINE_JOINER.join(
"function f() {",
" var x;",
" var x = 0;",
"}");
String expectedCode = LINE_JOINER.join(
"function f() {",
" var x;",
" x = 0;",
"}");
assertChanges(code, expectedCode);
}
@Test
public void testRedeclaration_multipleVars_withValue1() {
String code = LINE_JOINER.join(
"function f() {",
" var x;",
" var x = 0, y;",
"}");
String expectedCode = LINE_JOINER.join(
"function f() {",
" var x;",
" x = 0;",
"var y;",
"}");
assertChanges(code, expectedCode);
}
@Test
public void testRedeclaration_multipleVars_withValue2() {
String code = LINE_JOINER.join(
"function f() {",
" var x;",
" var y, x = 0;",
"}");
String expectedCode = LINE_JOINER.join(
"function f() {",
" var x;",
" var y;",
"x = 0;",
"}");
assertChanges(code, expectedCode);
}
// Make sure the vars stay in the same order, so that in case the get*
// functions have side effects, we don't change the order they're called in.
@Test
public void testRedeclaration_multipleVars_withValue3() {
String code = LINE_JOINER.join(
"function f() {",
" var y;",
" var x = getX(), y = getY(), z = getZ();",
"}");
String expectedCode = LINE_JOINER.join(
"function f() {",
" var y;",
" var x = getX();",
"y = getY();",
"var z = getZ();",
"}");
assertChanges(code, expectedCode);
}
@Test
public void testRedeclaration_multipleVars_withValue4() {
String code = LINE_JOINER.join(
"function f() {",
" var x;",
" var x = getX(), y = getY(), z = getZ();",
"}");
String expectedCode = LINE_JOINER.join(
"function f() {",
" var x;",
" x = getX();",
"var y = getY(), z = getZ();",
"}");
assertChanges(code, expectedCode);
}
@Test
public void testRedeclaration_multipleVars_withValue5() {
String code = LINE_JOINER.join(
"function f() {",
" var z;",
" var x = getX(), y = getY(), z = getZ();",
"}");
String expectedCode = LINE_JOINER.join(
"function f() {",
" var z;",
" var x = getX(), y = getY();",
"z = getZ();",
"}");
assertChanges(code, expectedCode);
}
@Test
public void testRedeclarationOfParam() {
assertChanges("function f(x) { var x = 3; }", "function f(x) { x = 3; }");
}
@Test
public void testRedeclaration_params() {
assertNoChanges("function f(x, x) {}");
}
@Test
public void testEarlyReference() {
String code = "if (x < 0) alert(1);\nvar x;";
String expectedCode = "var x;\n" + code;
assertChanges(code, expectedCode);
}
@Test
public void testEarlyReferenceInFunction() {
String code = "function f() {\n if (x < 0) alert(1);\nvar x;\n}";
String expectedCode = "function f() {\n var x;\nif (x < 0) alert(1);\nvar x;\n}";
assertChanges(code, expectedCode);
}
@Test
public void testInsertSemicolon1() {
String code = "var x = 3";
String expectedCode = "var x = 3;";
assertChanges(code, expectedCode);
}
@Test
public void testInsertSemicolon2() {
String code = "function f() { return 'it' }";
String expectedCode = "function f() { return 'it'; }";
assertChanges(code, expectedCode);
}
@Test
public void testRequiresSorted1() {
assertChanges(
LINE_JOINER.join(
"/**",
" * @fileoverview",
" * @suppress {extraRequire}",
" */",
"",
"",
"goog.require('b');",
"goog.require('a');",
"goog.require('c');",
"",
"",
"alert(1);"),
LINE_JOINER.join(
"/**",
" * @fileoverview",
" * @suppress {extraRequire}",
" */",
"",
"",
"goog.require('a');",
"goog.require('b');",
"goog.require('c');",
"",
"",
"alert(1);"));
}
@Test
public void testRequiresSorted2() {
assertChanges(
LINE_JOINER.join(
"/**",
" * @fileoverview",
" * @suppress {extraRequire}",
" */",
"goog.provide('x');",
"",
"/** @suppress {extraRequire} */",
"goog.require('b');",
"goog.require('a');",
"",
"alert(1);"),
LINE_JOINER.join(
"/**",
" * @fileoverview",
" * @suppress {extraRequire}",
" */",
"goog.provide('x');",
"",
"goog.require('a');",
"/** @suppress {extraRequire} */",
"goog.require('b');",
"",
"alert(1);"));
}
@Test
public void testSortRequiresInGoogModule_let() {
assertChanges(
LINE_JOINER.join(
"goog.module('m');",
"",
"/** @suppress {extraRequire} */",
"goog.require('a.c');",
"/** @suppress {extraRequire} */",
"goog.require('a.b');",
"",
"let localVar;"),
LINE_JOINER.join(
"goog.module('m');",
"",
"/** @suppress {extraRequire} */",
"goog.require('a.b');",
"/** @suppress {extraRequire} */",
"goog.require('a.c');",
"",
"let localVar;"));
}
@Test
public void testSortRequiresInGoogModule_const() {
assertChanges(
LINE_JOINER.join(
"goog.module('m');",
"",
"/** @suppress {extraRequire} */",
"goog.require('a.c');",
"/** @suppress {extraRequire} */",
"goog.require('a.b');",
"",
"const FOO = 0;"),
LINE_JOINER.join(
"goog.module('m');",
"",
"/** @suppress {extraRequire} */",
"goog.require('a.b');",
"/** @suppress {extraRequire} */",
"goog.require('a.c');",
"",
"const FOO = 0;"));
}
/**
* Using this form in a goog.module is a violation of the style guide, but still fairly common.
*/
@Test
public void testSortRequiresInGoogModule_standalone() {
assertChanges(
LINE_JOINER.join(
"goog.module('m');",
"",
"goog.require('a.c');",
"goog.require('a.b.d');",
"goog.require('a.b.c');",
"",
"alert(a.c());",
"alert(a.b.d());",
"alert(a.b.c());"),
LINE_JOINER.join(
"goog.module('m');",
"",
"goog.require('a.b.c');",
"goog.require('a.b.d');",
"goog.require('a.c');",
"",
"alert(a.c());",
"alert(a.b.d());",
"alert(a.b.c());"));
}
@Test
public void testSortRequiresInGoogModule_shorthand() {
assertChanges(
LINE_JOINER.join(
"goog.module('m');",
"",
"var c2 = goog.require('a.c');",
"var d = goog.require('a.b.d');",
"var c1 = goog.require('a.b.c');",
"",
"alert(c1());",
"alert(d());",
"alert(c2());"),
LINE_JOINER.join(
"goog.module('m');",
"",
"var c1 = goog.require('a.b.c');",
"var c2 = goog.require('a.c');",
"var d = goog.require('a.b.d');",
"",
"alert(c1());",
"alert(d());",
"alert(c2());"));
}
@Test
public void testSortRequiresInGoogModule_destructuring() {
assertChanges(
LINE_JOINER.join(
"goog.module('m');",
"",
"const {fooBar} = goog.require('x');",
"const {foo, bar} = goog.require('y');"),
LINE_JOINER.join(
"goog.module('m');",
"",
"const {foo, bar} = goog.require('y');",
"const {fooBar} = goog.require('x');"));
}
@Test
public void testSortRequiresInGoogModule_shorthandAndStandalone() {
assertChanges(
LINE_JOINER.join(
"/** @fileoverview @suppress {extraRequire} */",
"goog.module('m');",
"",
"const shorthand2 = goog.require('a');",
"goog.require('standalone.two');",
"goog.require('standalone.one');",
"const shorthand1 = goog.require('b');"),
LINE_JOINER.join(
"/** @fileoverview @suppress {extraRequire} */",
"goog.module('m');",
"",
"const shorthand1 = goog.require('b');",
"const shorthand2 = goog.require('a');",
"goog.require('standalone.one');",
"goog.require('standalone.two');"));
}
@Test
public void testSortRequiresInGoogModule_allThreeStyles() {
assertChanges(
LINE_JOINER.join(
"/** @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');"),
LINE_JOINER.join(
"/** @fileoverview @suppress {extraRequire} */",
"goog.module('m');",
"",
"const shorthand1 = goog.require('b');",
"const shorthand2 = goog.require('a');",
"const {destructuring} = goog.require('c');",
"goog.require('standalone.one');",
"goog.require('standalone.two');"));
}
@Test
public void testMissingRequireInGoogProvideFile() {
assertChanges(
LINE_JOINER.join(
"goog.provide('p');",
"",
"alert(new a.b.C());"),
LINE_JOINER.join(
"goog.provide('p');",
"goog.require('a.b.C');",
"",
"alert(new a.b.C());"));
}
@Test
public void testMissingRequireInGoogModule() {
assertChanges(
LINE_JOINER.join(
"goog.module('m');",
"",
"alert(new a.b.C());"),
LINE_JOINER.join(
"goog.module('m');",
"var C = goog.require('a.b.C');",
"",
"alert(new C());"));
}
@Test
public void testMissingRequireInGoogModuleTwice() {
assertChanges(
LINE_JOINER.join(
"goog.module('m');",
"",
"alert(new a.b.C());",
"alert(new a.b.C());"),
LINE_JOINER.join(
"goog.module('m');",
"var C = goog.require('a.b.C');",
"",
// TODO(tbreisacher): Can we make automatically switch both lines to use 'new C()'?
"alert(new a.b.C());",
"alert(new C());"));
}
@Test
public void testMissingRequireInGoogModule_call() {
assertChanges(
LINE_JOINER.join(
"goog.module('m');",
"",
"alert(a.b.c());"),
LINE_JOINER.join(
"goog.module('m');",
"var b = goog.require('a.b');",
"",
"alert(b.c());"));
}
@Test
public void testMissingRequireInGoogModule_extends() {
assertChanges(
LINE_JOINER.join(
"goog.module('m');",
"",
"class Cat extends world.util.Animal {}"),
LINE_JOINER.join(
"goog.module('m');",
"var Animal = goog.require('world.util.Animal');",
"",
"class Cat extends Animal {}"));
}
@Test
public void testMissingRequireInGoogModule_atExtends() {
assertChanges(
LINE_JOINER.join(
"goog.module('m');",
"",
"/** @constructor @extends {world.util.Animal} */",
"function Cat() {}"),
LINE_JOINER.join(
"goog.module('m');",
"var Animal = goog.require('world.util.Animal');",
"",
// TODO(tbreisacher): Change this to "@extends {Animal}"
"/** @constructor @extends {world.util.Animal} */",
"function Cat() {}"));
}
@Test
public void testMissingRequireInGoogModule_atExtends_qname() {
assertChanges(
LINE_JOINER.join(
"goog.module('m');",
"",
"/** @constructor @extends {world.util.Animal} */",
"world.util.Cat = function() {};"),
LINE_JOINER.join(
"goog.module('m');",
"var Animal = goog.require('world.util.Animal');",
"",
// TODO(tbreisacher): Change this to "@extends {Animal}"
"/** @constructor @extends {world.util.Animal} */",
"world.util.Cat = function() {};"));
}
@Test
public void testMissingRequireInGoogModule_insertedInCorrectOrder() {
assertChanges(
LINE_JOINER.join(
"goog.module('m');",
"",
"var A = goog.require('a.A');",
"var C = goog.require('c.C');",
"",
"alert(new A(new x.B(new C())));"),
LINE_JOINER.join(
"goog.module('m');",
"",
// Requires are sorted by the short name, not the full namespace.
"var A = goog.require('a.A');",
"var B = goog.require('x.B');",
"var C = goog.require('c.C');",
"",
"alert(new A(new B(new C())));"));
}
@Test
public void testMissingRequireInGoogModule_alwaysInsertsVar() {
assertChanges(
LINE_JOINER.join(
"goog.module('m');",
"",
"const A = goog.require('a.A');",
"const C = goog.require('c.C');",
"",
"alert(new A(new x.B(new C())));"),
LINE_JOINER.join(
"goog.module('m');",
"",
"const A = goog.require('a.A');",
// TODO(tbreisacher): Switch to const once ES6+ is on by default everywhere.
"var B = goog.require('x.B');",
"const C = goog.require('c.C');",
"",
"alert(new A(new B(new C())));"));
}
@Test
public void testSortShorthandRequiresInGoogModule() {
assertChanges(
LINE_JOINER.join(
"goog.module('m');",
"",
"var B = goog.require('x.B');",
"var A = goog.require('a.A');",
"var C = goog.require('c.C');",
"",
"alert(new A(new B(new C())));"),
LINE_JOINER.join(
"goog.module('m');",
"",
// Requires are sorted by the short name, not the full namespace.
"var A = goog.require('a.A');",
"var B = goog.require('x.B');",
"var C = goog.require('c.C');",
"",
"alert(new A(new B(new C())));"));
}
@Test
public void testShortRequireInGoogModule() {
assertChanges(
LINE_JOINER.join(
"goog.module('m');",
"",
"var c = goog.require('a.b.c');",
"",
"alert(a.b.c);"),
LINE_JOINER.join(
"goog.module('m');",
"",
"var c = goog.require('a.b.c');",
"",
"alert(c);"));
}
@Test
public void testProvidesSorted1() {
assertChanges(
LINE_JOINER.join(
"/** @fileoverview foo */",
"",
"",
"goog.provide('b');",
"goog.provide('a');",
"goog.provide('c');",
"",
"",
"alert(1);"),
LINE_JOINER.join(
"/** @fileoverview foo */",
"",
"",
"goog.provide('a');",
"goog.provide('b');",
"goog.provide('c');",
"",
"",
"alert(1);"));
}
@Test
public void testExtraRequire() {
assertChanges(
LINE_JOINER.join(
"goog.require('goog.object');",
"goog.require('goog.string');",
"",
"alert(goog.string.parseInt('7'));"),
LINE_JOINER.join(
"goog.require('goog.string');",
"",
"alert(goog.string.parseInt('7'));"));
}
@Test
public void testDuplicateRequire() {
assertChanges(
LINE_JOINER.join(
"goog.require('goog.string');",
"goog.require('goog.string');",
"",
"alert(goog.string.parseInt('7'));"),
LINE_JOINER.join(
"goog.require('goog.string');",
"",
"alert(goog.string.parseInt('7'));"));
}
@Test
public void testDuplicateRequire_shorthand() {
// We could provide a fix here, eliminating the later require. But then we'd need to switch
// str to googString in the last line. Probably not worth it.
assertNoChanges(
LINE_JOINER.join(
"const googString = goog.require('goog.string');",
"const str = goog.require('goog.string');",
"",
"alert(googString.parseInt('7'));",
"alert(str.parseInt('8'));"));
}
private void assertChanges(String originalCode, String expectedCode) {
compiler.compile(
ImmutableList.<SourceFile>of(), // Externs
ImmutableList.of(SourceFile.fromCode("test", originalCode)),
options);
JSError[] warningsAndErrors =
concat(compiler.getWarnings(), compiler.getErrors(), JSError.class);
assertThat(warningsAndErrors).named("warnings/errors").isNotEmpty();
Collection<SuggestedFix> fixes = errorManager.getAllFixes();
assertThat(fixes).named("fixes").isNotEmpty();
String newCode = ApplySuggestedFixes.applySuggestedFixesToCode(
fixes, ImmutableMap.of("test", originalCode)).get("test");
assertThat(newCode).isEqualTo(expectedCode);
}
protected void assertNoChanges(String originalCode) {
compiler.compile(
ImmutableList.<SourceFile>of(), // Externs
ImmutableList.of(SourceFile.fromCode("test", originalCode)),
options);
Collection<SuggestedFix> fixes = errorManager.getAllFixes();
assertThat(fixes).isEmpty();
}
}