forked from jshint/jshint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
1721 lines (1400 loc) · 54 KB
/
options.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 all non-environmental options. Non-environmental options are
* options that change how JSHint behaves instead of just pre-defining a set
* of global variables.
*/
"use strict";
var JSHINT = require('../../src/jshint.js').JSHINT;
var fs = require('fs');
var TestRun = require('../helpers/testhelper').setup.testRun;
var fixture = require('../helpers/fixture').fixture;
/**
* Option `shadow` allows you to re-define variables later in code.
*
* E.g.:
* var a = 1;
* if (cond == true)
* var a = 2; // Variable a has been already defined on line 1.
*
* More often than not it is a typo, but sometimes people use it.
*/
exports.shadow = function (test) {
var src = fs.readFileSync(__dirname + "/fixtures/redef.js", "utf8");
// Do not tolerate variable shadowing by default
TestRun(test)
.addError(5, "'a' is already defined.")
.addError(10, "'foo' is already defined.")
.test(src, {es3: true});
TestRun(test)
.addError(5, "'a' is already defined.")
.addError(10, "'foo' is already defined.")
.test(src, {es3: true, shadow: false });
TestRun(test)
.addError(5, "'a' is already defined.")
.addError(10, "'foo' is already defined.")
.test(src, {es3: true, shadow: "inner" });
// Allow variable shadowing when shadow is true
TestRun(test)
.test(src, { es3: true, shadow: true });
test.done();
};
/**
* Option `scopeshadow` allows you to re-define variables later in inner scopes.
*
* E.g.:
* var a = 1;
* function foo() {
* var a = 2;
* }
*/
exports.scopeshadow = function (test) {
var src = fs.readFileSync(__dirname + "/fixtures/scope-redef.js", "utf8");
// Do not tolarate inner scope variable shadowing by default
TestRun(test)
.addError(5, "'a' is already defined in outer scope.")
.addError(12, "'b' is already defined in outer scope.")
.addError(20, "'bar' is already defined in outer scope.")
.addError(26, "'foo' is already defined.")
.test(src, { es3: true, shadow: "outer" });
test.done();
}
/**
* Option `latedef` allows you to prohibit the use of variable before their
* definitions.
*
* E.g.:
* fn(); // fn will be defined later in code
* function fn() {};
*
* Since JavaScript has function-scope only, you can define variables and
* functions wherever you want. But if you want to be more strict, use
* this option.
*/
exports.latedef = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/latedef.js', 'utf8'),
src1 = fs.readFileSync(__dirname + '/fixtures/redef.js', 'utf8');
// By default, tolerate the use of variable before its definition
TestRun(test)
.test(src, {es3: true});
// However, JSHint must complain if variable is actually missing
TestRun(test)
.addError(1, "'fn' is not defined.")
.test('fn();', { es3: true, undef: true });
// And it also must complain about the redefinition (see option `shadow`)
TestRun(test)
.addError(5, "'a' is already defined.")
.addError(10, "'foo' is already defined.")
.test(src1, { es3: true });
// When latedef is true, JSHint must not tolerate the use before definition
TestRun(test)
.addError(10, "'vr' was used before it was defined.")
.test(src, { es3: true, latedef: "nofunc" });
// When latedef_func is true, JSHint must not tolerate the use before definition for functions
TestRun(test)
.addError(2, "'fn' was used before it was defined.")
.addError(6, "'fn1' was used before it was defined.")
.addError(10, "'vr' was used before it was defined.")
.addError(18, "Inner functions should be listed at the top of the outer function.")
.test(src, { es3: true, latedef: true });
test.done();
};
exports.notypeof = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/typeofcomp.js', 'utf8');
TestRun(test)
.addError(1, "Invalid typeof value 'funtion'")
.addError(2, "Invalid typeof value 'double'")
.addError(3, "Invalid typeof value 'bool'")
.addError(4, "Invalid typeof value 'obj'")
.test(src);
TestRun(test)
.test(src, { notypeof: true });
test.done();
}
exports['combination of latedef and undef'] = function (test) {
var src = fixture('latedefundef.js');
// Assures that when `undef` is set to true, it'll report undefined variables
// and late definitions won't be reported as `latedef` is set to false.
TestRun(test)
.addError(29, "'hello' is not defined.")
.addError(35, "'world' is not defined.")
.test(src, { es3: true, latedef: false, undef: true });
// When we suppress `latedef` and `undef` then we get no warnings.
TestRun(test)
.test(src, { es3: true, latedef: false, undef: false });
// If we warn on `latedef` but suppress `undef` we only get the
// late definition warnings.
TestRun(test)
.addError(5, "'func2' was used before it was defined.")
.addError(12, "'foo' was used before it was defined.")
.addError(18, "'fn1' was used before it was defined.")
.addError(26, "'baz' was used before it was defined.")
.addError(34, "'fn' was used before it was defined.")
.addError(41, "'q' was used before it was defined.")
.addError(46, "'h' was used before it was defined.")
.test(src, { es3: true, latedef: true, undef: false });
// But we get all the functions warning if we disable latedef func
TestRun(test)
.addError(41, "'q' was used before it was defined.")
.addError(46, "'h' was used before it was defined.")
.test(src, { es3: true, latedef: "nofunc", undef: false });
// If we warn on both options we get all the warnings.
TestRun(test)
.addError(5, "'func2' was used before it was defined.")
.addError(12, "'foo' was used before it was defined.")
.addError(18, "'fn1' was used before it was defined.")
.addError(26, "'baz' was used before it was defined.")
.addError(29, "'hello' is not defined.")
.addError(34, "'fn' was used before it was defined.")
.addError(35, "'world' is not defined.")
.addError(41, "'q' was used before it was defined.")
.addError(46, "'h' was used before it was defined.")
.test(src, { es3: true, latedef: true, undef: true });
// If we remove latedef_func, we don't get the functions warning
TestRun(test)
.addError(29, "'hello' is not defined.")
.addError(35, "'world' is not defined.")
.addError(41, "'q' was used before it was defined.")
.addError(46, "'h' was used before it was defined.")
.test(src, { es3: true, latedef: "nofunc", undef: true });
test.done();
};
exports.undefwstrict = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/undefstrict.js', 'utf8');
TestRun(test).test(src, { es3: true, undef: false });
test.done();
};
// Regression test for GH-431
exports["implied and unused should respect hoisting"] = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/gh431.js', 'utf8');
TestRun(test)
.addError(14, "'fun4' is not defined.")
.test(src, { undef: true }); // es5
JSHINT.flag = true;
JSHINT(src, { undef: true });
var report = JSHINT.data();
test.equal(report.implieds.length, 1);
test.equal(report.implieds[0].name, 'fun4');
test.deepEqual(report.implieds[0].line, [14]);
test.equal(report.unused.length, 3);
test.done();
};
/**
* The `proto` and `iterator` options allow you to prohibit the use of the
* special `__proto__` and `__iterator__` properties, respectively.
*/
exports.testProtoAndIterator = function (test) {
var source = fs.readFileSync(__dirname + '/fixtures/protoiterator.js', 'utf8');
var json = '{"__proto__": true, "__iterator__": false, "_identifier": null, "property": 123}';
// JSHint should not allow the `__proto__` and
// `__iterator__` properties by default
TestRun(test)
.addError(7, "The '__proto__' property is deprecated.")
.addError(8, "The '__proto__' property is deprecated.")
.addError(10, "The '__proto__' property is deprecated.")
.addError(27, "'__iterator__' is available in ES6 (use esnext option) or Mozilla JS extensions (use moz).")
.addError(33, "The '__proto__' property is deprecated.")
.addError(37, "The '__proto__' property is deprecated.")
.test(source, {es3: true});
TestRun(test)
.addError(1, "The '__proto__' key may produce unexpected results.")
.addError(1, "The '__iterator__' key may produce unexpected results.")
.test(json, {es3: true});
// Should not report any errors when proto and iterator
// options are on
TestRun("source").test(source, { es3: true, proto: true, iterator: true });
TestRun("json").test(json, { es3: true, proto: true, iterator: true });
test.done();
};
/**
* The `camelcase` option allows you to enforce use of the camel case convention.
*/
exports.testCamelcase = function (test) {
var source = fs.readFileSync(__dirname + '/fixtures/camelcase.js', 'utf8');
// By default, tolerate arbitrary identifiers
TestRun(test)
.test(source, {es3: true});
// Require identifiers in camel case if camelcase is true
TestRun(test)
.addError(5, "Identifier 'Foo_bar' is not in camel case.")
.addError(5, "Identifier 'test_me' is not in camel case.")
.addError(6, "Identifier 'test_me' is not in camel case.")
.addError(6, "Identifier 'test_me' is not in camel case.")
.addError(13, "Identifier 'test_1' is not in camel case.")
.test(source, { es3: true, camelcase: true });
test.done();
};
/**
* Option `curly` allows you to enforce the use of curly braces around
* control blocks. JavaScript allows one-line blocks to go without curly
* braces but some people like to always use curly bracse. This option is
* for them.
*
* E.g.:
* if (cond) return;
* vs.
* if (cond) { return; }
*/
exports.curly = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/curly.js', 'utf8'),
src1 = fs.readFileSync(__dirname + '/fixtures/curly2.js', 'utf8');
// By default, tolerate one-line blocks since they are valid JavaScript
TestRun(test).test(src, {es3: true});
TestRun(test).test(src1, {es3: true});
// Require all blocks to be wrapped with curly braces if curly is true
TestRun(test)
.addError(2, "Expected '{' and instead saw 'return'.")
.addError(5, "Expected '{' and instead saw 'doSomething'.")
.addError(8, "Expected '{' and instead saw 'doSomething'.")
.addError(11, "Expected '{' and instead saw 'doSomething'.")
.test(src, { es3: true, curly: true });
TestRun(test).test(src1, { es3: true, curly: true });
test.done();
};
/** Option `noempty` prohibits the use of empty blocks. */
exports.noempty = function (test) {
var code = 'for (;;) {}';
// By default, tolerate empty blocks since they are valid JavaScript
TestRun(test).test(code, { es3: true });
// Do not tolerate, when noempty is true
TestRun(test)
.addError(1, 'Empty block.')
.test(code, { es3: true, noempty: true });
test.done();
};
/**
* Option `noarg` prohibits the use of arguments.callee and arguments.caller.
* JSHint allows them by default but you have to know what you are doing since:
* - They are not supported by all JavaScript implementations
* - They might prevent an interpreter from doing some optimization tricks
* - They are prohibited in the strict mode
*/
exports.noarg = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/noarg.js', 'utf8');
// By default, tolerate both arguments.callee and arguments.caller
TestRun(test).test(src, { es3: true });
// Do not tolerate both .callee and .caller when noarg is true
TestRun(test)
.addError(2, 'Avoid arguments.callee.')
.addError(6, 'Avoid arguments.caller.')
.test(src, { es3: true, noarg: true });
test.done();
};
/** Option `nonew` prohibits the use of constructors for side-effects */
exports.nonew = function (test) {
var code = "new Thing();",
code1 = "var obj = new Thing();";
TestRun(test).test(code, { es3: true });
TestRun(test).test(code1, { es3: true });
TestRun(test)
.addError(1, "Do not use 'new' for side effects.", {
character: 1
})
.test(code, { es3: true, nonew: true });
test.done();
};
exports.shelljs = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/shelljs.js', 'utf8');
TestRun(test, 1)
.addError(1, "'target' is not defined.")
.addError(3, "'echo' is not defined.")
.addError(4, "'exit' is not defined.")
.addError(5, "'cd' is not defined.")
.addError(6, "'pwd' is not defined.")
.addError(7, "'ls' is not defined.")
.addError(8, "'find' is not defined.")
.addError(9, "'cp' is not defined.")
.addError(10, "'rm' is not defined.")
.addError(11, "'mv' is not defined.")
.addError(12, "'mkdir' is not defined.")
.addError(13, "'test' is not defined.")
.addError(14, "'cat' is not defined.")
.addError(15, "'sed' is not defined.")
.addError(16, "'grep' is not defined.")
.addError(17, "'which' is not defined.")
.addError(18, "'dirs' is not defined.")
.addError(19, "'pushd' is not defined.")
.addError(20, "'popd' is not defined.")
.addError(21, "'env' is not defined.")
.addError(22, "'exec' is not defined.")
.addError(23, "'chmod' is not defined.")
.addError(24, "'config' is not defined.")
.addError(25, "'error' is not defined.")
.addError(26, "'tempdir' is not defined.")
.addError(29, "'require' is not defined.")
.addError(30, "'module' is not defined.")
.addError(31, "'process' is not defined.")
.test(src, { undef: true });
TestRun(test, 2)
.test(src, { undef: true, shelljs: true });
test.done();
};
// Option `asi` allows you to use automatic-semicolon insertion
exports.asi = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/asi.js', 'utf8');
TestRun(test, 1)
.addError(2, "Missing semicolon.")
.addError(4, "Missing semicolon.")
.addError(5, "Missing semicolon.")
.addError(9, "Line breaking error 'continue'.")
.addError(9, "Missing semicolon.")
.addError(10, "Missing semicolon.")
.addError(11, "Line breaking error 'break'.")
.addError(11, "Missing semicolon.")
.addError(12, "Missing semicolon.")
.addError(16, "Missing semicolon.")
.addError(17, "Missing semicolon.")
.addError(19, "Line breaking error 'break'.")
.addError(19, "Missing semicolon.")
.addError(21, "Line breaking error 'break'.")
.addError(21, "Missing semicolon.")
.addError(25, "Missing semicolon.")
.addError(26, "Missing semicolon.", { character: 10 })
.addError(27, "Missing semicolon.", { character: 12 })
.addError(28, "Missing semicolon.", { character: 12 })
.test(src, { es3: true });
TestRun(test, 2)
.test(src, { es3: true, asi: true });
test.done();
};
// Option `asi` extended for safety -- warn in scenarios that would be unsafe when using asi.
exports.safeasi = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/safeasi.js', 'utf8');
TestRun(test, 1)
// TOOD consider setting an option to suppress these errors so that
// the tests don't become tightly interdependent
.addError(10, "Bad line breaking before '/'.")
.addError(10, "Expected an identifier and instead saw '.'.")
.addError(10, "Expected an assignment or function call and instead saw an expression.")
.addError(10, "Missing semicolon.")
.addError(10, "Missing semicolon.")
.addError(11, "Missing semicolon.")
.addError(21, "Missing semicolon.")
.test(src, {});
TestRun(test, 2)
.addError(5, "Bad line breaking before '('.")
.addError(8, "Bad line breaking before '('.")
.addError(10, "Bad line breaking before '/'.")
.addError(10, "Bad line breaking before '/'.")
.addError(10, "Expected an identifier and instead saw '.'.")
.addError(10, "Expected an assignment or function call and instead saw an expression.")
.test(src, { asi: true });
test.done();
};
/** Option `lastsemic` allows you to skip the semicolon after last statement in a block,
* if that statement is followed by the closing brace on the same line. */
exports.lastsemic = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/lastsemic.js', 'utf8');
// without lastsemic
TestRun(test)
.addError(2, "Missing semicolon.") // missing semicolon in the middle of a block
.addError(4, "Missing semicolon.") // missing semicolon in a one-liner function
.addError(5, "Missing semicolon.") // missing semicolon at the end of a block
.test(src, {es3: true});
// with lastsemic
TestRun(test)
.addError(2, "Missing semicolon.")
.addError(5, "Missing semicolon.")
.test(src, { es3: true, lastsemic: true });
// this line is valid now: [1, 2, 3].forEach(function(i) { print(i) });
// line 5 isn't, because the block doesn't close on the same line
test.done();
};
/**
* Option `expr` allows you to use ExpressionStatement as a Program code.
*
* Even though ExpressionStatement as a Program (i.e. without assingment
* of its result) is a valid JavaScript, more often than not it is a typo.
* That's why by default JSHint complains about it. But if you know what
* are you doing, there is nothing wrong with it.
*/
exports.expr = function (test) {
var exps = [
"obj && obj.method && obj.method();",
"myvar && func(myvar);",
"1;",
"true;",
"+function (test) {};"
];
for (var i = 0, exp; exp = exps[i]; i += 1) {
TestRun(test)
.addError(1, 'Expected an assignment or function call and instead saw an expression.')
.test(exp, { es3: true });
}
for (i = 0, exp = null; exp = exps[i]; i += 1) {
TestRun(test).test(exp, { es3: true, expr: true });
}
test.done();
};
// Option `undef` requires you to always define variables you use.
exports.undef = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/undef.js', 'utf8');
// Make sure there are no other errors
TestRun(test).test(src, { es3: true });
// Make sure it fails when undef is true
TestRun(test)
.addError(1, "'undef' is not defined.")
.addError(5, "'undef' is not defined.")
.addError(6, "'undef' is not defined.")
.addError(8, "'undef' is not defined.")
.addError(9, "'undef' is not defined.")
.addError(13, "'localUndef' is not defined.")
.addError(18, "'localUndef' is not defined.")
.addError(19, "'localUndef' is not defined.")
.addError(21, "'localUndef' is not defined.")
.addError(22, "'localUndef' is not defined.")
.test(src, { es3: true, undef: true });
// Regression test for GH-668.
src = fs.readFileSync(__dirname + "/fixtures/gh668.js", "utf8");
test.ok(JSHINT(src, { undef: true }));
test.ok(!JSHINT.data().implieds);
test.ok(JSHINT(src));
test.ok(!JSHINT.data().implieds);
test.done();
};
exports.unused = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/unused.js', 'utf8');
TestRun(test).test(src, { esnext: true });
var var_errors = [
[1, "'a' is defined but never used."],
[7, "'c' is defined but never used."],
[15, "'foo' is defined but never used."],
[20, "'bar' is defined but never used."],
[22, "'i' is defined but never used."],
[36, "'cc' is defined but never used."],
[39, "'dd' is defined but never used."]
];
var last_param_errors = [
[6, "'f' is defined but never used."],
[22, "'i' is defined but never used."],
[28, "'a' is defined but never used."],
[28, "'b' is defined but never used."],
[28, "'c' is defined but never used."]
];
var all_param_errors = [
[15, "'err' is defined but never used."],
[22, "'i' is defined but never used."],
[28, "'a' is defined but never used."],
[28, "'b' is defined but never used."],
[28, "'c' is defined but never used."]
];
var true_run = TestRun(test, {esnext: true});
var_errors.concat(last_param_errors).forEach(function (e) {
true_run.addError.apply(true_run, e);
});
true_run.test(src, { esnext: true, unused: true });
test.ok(!JSHINT(src, { esnext: true, unused: true }));
// Test checking all function params via unused="strict"
var all_run = TestRun(test);
var_errors.concat(last_param_errors, all_param_errors).forEach(function (e) {
all_run.addError.apply(true_run, e);
});
all_run.test(src, { esnext: true, unused: "strict"});
// Test checking everything except function params
var vars_run = TestRun(test);
var_errors.forEach(function (e) { vars_run.addError.apply(vars_run, e); });
vars_run.test(src, { esnext: true, unused: "vars"});
var unused = JSHINT.data().unused;
test.equal(12, unused.length);
test.ok(unused.some(function (err) { return err.line === 1 && err.character == 5 && err.name === "a"; }));
test.ok(unused.some(function (err) { return err.line === 6 && err.character == 18 && err.name === "f"; }));
test.ok(unused.some(function (err) { return err.line === 7 && err.character == 9 && err.name === "c"; }));
test.ok(unused.some(function (err) { return err.line === 15 && err.character == 10 && err.name === "foo"; }));
test.done();
};
// Regressions for "unused" getting overwritten via comment (GH-778)
exports['unused overrides'] = function (test) {
var code;
code = ['function foo(a) {', '/*jshint unused:false */', '}', 'foo();'];
TestRun(test).test(code, {es3: true, unused: true});
code = ['function foo(a, b, c) {', '/*jshint unused:vars */', 'var i = b;', '}', 'foo();'];
TestRun(test)
.addError(3, "'i' is defined but never used.")
.test(code, {es3: true, unused: true});
code = ['function foo(a, b, c) {', '/*jshint unused:true */', 'var i = b;', '}', 'foo();'];
TestRun(test)
.addError(1, "'c' is defined but never used.")
.addError(3, "'i' is defined but never used.")
.test(code, {es3: true, unused: "strict"});
code = ['function foo(a, b, c) {', '/*jshint unused:strict */', 'var i = b;', '}', 'foo();'];
TestRun(test)
.addError(1, "'a' is defined but never used.")
.addError(1, "'c' is defined but never used.")
.addError(3, "'i' is defined but never used.")
.test(code, {es3: true, unused: true});
code = ['/*jshint unused:vars */', 'function foo(a, b) {}', 'foo();'];
TestRun(test).test(code, {es3: true, unused: "strict"});
code = ['/*jshint unused:vars */', 'function foo(a, b) {', 'var i = 3;', '}', 'foo();'];
TestRun(test)
.addError(3, "'i' is defined but never used.")
.test(code, {es3: true, unused: "strict"});
test.done();
};
// Regression test for `undef` to make sure that ...
exports['undef in a function scope'] = function (test) {
var src = fixture('undef_func.js');
// Make sure that the lint is clean with and without undef.
TestRun(test).test(src, {es3: true});
TestRun(test).test(src, {es3: true, undef: true });
test.done();
};
/** Option `scripturl` allows the use of javascript-type URLs */
exports.scripturl = function (test) {
var code = [
"var foo = { 'count': 12, 'href': 'javascript:' };",
"foo = 'javascript:' + 'xyz';"
],
src = fs.readFileSync(__dirname + '/fixtures/scripturl.js', 'utf8');
// Make sure there is an error
TestRun(test)
.addError(1, "Script URL.")
.addError(2, "Script URL.") // 2 times?
.addError(2, "JavaScript URL.")
.test(code, {es3: true});
// Make sure the error goes away when javascript URLs are tolerated
TestRun(test).test(code, { es3: true, scripturl: true });
// Make sure an error does not exist for labels that look like URLs (GH-1013)
TestRun(test)
.test(src, {es3: true});
test.done();
};
/**
* Option `forin` disallows the use of for in loops without hasOwnProperty.
*
* The for in statement is used to loop through the names of properties
* of an object, including those inherited through the prototype chain.
* The method hasOwnPropery is used to check if the property belongs to
* an object or was inherited through the prototype chain.
*/
exports.forin = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/forin.js', 'utf8');
var msg = 'The body of a for in should be wrapped in an if statement to filter unwanted ' +
'properties from the prototype.';
// Make sure there are no other errors
TestRun(test).test(src, {es3: true});
// Make sure it fails when forin is true
TestRun(test)
.addError(15, msg)
.addError(23, msg)
.addError(37, msg)
.addError(43, msg)
.addError(73, msg)
.test(src, { es3: true, forin: true });
test.done();
};
/**
* Option `loopfunc` allows you to use function expression in the loop.
* E.g.:
* while (true) x = function (test) {};
*
* This is generally a bad idea since it is too easy to make a
* closure-related mistake.
*/
exports.loopfunc = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/loopfunc.js', 'utf8');
// By default, not functions are allowed inside loops
TestRun(test)
.addError(4, "Don't make functions within a loop.")
.addError(8, "Don't make functions within a loop.")
.addError(20, "Don't make functions within a loop.")
.addError(25, "Don't make functions within a loop.")
.addError(12, "Function declarations should not be placed in blocks. Use a function " +
"expression or move the statement to the top of the outer function.")
.test(src, {es3: true});
// When loopfunc is true, only function declaration should fail.
// Expressions are okay.
TestRun(test)
.addError(12, "Function declarations should not be placed in blocks. Use a function " +
"expression or move the statement to the top of the outer function.")
.test(src, { es3: true, loopfunc: true });
test.done();
};
/** Option `boss` unlocks some useful but unsafe features of JavaScript. */
exports.boss = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/boss.js', 'utf8');
// By default, warn about suspicious assignments
TestRun(test)
.addError(1, 'Expected a conditional expression and instead saw an assignment.')
.addError(4, 'Expected a conditional expression and instead saw an assignment.')
.addError(7, 'Expected a conditional expression and instead saw an assignment.')
.addError(12, 'Expected a conditional expression and instead saw an assignment.')
// GH-657
.addError(14, 'Expected a conditional expression and instead saw an assignment.')
.addError(17, 'Expected a conditional expression and instead saw an assignment.')
.addError(20, 'Expected a conditional expression and instead saw an assignment.')
.addError(25, 'Expected a conditional expression and instead saw an assignment.')
// GH-670
.addError(28, "Did you mean to return a conditional instead of an assignment?")
.addError(32, "Did you mean to return a conditional instead of an assignment?")
.test(src, {es3: true});
// But if you are the boss, all is good
TestRun(test).test(src, { es3: true, boss: true });
test.done();
};
/**
* Options `eqnull` allows you to use '== null' comparisons.
* It is useful when you want to check if value is null _or_ undefined.
*/
exports.eqnull = function (test) {
var code = [
'if (e == null) doSomething();',
'if (null == e) doSomething();',
'if (e != null) doSomething();',
'if (null != e) doSomething();',
];
// By default, warn about `== null` comparison
TestRun(test)
.addError(1, "Use '===' to compare with 'null'.")
.addError(2, "Use '===' to compare with 'null'.")
.addError(3, "Use '!==' to compare with 'null'.")
.addError(4, "Use '!==' to compare with 'null'.")
.test(code, {es3: true});
// But when `eqnull` is true, no questions asked
TestRun(test).test(code, { es3: true, eqnull: true });
// Make sure that `eqnull` has precedence over `eqeqeq`
TestRun(test).test(code, { es3: true, eqeqeq: true, eqnull: true });
test.done();
};
/**
* Option `supernew` allows you to use operator `new` with anonymous functions
* and objects without invocation.
*
* Ex.:
* new function (test) { ... };
* new Date;
*/
exports.supernew = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/supernew.js', 'utf8');
TestRun(test)
.addError(1, "Weird construction. Is 'new' necessary?")
.addError(9, "Missing '()' invoking a constructor.", { character: 1 })
.addError(11, "Missing '()' invoking a constructor.", {
character: 13
})
.test(src, {es3: true});
TestRun(test).test(src, { es3: true, supernew: true });
test.done();
};
/** Option `bitwise` disallows the use of bitwise operators. */
exports.bitwise = function (test) {
var ops = [ '&', '|', '^', '<<', '>>', '>>>' ];
var moreTests = [
'var c = ~a;',
'c &= 2;'
];
// By default allow bitwise operators
for (var i = 0, op; op = ops[i]; i += 1) {
TestRun(test).test('var c = a ' + op + ' b;', {es3: true});
}
TestRun(test).test(moreTests, {es3: true});
for (i = 0, op = null; op = ops[i]; i += 1) {
TestRun(test)
.addError(1, "Unexpected use of '" + op + "'.")
.test('var c = a ' + op + ' b;', { es3: true, bitwise: true });
}
TestRun(test)
.addError(1, "Unexpected '~'.")
.addError(2, "Unexpected use of '&='.")
.test(moreTests, { es3: true, bitwise: true });
test.done();
};
/** Option `debug` allows the use of debugger statements. */
exports.debug = function (test) {
var code = 'function test () { debugger; return true; }';
// By default disallow debugger statements.
TestRun(test)
.addError(1, "Forgotten 'debugger' statement?")
.test(code, {es3: true});
// But allow them if debug is true.
TestRun(test).test(code, { es3: true, debug: true });
test.done();
};
/** `debugger` statements without semicolons are found on the correct line */
exports.debug = function (test) {
var src = [
"function test () {",
"debugger",
"return true; }"
];
// Ensure we mark the correct line when finding debugger statements
TestRun(test)
.addError(2, "Forgotten 'debugger' statement?")
.test(src, {es3: true, asi: true});
test.done();
};
/** Option `eqeqeq` requires you to use === all the time. */
exports.eqeqeq = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/eqeqeq.js', 'utf8');
TestRun(test)
.addError(8, "Use '===' to compare with 'null'.")
.test(src, {es3: true});
TestRun(test)
.addError(2, "Expected '===' and instead saw '=='.")
.addError(5, "Expected '!==' and instead saw '!='.")
.addError(8, "Expected '===' and instead saw '=='.")
.test(src, { es3: true, eqeqeq: true });
test.done();
};
/** Option `evil` allows the use of eval. */
exports.evil = function (test) {
var src = [
"eval('hey();');",
"document.write('');",
"document.writeln('');",
"window.execScript('xyz');",
"new Function('xyz();');",
"setTimeout('xyz();', 2);",
"setInterval('xyz();', 2);",
"var t = document['eval']('xyz');"
];
TestRun(test)
.addError(1, "eval can be harmful.")
.addError(2, "document.write can be a form of eval.")
.addError(3, "document.write can be a form of eval.")
.addError(4, "eval can be harmful.")
.addError(5, "The Function constructor is a form of eval.")
.addError(6, "Implied eval. Consider passing a function instead of a string.")
.addError(7, "Implied eval. Consider passing a function instead of a string.")
.addError(8, "eval can be harmful.")
.test(src, { es3: true, browser: true });
TestRun(test).test(src, { es3: true, evil: true, browser: true });
test.done();
};
/**
* Option `immed` forces you to wrap immediate invocations in parens.
*
* Functions in JavaScript can be immediately invoce but that can confuse
* readers of your code. To make it less confusing, wrap the invocations in
* parens.
*
* E.g. (note the parens):
* var a = (function (test) {
* return 'a';
* }());
* console.log(a); // --> 'a'
*/
exports.immed = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/immed.js', 'utf8');
TestRun(test).test(src, {es3: true});
TestRun(test)
.addError(3, "Wrap an immediate function invocation in parens " +
"to assist the reader in understanding that the expression " +
"is the result of a function, and not the function itself.")
.addError(13, "Wrapping non-IIFE function literals in parens is unnecessary.")
.test(src, { es3: true, immed: true });
// Regression for GH-900
TestRun(test)
.addError(1, "Expected an assignment or function call and instead saw an expression.")
.addError(1, "Missing semicolon.")
.addError(1, "Expected an identifier and instead saw ')'.")
.addError(1, "Expected an assignment or function call and instead saw an expression.")
.addError(1, "Unmatched '{'.")
.addError(1, "Unmatched '('.")
.addError(1, "Wrapping non-IIFE function literals in parens is unnecessary.")
.addError(1, "Expected an assignment or function call and instead saw an expression.")
.addError(1, "Missing semicolon.")
.test("(function () { if (true) { }());", { es3: true, immed: true });
test.done();
};
/** Option `plusplus` prohibits the use of increments/decrements. */
exports.plusplus = function (test) {
var ops = [ '++', '--' ];
for (var i = 0, op; op = ops[i]; i += 1) {
TestRun(test).test('var i = j' + op + ';', {es3: true});
TestRun(test).test('var i = ' + op + 'j;', {es3: true});
}
for (i = 0, op = null; op = ops[i]; i += 1) {
TestRun(test)
.addError(1, "Unexpected use of '" + op + "'.")
.test('var i = j' + op + ';', { es3: true, plusplus: true });
TestRun(test)
.addError(1, "Unexpected use of '" + op + "'.")
.test('var i = ' + op + 'j;', { es3: true, plusplus: true });
}
test.done();
};
/**
* Option `newcap` requires constructors to be capitalized.
*
* Constructors are functions that are designed to be used with the `new` statement.
* `new` creates a new object and binds it to the implied this parameter.
* A constructor executed without new will have its this assigned to a global object,
* leading to errors.
*
* Unfortunately, JavaScript gives us absolutely no way of telling if a function is a
* constructor. There is a convention to capitalize all constructor names to prevent
* those mistakes. This option enforces that convention.
*/
exports.newcap = function (test) {
var src = fs.readFileSync(__dirname + '/fixtures/newcap.js', 'utf8');