/
core.js
2193 lines (1798 loc) · 58.9 KB
/
core.js
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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
var JSHINT = require("../..").JSHINT;
var fs = require('fs');
var TestRun = require("../helpers/testhelper").setup.testRun;
/**
* JSHint allows you to specify custom globals as a parameter to the JSHINT
* function so it is not necessary to spam code with jshint-related comments
*/
exports.testCustomGlobals = function (test) {
var code = '(function (test) { return [ fooGlobal, barGlobal ]; }());';
var custom = { fooGlobal: false, barGlobal: false };
test.ok(JSHINT(code, {}, custom));
var report = JSHINT.data();
test.strictEqual(report.implieds, undefined);
test.equal(report.globals.length, 2);
var dict = {};
for (var i = 0, g; g = report.globals[i]; i += 1)
dict[g] = true;
var customKeys = Object.keys(custom);
for (i = 0, g = null; g = customKeys[i]; i += 1)
test.ok(g in dict);
// Regression test (GH-665)
code = [
"/*global bar*/",
"foo = {};",
"bar = {};"
];
TestRun(test)
.addError(2, "Read only.")
.addError(3, "Read only.")
.test(code, { es3: true, unused: true, predef: { foo: false }});
test.done();
};
exports.testUnusedDefinedGlobals = function (test) {
var src = fs.readFileSync(__dirname + "/fixtures/unusedglobals.js", "utf8");
TestRun(test)
.addError(2, "'bar' is defined but never used.")
.test(src, { es3: true, unused: true });
test.done();
};
exports.testImplieds = function (test) {
var src = [
"f = 0;",
"(function() {",
" g = 0;",
"}());",
"h = 0;"
];
var report;
TestRun(test).test(src);
report = JSHINT.data();
test.deepEqual(
report.implieds,
[
{ name: "f", line: [1] },
{ name: "g", line: [3] },
{ name: "h", line: [5] }
]
);
TestRun(test)
.test("__proto__ = 0;", { proto: true });
report = JSHINT.data();
test.deepEqual(report.implieds, [ { name: "__proto__", line: [1] } ]);
test.done();
};
exports.testExportedDefinedGlobals = function (test) {
var src = ["/*global foo, bar */",
"export { bar, foo };"];
// Test should pass
TestRun(test).test(src, { esnext: true, unused: true }, {});
var report = JSHINT.data();
test.deepEqual(report.globals, ['bar', 'foo']);
test.done();
};
exports.testGlobalVarDeclarations = function (test) {
var src = "var a;";
TestRun(test).test(src, { es3: true }, {});
var report = JSHINT.data();
test.deepEqual(report.globals, ['a']);
TestRun(test).test(src, { es3: true, node: true }, {});
report = JSHINT.data();
test.strictEqual(report.globals, undefined);
TestRun(test).test("var __proto__;", { proto: true });
report = JSHINT.data();
test.deepEqual(report.globals, ["__proto__"]);
test.done();
};
exports.globalDeclarations = function (test) {
var src = "exports = module.exports = function (test) {};";
// Test should pass
TestRun(test).test(src, { es3: true, node: true }, { exports: true });
// Test should pass as well
src = [
"/*jshint node:true */",
"/*global exports:true */",
"exports = module.exports = function (test) {};"
];
TestRun(test).test(src.join('\n'));
test.done();
};
exports.multilineGlobalDeclarations = function (test) {
var src = fs.readFileSync(__dirname + "/fixtures/multiline-global-declarations.js", "utf8");
TestRun(test)
.addError(12, "'pi' is defined but never used.")
.test(src, { unused: true });
test.done();
};
/** Test that JSHint recognizes `new Array(<expr>)` as a valid expression */
exports.testNewArray = function (test) {
var code = 'new Array(1);';
var code1 = 'new Array(v + 1);';
var code2 = 'new Array("hello", "there", "chaps");';
TestRun(test).test(code);
TestRun(test).test(code1);
TestRun(test).test(code2);
TestRun(test)
.addError(1, "The array literal notation [] is preferable.")
.test('new Array();');
test.done();
};
/** Test that JSHint recognizes `new foo.Array(<expr>)` as a valid expression #527 **/
exports.testNewNonNativeArray = function (test) {
var code = 'new foo.Array();';
var code1 = 'new foo.Array(1);';
var code2 = 'new foo.Array(v + 1);';
var code3 = 'new foo.Array("hello", "there", "chaps");';
TestRun(test).test(code);
TestRun(test).test(code1);
TestRun(test).test(code2);
TestRun(test).test(code3);
test.done();
};
exports.testNonNativeArray = function (test) {
var code1 = 'foo.Array();';
var code2 = 'foo.Array(v + 1);';
var code3 = 'foo.Array("hello", "there", "chaps");';
TestRun(test).test(code1);
TestRun(test).test(code2);
TestRun(test).test(code3);
test.done();
};
/** Test that JSHint recognizes `new Object(<expr>)` as a valid expression */
exports.testNewObject = function (test) {
var code = 'Object(1);';
var code1 = 'new Object(1);';
TestRun(test).test(code);
TestRun(test).test(code1);
TestRun(test)
.addError(1, "The object literal notation {} is preferable.")
.test('Object();');
TestRun(test)
.addError(1, "The object literal notation {} is preferable.")
.test('new Object();');
test.done();
};
/** Test that JSHint recognizes `new foo.Object(<expr>)` as a valid expression #527 **/
exports.testNewNonNativeObject = function (test) {
var code = 'new foo.Object();';
var code1 = 'new foo.Object(1);';
var code2 = 'foo.Object();';
var code3 = 'foo.Object(1);';
TestRun(test).test(code);
TestRun(test).test(code1);
TestRun(test).test(code2);
TestRun(test).test(code3);
test.done();
};
/**
* Test that JSHint allows `undefined` to be a function parameter.
* It is a common pattern to protect against the case when somebody
* overwrites undefined. It also helps with minification.
*
* More info: https://gist.github.com/315916
*/
exports.testUndefinedAsParam = function (test) {
var code = '(function (undefined) {}());';
var code1 = 'var undefined = 1;';
TestRun(test).test(code);
// But it must never tolerate reassigning of undefined
TestRun(test)
.addError(1, "Expected an identifier and instead saw 'undefined' (a reserved word).")
.test(code1);
test.done();
};
/** Tests that JSHint accepts new line after a dot (.) operator */
exports.newLineAfterDot = function (test) {
TestRun(test).test([ "chain().chain().", "chain();" ]);
test.done();
};
/**
* JSHint does not tolerate deleting variables.
* More info: http://perfectionkills.com/understanding-delete/
*/
exports.noDelete = function (test) {
TestRun(test)
.addError(1, 'Variables should not be deleted.')
.test('delete NullReference;');
test.done();
};
/**
* JSHint allows case statement fall through only when it is made explicit
* using special comments.
*/
exports.switchFallThrough = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/switchFallThrough.js', 'utf8');
TestRun(test)
.addError(3, "Expected a 'break' statement before 'case'.")
.addError(18, "Expected a 'break' statement before 'default'.")
.addError(40, "Unexpected ':'.")
.test(src);
test.done();
};
// GH-490: JSHint shouldn't require break before default if default is
// the first switch statement.
exports.switchDefaultFirst = function (test) {
var src = fs.readFileSync(__dirname + "/fixtures/switchDefaultFirst.js", "utf8");
TestRun(test)
.addError(5, "Expected a 'break' statement before 'default'.")
.test(src);
test.done();
};
exports.testVoid = function (test) {
var code = [
"void(0);",
"void 0;",
"var a = void(1);"
];
TestRun(test).test(code);
test.done();
};
exports.functionScopedOptions = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/functionScopedOptions.js', 'utf8');
TestRun(test)
.addError(1, "eval can be harmful.")
.addError(8, "eval can be harmful.")
.test(src);
test.done();
};
/** JSHint should not only read jshint, but also jslint options */
exports.jslintOptions = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/jslintOptions.js', 'utf8');
TestRun(test).test(src);
test.done();
};
exports.jslintInverted = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/jslintInverted.js', 'utf8');
TestRun(test).test(src);
test.done();
};
exports.jslintRenamed = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/jslintRenamed.js', 'utf8');
TestRun(test)
.addError(4, "Expected '===' and instead saw '=='.")
.test(src);
test.done();
};
exports.jslintSloppy = function (test) {
var src = "/*jslint sloppy:true */ function test() { return 1; }";
TestRun(test)
.test(src);
test.done();
};
/** JSHint should ignore unrecognized jslint options */
exports.jslintUnrecognized = function (test) {
var src = "/*jslint closure:true */ function test() { return 1; }";
TestRun(test)
.test(src);
test.done();
};
exports.caseExpressions = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/caseExpressions.js', 'utf8');
TestRun(test)
.test(src);
test.done();
};
exports.returnStatement = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/return.js', 'utf8');
TestRun(test)
.addError(3, "Did you mean to return a conditional instead of an assignment?")
.addError(38, "Line breaking error 'return'.")
.addError(38, "Missing semicolon.")
.addError(39, "Unnecessary semicolon.")
.test(src, { es3: true });
test.done();
};
exports.argsInCatchReused = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/trycatch.js', 'utf8');
TestRun(test)
.addError(6, "'e' is already defined.")
.addError(12, "Do not assign to the exception parameter.")
.addError(13, "Do not assign to the exception parameter.")
.addError(24, "'e' is not defined.")
.test(src, { es3: true, undef: true });
test.done();
};
exports.testRawOnError = function (test) {
JSHINT(';', { maxerr: 1 });
test.equal(JSHINT.errors[0].raw, 'Unnecessary semicolon.');
test.equal(JSHINT.errors[1].raw, 'Too many errors.');
test.equal(JSHINT.errors[2], null);
test.done();
};
exports.yesEmptyStmt = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/emptystmt.js', 'utf8');
TestRun(test)
.addError(1, "Expected an identifier and instead saw ';'.")
.addError(6, "Expected an assignment or function call and instead saw an expression.")
.addError(10, "Unnecessary semicolon.")
.addError(17, "Unnecessary semicolon.")
.test(src, { es3: true, curly: false });
TestRun(test)
.addError(1, "Expected an identifier and instead saw ';'.")
.addError(10, "Unnecessary semicolon.")
.addError(17, "Unnecessary semicolon.")
.test(src, { es3: true, curly: false, expr: true });
test.done();
};
exports.insideEval = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/insideEval.js', 'utf8');
TestRun(test)
.addError(1, "eval can be harmful.")
.addError(3, "eval can be harmful.")
.addError(5, "eval can be harmful.")
.addError(7, "eval can be harmful.")
.addError(9, "eval can be harmful.")
.addError(11, "Implied eval. Consider passing a function instead of a string.")
.addError(13, "Implied eval. Consider passing a function instead of a string.")
.addError(15, "Implied eval. Consider passing a function instead of a string.")
.addError(17, "Implied eval. Consider passing a function instead of a string.")
// The "TestRun" class (and these errors) probably needs some
// facility for checking the expected scope of the error
.addError(13, "Unexpected early end of program.")
.addError(13, "Unrecoverable syntax error. (100% scanned).")
.addError(17, "Unexpected early end of program.")
.addError(17, "Unrecoverable syntax error. (100% scanned).")
.test(src, { es3: true, evil: false });
// Regression test for bug GH-714.
JSHINT(src, { evil: false, maxerr: 1 });
var err = JSHINT.data().errors[1];
test.equal(err.raw, "Too many errors.");
test.equal(err.scope, "(main)");
test.done();
};
exports.escapedEvil = function (test) {
var code = [
"\\u0065val(\"'test'\");"
];
TestRun(test)
.addError(1, "eval can be harmful.")
.test(code, { evil: false });
test.done();
};
// Regression test for GH-394.
exports.noExcOnTooManyUndefined = function (test) {
var code = 'a(); b();';
try {
JSHINT(code, {undef: true, maxerr: 1});
} catch (e) {
test.ok(false, 'Exception was thrown');
}
TestRun(test)
.addError(1, "'a' is not defined.")
.addError(1, "'b' is not defined.")
.test(code, { es3: true, undef: true });
test.done();
};
exports.defensiveSemicolon = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/gh-226.js', 'utf8');
TestRun(test)
.addError(16, "Unnecessary semicolon.")
.addError(17, "Unnecessary semicolon.")
.test(src, { es3: true, expr: true, laxbreak: true });
test.done();
};
// Test different variants of IIFE
exports.iife = function (test) {
var iife = [
'(function (test) { return; }());',
'(function (test) { return; })();'
];
TestRun(test).test(iife.join('\n'));
test.done();
};
// Tests invalid options when they're passed as function arguments
// For code that tests /*jshint ... */ see parser.js
exports.invalidOptions = function (test) {
TestRun(test)
.addError(0, "Bad option: 'invalid'.")
.test("function test() {}", { es3: true, devel: true, invalid: true });
test.done();
};
exports.multilineArray = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/gh-334.js', 'utf8');
TestRun(test).test(src);
test.done();
};
exports.testInvalidSource = function (test) {
TestRun(test)
.addError(0, "Input is neither a string nor an array of strings.")
.test(undefined);
TestRun(test)
.addError(0, "Input is neither a string nor an array of strings.")
.test(null);
TestRun(test)
.addError(0, "Input is neither a string nor an array of strings.")
.test({}, {es3: true});
TestRun(test)
.test("", {es3: true});
TestRun(test)
.test([], {es3: true});
test.done();
};
exports.testConstructor = function (test) {
var code = "new Number(5);";
TestRun(test)
.addError(1, "Do not use Number as a constructor.", {
character: 1
})
.test(code, {es3: true});
test.done();
};
exports.missingRadix = function (test) {
var code = "parseInt(20);";
TestRun(test)
.addError(1, "Missing radix parameter.", {
character: 12
})
.test(code, {es3: true});
TestRun(test).test(code);
test.done();
};
exports.NumberNaN = function (test) {
var code = "(function (test) { return Number.NaN; })();";
TestRun(test).test(code, {es3: true});
test.done();
};
exports.htmlEscapement = function (test) {
TestRun(test).test("var a = '<\\!--';", {es3: true});
TestRun(test)
.test("var a = '\\!';", {es3: true});
test.done();
};
// GH-551 regression test.
exports.testSparseArrays = function (test) {
var src = "var arr = ['a',, null,, '',, undefined,,];";
TestRun(test)
.addError(1, "Extra comma. (it breaks older versions of IE)")
.addError(1, "Extra comma. (it breaks older versions of IE)")
.addError(1, "Extra comma. (it breaks older versions of IE)")
.addError(1, "Extra comma. (it breaks older versions of IE)")
.test(src, {es3: true});
TestRun(test)
.test(src, { elision: true }); // es5
test.done();
};
exports.testReserved = function (test) {
var src = fs.readFileSync(__dirname + "/fixtures/reserved.js", "utf8");
TestRun(test)
.addError(1, "Expected an identifier and instead saw 'volatile' (a reserved word).")
.addError(5, "Expected an identifier and instead saw 'let' (a reserved word).")
.addError(10, "Expected an identifier and instead saw 'let' (a reserved word).")
.addError(13, "Expected an identifier and instead saw 'class' (a reserved word).")
.addError(14, "Expected an identifier and instead saw 'else' (a reserved word).")
.addError(15, "Expected an identifier and instead saw 'protected' (a reserved word).")
.test(src, {es3: true});
TestRun(test)
.addError(5, "Expected an identifier and instead saw 'let' (a reserved word).")
.addError(10, "Expected an identifier and instead saw 'let' (a reserved word).")
.test(src, {}); // es5
test.done();
};
// GH-744: Prohibit the use of reserved words as non-property
// identifiers.
exports.testES5Reserved = function (test) {
var src = fs.readFileSync(__dirname + "/fixtures/es5Reserved.js", "utf8");
TestRun(test)
.addError(2, "Expected an identifier and instead saw 'default' (a reserved word).")
.addError(3, "Unexpected 'in'.")
.addError(3, "Expected an identifier and instead saw 'in' (a reserved word).")
.addError(6, "Expected an identifier and instead saw 'default' (a reserved word).")
.addError(7, "Expected an identifier and instead saw 'new' (a reserved word).")
.addError(8, "Expected an identifier and instead saw 'class' (a reserved word).")
.addError(9, "Expected an identifier and instead saw 'default' (a reserved word).")
.addError(10, "Expected an identifier and instead saw 'in' (a reserved word).")
.addError(11, "Expected an identifier and instead saw 'in' (a reserved word).")
.test(src, {es3: true});
TestRun(test)
.addError(6, "Expected an identifier and instead saw 'default' (a reserved word).")
.addError(7, "Expected an identifier and instead saw 'new' (a reserved word).")
.addError(8, "Expected an identifier and instead saw 'class' (a reserved word).")
.addError(11, "Expected an identifier and instead saw 'in' (a reserved word).")
.test(src, {}); // es5
test.done();
};
exports.testCatchBlocks = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/gh247.js', 'utf8');
TestRun(test)
.addError(19, "'w' is already defined.")
.addError(35, "'u2' used out of scope.")
.addError(36, "'w2' used out of scope.")
.test(src, { es3: true, undef: true, devel: true });
src = fs.readFileSync(__dirname + '/fixtures/gh618.js', 'utf8');
TestRun(test)
.addError(5, "Value of 'x' may be overwritten in IE 8 and earlier.")
.addError(15, "Value of 'y' may be overwritten in IE 8 and earlier.")
.test(src, { es3: true, undef: true, devel: true });
TestRun(test)
.test(src, { es3: true, undef: true, devel: true, node: true });
var code = "try {} catch ({ message }) {}";
TestRun(test, "destructuring in catch blocks' parameter")
.test(code, { esnext: true });
test.done();
};
exports.testNumericParams = function (test) {
TestRun(test)
.test("/*jshint maxparams:4, indent:3, maxlen:false */");
TestRun(test)
.addError(1, "Expected a small integer or 'false' and instead saw 'face'.")
.test("/*jshint maxparams:face */");
test.done();
};
exports.testForIn = function (test) {
var src = [
"(function (o) {",
"for (var i in o) { i(); }",
"}());"
];
TestRun(test)
.test(src, {es3: true});
src = [
"(function (o) {",
"for (i in o) { i(); }",
"}());"
];
TestRun(test)
.addError(2, "Creating global 'for' variable. Should be 'for (var i ...'.")
.test(src, {es3: true});
src = [
"(function (o) {",
"for ('i' in o) { i(); }",
"}());"
];
TestRun(test)
.addError(2, "Expected an identifier and instead saw 'i'.")
.test(src);
src = [
"(function (o) {",
"for (i, j in o) { i(); }",
"for (var x, u in o) { x(); }",
"for (z = 0 in o) { z(); }",
"for (var q = 0 in o) { q(); }",
"})();"
];
TestRun(test, "bad lhs errors")
.addError(2, "Invalid for-in loop left-hand-side: more than one ForBinding.")
.addError(3, "Invalid for-in loop left-hand-side: more than one ForBinding.")
.addError(4, "Invalid for-in loop left-hand-side: initializer is forbidden.")
.addError(5, "Invalid for-in loop left-hand-side: initializer is forbidden.")
.test(src);
src = [
"(function (o) {",
"for (let i, j in o) { i(); }",
"for (const x, u in o) { x(); }",
"for (let z = 0 in o) { z(); }",
"for (const q = 0 in o) { q(); }",
"})();"
];
TestRun(test, "bad lhs errors (lexical)")
.addError(2, "Invalid for-in loop left-hand-side: more than one ForBinding.")
.addError(3, "Invalid for-in loop left-hand-side: more than one ForBinding.")
.addError(4, "Invalid for-in loop left-hand-side: initializer is forbidden.")
.addError(5, "Invalid for-in loop left-hand-side: initializer is forbidden.")
.test(src, { esnext: true });
test.done();
};
exports.testRegexArray = function (test) {
var src = fs.readFileSync(__dirname + "/fixtures/regex_array.js", "utf8");
TestRun(test)
.test(src, {es3: true});
test.done();
};
// Regression test for GH-1070
exports.testUndefinedAssignment = function (test) {
var src = [
"var x = undefined;",
"const y = undefined;",
"let z = undefined;",
"for(var a = undefined; a < 9; a++) {",
" var b = undefined;", // necessary - see gh-1191
" const c = undefined;",
" let d = undefined;",
" var e = function() {",
" var f = undefined;",
" const g = undefined;",
" let h = undefined;",
" };",
"}",
"// jshint -W080",
"var i = undefined;",
"const j = undefined;",
"let k = undefined;",
"// jshint +W080",
"var l = undefined === 0;",
"const m = undefined === 0;",
"let n = undefined === 0;",
"let [ o = undefined === 0 ] = [];",
"[ o = undefined === 0] = [];",
"let { p = undefined === 0, x: q = undefined === 0 } = {};",
"({ p = undefined === 0, x: q = undefined === 0 } = {});"
];
TestRun(test)
.addError(1, "It's not necessary to initialize 'x' to 'undefined'.")
.addError(2, "It's not necessary to initialize 'y' to 'undefined'.")
.addError(3, "It's not necessary to initialize 'z' to 'undefined'.")
.addError(4, "It's not necessary to initialize 'a' to 'undefined'.")
.addError(6, "It's not necessary to initialize 'c' to 'undefined'.")
.addError(7, "It's not necessary to initialize 'd' to 'undefined'.")
.addError(9, "It's not necessary to initialize 'f' to 'undefined'.")
.addError(10, "It's not necessary to initialize 'g' to 'undefined'.")
.addError(11, "It's not necessary to initialize 'h' to 'undefined'.")
.test(src, {esnext: true});
test.done();
};
exports.testES6Modules = function (test) {
var src = fs.readFileSync(__dirname + "/fixtures/es6-import-export.js", "utf8");
var importConstErrors = [
[51, "Attempting to override '$' which is a constant."],
[52, "Attempting to override 'emGet' which is a constant."],
[53, "Attempting to override 'one' which is a constant."],
[54, "Attempting to override '_' which is a constant."],
[55, "Attempting to override 'ember2' which is a constant."],
[57, "'$' has already been declared."],
[58, "'emGet' has already been declared."],
[58, "'set' has already been declared."],
[59, "'_' has already been declared."],
[60, "'ember2' has already been declared."],
[65, "'newImport' was used before it was declared, which is illegal for 'const' variables."]
];
var testRun = TestRun(test);
importConstErrors.forEach(function(error) { testRun.addError.apply(testRun, error); });
testRun.test(src, {esnext: true});
testRun = TestRun(test)
.addError(3, "'import' is only available in ES6 (use 'esversion: 6').")
.addError(4, "'import' is only available in ES6 (use 'esversion: 6').")
.addError(5, "'import' is only available in ES6 (use 'esversion: 6').")
.addError(6, "'import' is only available in ES6 (use 'esversion: 6').")
.addError(7, "'import' is only available in ES6 (use 'esversion: 6').")
.addError(8, "'import' is only available in ES6 (use 'esversion: 6').")
.addError(9, "'import' is only available in ES6 (use 'esversion: 6').")
.addError(10, "'import' is only available in ES6 (use 'esversion: 6').")
.addError(11, "'import' is only available in ES6 (use 'esversion: 6').")
.addError(22, "'export' is only available in ES6 (use 'esversion: 6').")
.addError(26, "'export' is only available in ES6 (use 'esversion: 6').")
.addError(30, "'export' is only available in ES6 (use 'esversion: 6').")
.addError(31, "'export' is only available in ES6 (use 'esversion: 6').")
.addError(32, "'export' is only available in ES6 (use 'esversion: 6').")
.addError(36, "'export' is only available in ES6 (use 'esversion: 6').")
.addError(40, "'export' is only available in ES6 (use 'esversion: 6').")
.addError(44, "'export' is only available in ES6 (use 'esversion: 6').")
.addError(46, "'export' is only available in ES6 (use 'esversion: 6').")
.addError(47, "'class' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).")
.addError(48, "'export' is only available in ES6 (use 'esversion: 6').")
.addError(48, "'class' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).")
.addError(47, "'export' is only available in ES6 (use 'esversion: 6').")
.addError(46, "'class' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).")
.addError(57, "'import' is only available in ES6 (use 'esversion: 6').")
.addError(58, "'import' is only available in ES6 (use 'esversion: 6').")
.addError(59, "'import' is only available in ES6 (use 'esversion: 6').")
.addError(60, "'import' is only available in ES6 (use 'esversion: 6').")
.addError(65, "'import' is only available in ES6 (use 'esversion: 6').")
.addError(67, "'export' is only available in ES6 (use 'esversion: 6').")
.addError(67, "'function*' is only available in ES6 (use 'esversion: 6').")
.addError(67, "'yield' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).");
importConstErrors.forEach(function(error) { testRun.addError.apply(testRun, error); });
testRun.test(src, {});
var src2 = [
"var a = {",
"import: 'foo',",
"export: 'bar'",
"};"
];
TestRun(test)
.test(src2, {});
test.done();
};
exports.testES6ModulesNamedExportsAffectUnused = function (test) {
// Named Exports should count as used
var src1 = [
"var a = {",
" foo: 'foo',",
" bar: 'bar'",
"};",
"var x = 23;",
"var z = 42;",
"let c = 2;",
"const d = 7;",
"export { c, d };",
"export { a, x };",
"export var b = { baz: 'baz' };",
"export function boo() { return z; }",
"export class MyClass { }",
"export var varone = 1, vartwo = 2;",
"export const constone = 1, consttwo = 2;",
"export let letone = 1, lettwo = 2;",
"export var v1u, v2u;",
"export let l1u, l2u;",
"export const c1u, c2u;",
"export function* gen() { yield 1; }"
];
TestRun(test)
.addError(19, "const 'c1u' is initialized to 'undefined'.")
.addError(19, "const 'c2u' is initialized to 'undefined'.")
.test(src1, {
esnext: true,
unused: true
});
test.done();
};
exports.testConstRedeclaration = function (test) {
// consts cannot be redeclared, but they can shadow
var src = [
"const a = 1;",
"const a = 2;",
"if (a) {",
" const a = 3;",
"}",
"for(const a in a) {",
" const a = 4;",
"}",
"function a() {",
"}",
"function b() {",
"}",
"const b = 1;"
];
TestRun(test)
.addError(2, "'a' has already been declared.")
.addError(6, "'a' was used before it was declared, which is illegal for 'const' variables.")
.addError(9, "'a' has already been declared.")
.addError(13, "'b' has already been declared.")
.test(src, {
esnext: true
});
test.done();
};
exports["test typeof in TDZ"] = function (test) {
var src = [
"let a = typeof b;", // error, use in TDZ
"let b;",
"function d() { return typeof c; }", // d may be called after declaration, no error
"let c = typeof e;", // e is not in scope, no error
"{",
" let e;",
"}"
];
TestRun(test)
.addError(2, "'b' was used before it was declared, which is illegal for 'let' variables.")
.test(src, {
esnext: true
});
test.done();
};
exports.testConstModification = function (test) {
var src = [
"const a = 1;",
"const b = { a: 2 };",
// const errors
"a = 2;",
"b = 2;",
"a++;",
"--a;",
"a += 1;",
"let y = a = 3;",
// valid const access
"b.a++;",
"--b.a;",
"b.a = 3;",
"a.b += 1;",
"const c = () => 1;",
"c();",
"const d = [1, 2, 3];",
"d[0] = 2;",
"let x = -a;",
"x = +a;",
"x = a + 1;",
"x = a * 2;",
"x = a / 2;",
"x = a % 2;",
"x = a & 1;",
"x = a ^ 1;",
"x = a === true;",
"x = a == 1;",
"x = a !== true;",
"x = a != 1;",
"x = a > 1;",
"x = a >= 1;",
"x = a < 1;",
"x = a <= 1;",
"x = 1 + a;",
"x = 2 * a;",
"x = 2 / a;",