/
parser.js
10228 lines (8873 loc) · 373 KB
/
parser.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
/**
* Tests for the parser/tokenizer
*/
"use strict";
var JSHINT = require("../..").JSHINT;
var fs = require('fs');
var TestRun = require("../helpers/testhelper").setup.testRun;
var path = require("path");
/**
* The warning for this input was intentionally disabled after research into
* its justification produced no results.
*/
exports.unsafe = function (test) {
var code = [
"var a\u000a = 'Here is a unsafe character';",
];
TestRun(test)
.test(code, {es3: true});
test.done();
};
exports.peekOverDirectives = function (test) {
var code = fs.readFileSync(__dirname + "/fixtures/peek-over-directives.js", "utf8");
TestRun(test)
// Within object literal
.addError(18, 14, "Unexpected control character in regular expression.")
.addError(19, 14, "Unexpected escaped character '<' in regular expression.")
.addError(20, 81, "Line is too long.")
.addError(21, 15, "Control character in string: <non-printable>.")
.addError(22, 14, "'Octal integer literal' is only available in ES6 (use 'esversion: 6').")
.addError(23, 14, "'Binary integer literal' is only available in ES6 (use 'esversion: 6').")
.addError(24, 14, "'template literal syntax' is only available in ES6 (use 'esversion: 6').")
.addError(25, 14, "'Sticky RegExp flag' is only available in ES6 (use 'esversion: 6').")
// Within array literal:
.addError(44, 3, "Unexpected control character in regular expression.")
.addError(45, 3, "Unexpected escaped character '<' in regular expression.")
.addError(46, 81, "Line is too long.")
.addError(47, 4, "Control character in string: <non-printable>.")
.addError(48, 3, "'Octal integer literal' is only available in ES6 (use 'esversion: 6').")
.addError(49, 3, "'Binary integer literal' is only available in ES6 (use 'esversion: 6').")
.addError(50, 3, "'template literal syntax' is only available in ES6 (use 'esversion: 6').")
.addError(51, 3, "'Sticky RegExp flag' is only available in ES6 (use 'esversion: 6').")
// Within grouping operator:
.addError(70, 3, "Unexpected control character in regular expression.")
.addError(71, 3, "Unexpected escaped character '<' in regular expression.")
.addError(72, 81, "Line is too long.")
.addError(73, 4, "Control character in string: <non-printable>.")
.addError(74, 3, "'Octal integer literal' is only available in ES6 (use 'esversion: 6').")
.addError(75, 3, "'Binary integer literal' is only available in ES6 (use 'esversion: 6').")
.addError(76, 3, "'template literal syntax' is only available in ES6 (use 'esversion: 6').")
.addError(77, 3, "'Sticky RegExp flag' is only available in ES6 (use 'esversion: 6').")
.test(code);
test.done();
};
exports.other = function (test) {
var code = [
"\\",
"!",
];
TestRun(test)
.addError(1, 1, "Unexpected '\\'.")
.addError(2, 1, "Unexpected early end of program.")
.addError(2, 1, "Unrecoverable syntax error. (100% scanned).")
.test(code, {es3: true});
// GH-818
TestRun(test)
.addError(1, 15, "Expected an identifier and instead saw ')'.")
.addError(1, 15, "Unrecoverable syntax error. (100% scanned).")
.test("if (product < ) {}", {es3: true});
// GH-2506
TestRun(test)
.addError(1, 7, "Expected an identifier and instead saw ';'.")
.addError(1, 1, "Unrecoverable syntax error. (100% scanned).")
.test("typeof;");
TestRun(test)
.addError(1, 1, "Unrecoverable syntax error. (0% scanned).")
.test("}");
test.done();
};
exports.confusingOps = function (test) {
var code = [
"var a = 3 - -3;",
"var b = 3 + +3;",
"a = a - --a;",
"a = b + ++b;",
"a = a-- - 3;", // this is not confusing?!
"a = a++ + 3;", // this is not confusing?!
];
var run = TestRun(test, "AdditiveExpressions")
.addError(1, 13, "Confusing minuses.")
.addError(2, 13, "Confusing plusses.")
.addError(3, 9, "Confusing minuses.")
.addError(4, 9, "Confusing plusses.");
run.test(code, {es3: true});
run.test(code, {}); // es5
run.test(code, {esnext: true});
run.test(code, {moz: true});
TestRun(test, "UnaryExpressions")
.addError(1, 8, "Confusing minuses.")
.addError(2, 8, "Confusing minuses.")
.addError(3, 8, "Confusing plusses.")
.addError(4, 8, "Confusing plusses.")
.test([
"void - -x;",
"void - --x;",
"void + +x;",
"void + ++x;"
]);
test.done();
};
exports.division = function (test) {
var run;
var code = [
'var a=4,b=4,i=2;',
'a/=b+2;',
'a/=b/2;',
'a/=b/i;',
'/*jshint expr:true*/',
'/=b/i;' // valid standalone RegExp expression
];
run = TestRun(test);
run.test(code);
test.done();
};
exports.plusplus = function (test) {
var run;
var code = [
"var a = ++[2];",
"var b = --(2);",
"var c = [2]++;",
"var d = (2)--;",
];
run = TestRun(test)
.addError(1, 9, "Unexpected use of '++'.")
.addError(1, 9, "Bad assignment.")
.addError(2, 9, "Unexpected use of '--'.")
.addError(2, 9, "Bad assignment.")
.addError(3, 12, "Unexpected use of '++'.")
.addError(3, 12, "Bad assignment.")
.addError(4, 12, "Unexpected use of '--'.")
.addError(4, 12, "Bad assignment.");
run.test(code, { plusplus: true, es3: true });
run.test(code, { plusplus: true }); // es5
run.test(code, { plusplus: true, esnext: true });
run.test(code, { plusplus: true, moz: true });
run = TestRun(test)
.addError(1, 9, "Bad assignment.")
.addError(2, 9, "Bad assignment.")
.addError(3, 12, "Bad assignment.")
.addError(4, 12, "Bad assignment.");
run.test(code, { plusplus: false, es3: true });
run.test(code, { plusplus: false }); // es5
run.test(code, { plusplus: false, esnext: true });
run.test(code, { plusplus: false, moz: true });
test.done();
};
exports.assignment = function (test) {
var code = [
"function test() {",
" arguments.length = 2;",
" arguments[0] = 3;",
" arguments.length &= 2;",
" arguments[0] >>= 3;",
"}",
"function test2() {",
" \"use strict\";",
" arguments.length = 2;",
" arguments[0] = 3;",
" arguments.length &= 2;",
" arguments[0] >>= 3;",
"}",
"a() = 2;",
];
var run = TestRun(test)
.addError(2, 20, "Assignment to properties of a mapped arguments object may cause unexpected changes to formal parameters.")
.addError(3, 16, "Assignment to properties of a mapped arguments object may cause unexpected changes to formal parameters.")
.addError(4, 20, "Assignment to properties of a mapped arguments object may cause unexpected changes to formal parameters.")
.addError(5, 16, "Assignment to properties of a mapped arguments object may cause unexpected changes to formal parameters.")
.addError(14, 5, "Bad assignment.");
run.test(code, { plusplus: true, es3: true });
run.test(code, { plusplus: true }); // es5
run.test(code, { plusplus: true, esnext: true });
run.test(code, { plusplus: true, moz: true });
TestRun(test, "assignment to `eval` outside of strict mode code")
.test([
"(function() {",
" var eval = 3;",
"}());"
]);
TestRun(test, "assignment to `eval` within strict mode code")
.addError(5, 10, "Bad assignment.")
.test([
"(function() {",
" var eval;",
" (function() {",
" 'use strict';",
" eval = 3;",
" }());",
"}());"
]);
TestRun(test, "assignment to `arguments` outside of strict mode code")
.addError(2, 13, "Assignment to properties of a mapped arguments object may cause unexpected changes to formal parameters.")
.test([
"(function() {",
" arguments = 3;",
"}());"
]);
TestRun(test, "assignment to `arguments` within strict mode code")
.addError(3, 13, "Bad assignment.")
.test([
"(function() {",
" 'use strict';",
" arguments = 3;",
"}());"
]);
test.done();
};
exports.relations = function (test) {
var code = [
"var a = 2 === NaN;",
"var b = NaN == 2;",
"var c = !2 < 3;",
"var c = 2 < !3;",
"var d = (!'x' in obj);",
"var e = (!a === b);",
"var f = (a === !'hi');",
"var g = (!2 === 1);",
"var h = (![1, 2, 3] === []);",
"var i = (!([]) instanceof Array);"
];
var run = TestRun(test)
.addError(1, 11, "Use the isNaN function to compare with NaN.")
.addError(2, 13, "Use the isNaN function to compare with NaN.")
.addError(3, 9, "Confusing use of '!'.")
.addError(4, 13, "Confusing use of '!'.")
.addError(5, 10, "Confusing use of '!'.")
.addError(6, 10, "Confusing use of '!'.")
.addError(7, 16, "Confusing use of '!'.")
.addError(8, 10, "Confusing use of '!'.")
.addError(9, 10, "Confusing use of '!'.")
.addError(10, 10, "Confusing use of '!'.");
run.test(code, {es3: true});
run.test(code, {}); // es5
run.test(code, {esnext: true});
run.test(code, {moz: true});
TestRun(test, "No suitable expression following logical NOT.")
.addError(1, 7, "Expected an identifier and instead saw ';'.")
.addError(1, 6, "Unrecoverable syntax error. (100% scanned).")
.test("void !;");
TestRun(test, "Logical NOT in combination with 'infix' operators.")
.addError(3, 6, "Confusing use of '!'.")
.addError(4, 6, "Confusing use of '!'.")
.addError(5, 6, "Confusing use of '!'.")
.addError(6, 6, "Confusing use of '!'.")
.addError(7, 6, "Confusing use of '!'.")
.addError(8, 6, "Confusing use of '!'.")
.addError(9, 6, "Confusing use of '!'.")
.addError(10, 6, "Confusing use of '!'.")
.addError(11, 6, "Confusing use of '!'.")
.addError(12, 6, "Confusing use of '!'.")
.addError(13, 6, "Confusing use of '!'.")
.addError(14, 6, "Confusing use of '!'.")
.addError(15, 6, "Confusing use of '!'.")
.test([
"void !'-';",
"void !'+';",
"void !(0 < 0);",
"void !(0 <= 0);",
"void !(0 == 0);",
"void !(0 === 0);",
"void !(0 !== 0);",
"void !(0 != 0);",
"void !(0 > 0);",
"void !(0 >= 0);",
"void !(0 + 0);",
"void !(0 - 0);",
"void !(0 * 0);",
"void !(0 / 0);",
"void !(0 % 0);",
]);
TestRun(test, "Logical NOT in combination with other unary operators.")
.addError(3, 6, "Confusing use of '!'.")
.addError(4, 6, "Confusing use of '!'.")
.test([
"void !'-';",
"void !'+';",
"void !+0;",
"void !-0;"
]);
test.done();
};
exports.options = function (test) {
var code = [
"/*member a*/",
"/*members b*/",
"var x; x.a.b.c();",
"/*jshint ++ */",
"/*jslint indent: 0 */",
"/*jslint indent: -2 */",
"/*jslint indent: 100.4 */",
"/*jslint maxlen: 200.4 */",
"/*jslint maxerr: 300.4 */",
"/*jslint maxerr: 0 */",
"/*jslint maxerr: 20 */",
"/*member c:true */",
"/*jshint d:no */",
"/*global xxx*/",
"xxx = 2;",
"/*jshint relaxing: true */",
"/*jshint toString: true */",
];
var run = TestRun(test)
.addError(3, 14, "Unexpected /*member 'c'.")
.addError(4, 1, "Bad option: '++'.")
.addError(5, 1, "Expected a small integer or 'false' and instead saw '0'.")
.addError(6, 1, "Expected a small integer or 'false' and instead saw '-2'.")
.addError(7, 1, "Expected a small integer or 'false' and instead saw '100.4'.")
.addError(8, 1, "Expected a small integer or 'false' and instead saw '200.4'.")
.addError(9, 1, "Expected a small integer or 'false' and instead saw '300.4'.")
.addError(10, 1, "Expected a small integer or 'false' and instead saw '0'.")
.addError(13, 1, "Bad option: 'd'.")
.addError(15, 1, "Read only.")
.addError(16, 1, "Bad option: 'relaxing'.")
.addError(17, 1, "Bad option: 'toString'.");
run.test(code, {es3: true});
run.test(code, {}); // es5
run.test(code, {esnext: true});
run.test(code, {moz: true});
TestRun(test).test(fs.readFileSync(__dirname + "/fixtures/gh988.js", "utf8"));
test.done();
};
exports.emptyDirectives = function (test) {
TestRun(test)
.addError(1, 1, "Bad option value.")
.test('/* global */');
TestRun(test)
.addError(1, 1, "Bad option value.")
.test('/* global : */');
TestRun(test)
.addError(1, 1, "Bad option value.")
.test('/* global -: */');
TestRun(test)
.test('/* global foo, bar, baz, */');
TestRun(test)
.addError(1, 1, "Bad option value.")
.test('/* globals */');
TestRun(test)
.addError(1, 1, "Bad option value.")
.test('/* globals : */');
TestRun(test)
.addError(1, 1, "Bad option value.")
.test('/* globals -: */');
TestRun(test)
.test('/* globals foo, bar, baz, */');
TestRun(test)
.addError(1, 1, "Bad option value.")
.test('/* exported */');
TestRun(test)
.test('/* exported foo, bar, baz, */');
test.done();
};
exports["jshint option comments single line"] = function (test) {
var src = fs.readFileSync(__dirname + "/fixtures/gh1768-1.js", "utf8");
TestRun(test).test(src);
test.done();
};
exports["jshint option comments single line, leading and trailing space"] = function (test) {
var src = fs.readFileSync(__dirname + "/fixtures/gh1768-2.js", "utf8");
TestRun(test).test(src);
test.done();
};
exports["jshint option comments multi line"] = function (test) {
var src = fs.readFileSync(__dirname + "/fixtures/gh1768-3.js", "utf8");
TestRun(test).test(src);
test.done();
};
exports["jshint option comments multi line, leading and trailing space"] = function (test) {
var src = fs.readFileSync(__dirname + "/fixtures/gh1768-4.js", "utf8");
TestRun(test)
.addError(4, 1, "'foo' is not defined.")
.test(src);
test.done();
};
exports["jshint option comments multi line/option"] = function (test) {
var src = fs.readFileSync(__dirname + "/fixtures/gh1768-5.js", "utf8");
TestRun(test)
.addError(3, 1, "'foo' is not defined.")
.test(src);
test.done();
};
exports["jshint option comments multi line/option, leading and trailing space"] = function (test) {
var src = fs.readFileSync(__dirname + "/fixtures/gh1768-6.js", "utf8");
TestRun(test)
.addError(4, 1, "'foo' is not defined.")
.test(src);
test.done();
};
exports["jshint option inline comments, leading and trailing tabs and spaces"] = function (test) {
var src = fs.readFileSync(__dirname + "/fixtures/inline-tabs-spaces.js", "utf8");
TestRun(test)
.addError(3, 9, "'x' is defined but never used.")
.addError(10, 9, "'y' is defined but never used.")
.addError(17, 9, "'z' is defined but never used.")
.test(src);
test.done();
};
exports.shebang = function (test) {
var code = [
"#!test",
"var a = 'xxx';",
"#!test"
];
var run = TestRun(test)
.addError(3, 1, "Expected an identifier and instead saw '#'.")
.addError(3, 2, "Expected an operator and instead saw '!'.")
.addError(3, 2, "Expected an assignment or function call and instead saw an expression.")
.addError(3, 3, "Expected an assignment or function call and instead saw an expression.")
.addError(3, 3, "Missing semicolon.")
.addError(3, 7, "Missing semicolon.");
run.test(code, {es3: true});
run.test(code, {}); // es5
run.test(code, {esnext: true});
run.test(code, {moz: true});
test.done();
};
exports.shebangImpliesNode = function (test) {
var code = [
"#!usr/bin/env node",
"require('module');",
];
TestRun(test).test(code);
test.done();
};
exports.numbers = function (test) {
/*jshint maxlen: 300*/
var code = [
"var a = 10e307;",
"var b = 10e308;",
"var c = 0.03 + 0.3 + 3.0 + 30.00;",
"var d = 03;",
"var e = .3;",
"var f = 0xAAg;",
"var g = 0033;",
"var h = 3.;",
"var i = 3.7.toString();",
"var j = 1e-10;", // GH-821
"var k = 0o1234567;",
"var l = 0b101;",
"var m = 0x;",
"var n = 09;",
"var o = 1e-A;",
"var p = 1/;",
"var q = 1x;"
];
TestRun(test)
.addError(2, 15, "Bad number '10e308'.")
.addError(5, 11, "A leading decimal point can be confused with a dot: '.3'.")
.addError(6, 9, "Unexpected '0'.")
.addError(7, 1, "Expected an identifier and instead saw 'var'.")
.addError(7, 4, "Missing semicolon.")
.addError(7, 13, "Don't use extra leading zeros '0033'.")
.addError(8, 11, "A trailing decimal point can be confused with a dot: '3.'.")
.addError(9, 9, "A dot following a number can be confused with a decimal point.")
.addError(11, 9, "'Octal integer literal' is only available in ES6 (use 'esversion: 6').")
.addError(12, 9, "'Binary integer literal' is only available in ES6 (use 'esversion: 6').")
.addError(13, 11, "Bad number '0x'.")
.addError(15, 9, "Unexpected '1'.")
.addError(16, 11, "Expected an identifier and instead saw ';'.")
.addError(16, 1, "Expected an identifier and instead saw 'var'.")
.addError(16, 4, "Missing semicolon.")
.addError(16, 12, "Missing semicolon.")
.addError(17, 9, "Unexpected '1'.")
.addError(17, 7, "Unexpected early end of program.")
.addError(17, 9, "Unrecoverable syntax error. (100% scanned).")
.test(code, {es3: true});
// Octals are prohibited in strict mode.
TestRun(test)
.addError(3, 11, "Octal literals are not allowed in strict mode.")
.test([
"(function () {",
"'use strict';",
"return 045;",
"}());"
]);
// GitHub #751 - an expression containing a number with a leading decimal point should be parsed in its entirety
TestRun(test)
.addError(1, 11, "A leading decimal point can be confused with a dot: '.3'.")
.addError(2, 15, "A leading decimal point can be confused with a dot: '.3'.")
.test([
"var a = .3 + 1;",
"var b = 1 + .3;",
]);
TestRun(test)
.addError(5, 18, "Missing semicolon.")
.addError(5, 18, "Expected an assignment or function call and instead saw an expression.")
.addError(6, 14, "Missing semicolon.")
.addError(6, 14, "Expected an assignment or function call and instead saw an expression.")
.test([
"var a = 0o1234567;",
"var b = 0O1234567;",
"var c = 0b101;",
"var d = 0B101;",
"var e = 0o12345678;",
"var f = 0b1012;",
], {esnext: true});
TestRun(test)
.test([
"// jshint esnext: true",
"var a = 0b0 + 0o0;"
]);
test.done();
};
exports.comments = function (test) {
var code = [
"/*",
"/* nested */",
"*/",
"/* unclosed ..",
];
var run = TestRun(test)
.addError(3, 1, "Unbegun comment.")
.addError(4, 1, "Unclosed comment.");
run.test(code, {es3: true});
run.test(code, {}); // es5
run.test(code, {esnext: true});
run.test(code, {moz: true});
var src = "/* this is a comment /* with nested slash-start */";
TestRun(test).test(src);
TestRun(test).test(fs.readFileSync(__dirname + "/fixtures/gruntComment.js", "utf8"));
TestRun(test)
.addError(1, 2, "Unmatched '{'.")
.addError(1, 2, "Unrecoverable syntax error. (100% scanned).")
.test("({");
test.done();
};
exports.regexp = {};
exports.regexp.basic = function (test) {
var code = [
"var a1 = /\\\x1f/;",
"var a2 = /[\\\x1f]/;",
"var b1 = /\\</;", // only \< is unexpected?!
"var b2 = /[\\<]/;", // only \< is unexpected?!
"var c = /(?(a)b)/;",
"var d = /)[--aa-b-cde-]/;",
"var e = /[]/;",
"var f = /[^]/;",
"var g = /[a^[]/;",
// FIXME: Firefox doesn't handle [a-\\s] well.
// See https://bugzilla.mozilla.org/show_bug.cgi?id=813249
"", // "var h = /[a-\\s-\\w-\\d\\x10-\\x20--]/;",
"var i = /[/-a1-/]/;",
"var j = /[a-<<-3]./;",
"var k = /]}/;",
"var l = /?(*)(+)({)/;",
"var m = /a{b}b{2,c}c{3,2}d{4,?}x{30,40}/;",
"var n = /a??b+?c*?d{3,4}? a?b+c*d{3,4}/;",
"var o = /a\\/* [a-^-22-]/;",
"var p = /(?:(?=a|(?!b)))/;",
"var q = /=;/;",
"var r = /(/;",
"var s = /(((/;",
"var t = /x/* 2;",
"var u = /x/;",
"var w = v + /s/;",
"var x = w - /s/;",
"var y = typeof /[a-z]/;", // GH-657
"var z = /a/ instanceof /a/.constructor;", // GH-2773
"void /./;",
"var v = /dsdg;"
];
var run = TestRun(test)
.addError(1, 10, "Unexpected control character in regular expression.")
.addError(2, 10, "Unexpected control character in regular expression.")
.addError(3, 10, "Unexpected escaped character '<' in regular expression.")
.addError(4, 10, "Unexpected escaped character '<' in regular expression.")
.addError(5, 9, "Invalid regular expression.")
.addError(6, 9, "Invalid regular expression.")
.addError(11, 9, "Invalid regular expression.")
.addError(12, 9, "Invalid regular expression.")
.addError(14, 9, "Invalid regular expression.")
.addError(15, 9, "Invalid regular expression.")
.addError(17, 9, "Invalid regular expression.")
.addError(20, 9, "Invalid regular expression.")
.addError(21, 9, "Invalid regular expression.")
.addError(29, 9, "Unclosed regular expression.")
.addError(29, 9, "Unrecoverable syntax error. (100% scanned).");
run.test(code, {es3: true});
run.test(code, {}); // es5
run.test(code, {esnext: true});
run.test(code, {moz: true});
TestRun(test).test("var a = `${/./}${/./}`;", { esversion: 6 });
// Pre Regular Expression Punctuation
// (See: token method, create function in lex.js)
//
// "."
TestRun(test)
.addError(1, 12, "A trailing decimal point can be confused with a dot: '10.'.")
.test("var y = 10. / 1;", {es3: true});
TestRun(test)
.addError(1, 12, "A trailing decimal point can be confused with a dot: '10.'.")
.test("var y = 10. / 1;", {}); // es5
TestRun(test)
.addError(1, 12, "A trailing decimal point can be confused with a dot: '10.'.")
.test("var y = 10. / 1;", {esnext: true});
TestRun(test)
.addError(1, 12, "A trailing decimal point can be confused with a dot: '10.'.")
.test("var y = 10. / 1;", {moz: true});
// ")"
TestRun(test).test("var y = Math.sqrt(16) / 180;", {es3: true});
TestRun(test).test("var y = Math.sqrt(16) / 180;", {}); // es5
TestRun(test).test("var y = Math.sqrt(16) / 180;", {esnext: true});
TestRun(test).test("var y = Math.sqrt(16) / 180;", {moz: true});
// "~"
TestRun(test).test("var y = ~16 / 180;", {es3: true});
TestRun(test).test("var y = ~16 / 180;", {}); // es5
TestRun(test).test("var y = ~16 / 180;", {esnext: true});
TestRun(test).test("var y = ~16 / 180;", {moz: true});
// "]" (GH-803)
TestRun(test).test("var x = [1]; var y = x[0] / 180;", {es3: true});
TestRun(test).test("var x = [1]; var y = x[0] / 180;", {}); // es5
TestRun(test).test("var x = [1]; var y = x[0] / 180;", {esnext: true});
TestRun(test).test("var x = [1]; var y = x[0] / 180;", {moz: true});
// "++" (GH-1787)
TestRun(test).test("var a = 1; var b = a++ / 10;", {es3: true});
TestRun(test).test("var a = 1; var b = a++ / 10;", {}); // es5
TestRun(test).test("var a = 1; var b = a++ / 10;", {esnext: true});
TestRun(test).test("var a = 1; var b = a++ / 10;", {moz: true});
// "--" (GH-1787)
TestRun(test).test("var a = 1; var b = a-- / 10;", {es3: true});
TestRun(test).test("var a = 1; var b = a-- / 10;", {}); // es5
TestRun(test).test("var a = 1; var b = a-- / 10;", {esnext: true});
TestRun(test).test("var a = 1; var b = a-- / 10;", {moz: true});
TestRun(test, "gh-3308").test("void (function() {} / 0);");
TestRun(test)
.addError(1, 9, "Invalid regular expression.")
.test("var a = /.*/ii;");
TestRun(test, "Invalid Decimal Escape Sequence tolerated without `u` flag")
.test([
"void /\\00/;",
"void /\\01/;",
"void /\\02/;",
"void /\\03/;",
"void /\\04/;",
"void /\\05/;",
"void /\\06/;",
"void /\\07/;",
"void /\\08/;",
"void /\\09/;"
]);
TestRun(test, "following `new`")
.addError(1, 5, "Bad constructor.")
.addError(1, 5, "Missing '()' invoking a constructor.")
.test("new /./;");
TestRun(test, "following `delete`")
.addError(1, 11, "Variables should not be deleted.")
.test("delete /./;");
TestRun(test, "following `extends`")
.test("class R extends /./ {}", {esversion: 6});
TestRun(test, "following `default`")
.test("export default /./;", {esversion: 6, module: true});
test.done();
};
exports.regexp.uFlag = function (test) {
// Flag validity
TestRun(test)
.addError(1, 9, "'Unicode RegExp flag' is only available in ES6 (use 'esversion: 6').")
.test("var a = /.*/u;");
TestRun(test)
.test("var a = /.*/u;", { esversion: 6 });
// Hexidecimal limits
TestRun(test)
.addError(3, 5, "Invalid regular expression.")
.test([
"var a = /\\u{0}/u;",
"a = /\\u{10FFFF}/u;",
"a = /\\u{110000}/u;"
], { esnext: true });
TestRun(test, "Guard against regression from escape sequence substitution")
.test("void /\\u{3f}/u;", { esversion: 6 });
// Hexidecimal in range patterns
TestRun(test)
.addError(3, 5, "Invalid regular expression.")
.addError(4, 5, "Invalid regular expression.")
.test([
"var a = /[\\u{61}-b]/u;",
"a = /[\\u{061}-b]/u;",
"a = /[\\u{63}-b]/u;",
"a = /[\\u{0063}-b]/u;",
], { esnext: true });
TestRun(test)
.test("var x = /[\uD834\uDF06-\uD834\uDF08a-z]/u;", { esversion: 6 });
TestRun(test)
.addError(1, 6, "Invalid regular expression.")
.test("void /.{}/u;", { esversion: 6 });
TestRun(test)
.test([
"void /.{0}/;",
"void /.{0}/u;",
"void /.{9}/;",
"void /.{9}/u;",
"void /.{23}/;",
"void /.{23}/u;"
], { esversion: 6 });
TestRun(test)
.addError(1, 6, "Invalid regular expression.")
.test("void /.{,2}/u;", { esversion: 6 });
TestRun(test)
.addError(1, 6, "Invalid regular expression.")
.test("void /.{2,1}/u;", { esversion: 6 });
TestRun(test, "Invalid group reference")
.addError(1, 6, "Invalid regular expression.")
.test("void /(.)(.)\\3/u;", { esversion: 6 });
TestRun(test, "Valid group reference - multiple digits")
.test([
"void /(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)\\10/u;",
"void /(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)\\10a/u;",
"void /(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)\\11/u;",
"void /(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)\\11a/u;"
], { esversion: 6 });
// A negative syntax test is the only way to verify JSHint is interpeting the
// adjacent digits as one reference as opposed to a reference followed by a
// literal digit.
TestRun(test, "Invalid group reference - two digits")
.addError(1, 6, "Invalid regular expression.")
.test("void /(.)\\10/u;", { esversion: 6 });
TestRun(test, "Invalid group reference - three digits")
.addError(1, 6, "Invalid regular expression.")
.test("void /(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)\\100/u;", { esversion: 6 });
TestRun(test, "Invalid group reference (permitted without flag)")
.test("void /(.)(.)\\3/;", { esversion: 6 });
TestRun(test, "Invalid unicode escape sequence")
.addError(1, 6, "Invalid regular expression.")
.test("void /\\u{123,}/u;", { esversion: 6 });
TestRun(test, "Invalid unicode escape sequence (permitted without flag)")
.test("void /\\u{123,}/;", { esversion: 6 });
TestRun(test, "Invalid character escape")
.addError(1, 6, "Invalid regular expression.")
.test("void /\\m/u;", { esversion: 6 });
TestRun(test, "Invalid character escape (permitted without flag)")
.test("void /\\m/;", { esversion: 6 });
TestRun(test, "Invalid quantifed group")
.addError(1, 6, "Invalid regular expression.")
.test("void /(?=.)?/u;", { esversion: 6 });
TestRun(test, "Invalid quantifed group (permitted without flag)")
.test("void /(?=.)?/;", { esversion: 6 });
TestRun(test, "Invalid quantifier - unclosed")
.addError(1, 6, "Invalid regular expression.")
.test("void /.{1/u;", { esversion: 6 });
TestRun(test, "Invalid quantifier - unclosed (permitted without flag)")
.test("void /.{1/;", { esversion: 6 });
TestRun(test, "Invalid quantifier - unclosed with comma")
.addError(1, 6, "Invalid regular expression.")
.test("void /.{1,/u;", { esversion: 6 });
TestRun(test, "Invalid quantifier - unclosed with comma (permitted without flag)")
.test("void /.{1,/;", { esversion: 6 });
TestRun(test, "Invalid quantifier - unclosed with upper bound")
.addError(1, 6, "Invalid regular expression.")
.test("void /.{1,2/u;", { esversion: 6 });
TestRun(test, "Invalid quantifier - unclosed with upper bound (permitted without flag)")
.test("void /.{1,2/;", { esversion: 6 });
TestRun(test, "Character class in lower bound of range (permitted without flag)")
.test('void /[\\s-1]/;', { esversion: 6 });
TestRun(test, "Character class in lower bound of range")
.addError(1, 6, "Invalid regular expression.")
.test('void /[\\s-1]/u;', { esversion: 6 });
TestRun(test, "Character class in upper bound of range (permitted without flag)")
.test('void /[1-\\W]/;', { esversion: 6 });
TestRun(test, "Character class in upper bound of range")
.addError(1, 6, "Invalid regular expression.")
.test('void /[1-\\W]/u;', { esversion: 6 });
TestRun(test)
.test('void /[\\s0-1\\s2-3\\s]/u;', { esversion: 6 });
TestRun(test, "Null CharacterEscape")
.test([
"void /\\0/u;",
"void /\\0a/u;"
], { esversion: 6 });
TestRun(test)
.addError(1, 6, "Invalid regular expression.")
.test("void /\\00/u;", { esversion: 6 });
TestRun(test)
.addError(1, 6, "Invalid regular expression.")
.test("void /\\01/u;", { esversion: 6 });
TestRun(test, "ControlEscape")
.test([
"void /\\f/u;",
"void /\\n/u;",
"void /\\r/u;",
"void /\\t/u;",
"void /\\v/u;"
], { esversion: 6 });
test.done();
};
exports.regexp.yFlag = function (test) {
// Flag validity
TestRun(test)
.addError(1, 9, "'Sticky RegExp flag' is only available in ES6 (use 'esversion: 6').")
.test("var a = /.*/y;");
TestRun(test)
.test("var a = /.*/y;", { esversion: 6 });
TestRun(test)
.addError(1, 9, "Invalid regular expression.")
.test("var a = /.*/yiy;", { esversion: 6 });
test.done();
};
exports.regexp.dotAll = function (test) {
TestRun(test, "flag presence - disallowed in editions prior to 2018")
.addError(1, 6, "'DotAll RegExp flag' is only available in ES9 (use 'esversion: 9').")
.addError(2, 6, "'DotAll RegExp flag' is only available in ES9 (use 'esversion: 9').")
.addError(3, 6, "'DotAll RegExp flag' is only available in ES9 (use 'esversion: 9').")
.test([
"void /./s;",
"void /./gs;",
"void /./sg;"
], { esversion: 8 });
TestRun(test, "flag presence - allowed in 2018")
.test([
"void /./s;",
"void /./gs;",
"void /./sg;"
], { esversion: 9 });
TestRun(test, "duplicate flag")
.addError(1, 6, "Invalid regular expression.")
.addError(2, 6, "Invalid regular expression.")
.test([
"void /./ss;",
"void /./sgs;"
], { esversion: 9 });
TestRun(test, "missing dot")
.addError(1, 6, "Unnecessary RegExp 's' flag.")
.addError(2, 6, "Unnecessary RegExp 's' flag.")
.addError(3, 6, "Unnecessary RegExp 's' flag.")
.addError(4, 6, "Unnecessary RegExp 's' flag.")
.addError(5, 6, "Unnecessary RegExp 's' flag.")
.test([
"void /dotall flag without dot/s;",
"void /literal period \\./s;",