forked from jslint-org/jslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjslint.js
4995 lines (4565 loc) · 162 KB
/
jslint.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
// jslint.js
// 2017-07-01
// Copyright (c) 2015 Douglas Crockford (www.JSLint.com)
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// The Software shall be used for Good, not Evil.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// jslint(source, option_object, global_array) is a function that takes 3
// arguments. The second two arguments are optional.
// source A text to analyze, a string or an array of strings.
// option_object An object whose keys correspond to option names.
// global_array An array of strings containing global variables that
// the file is allowed readonly access.
// jslint returns an object containing its results. The object contains a lot
// of valuable information. It can be used to generate reports. The object
// contains:
// directives: an array of directive comment tokens.
// edition: the version of JSLint that did the analysis.
// exports: the names exported from the module.
// froms: an array of strings representing each of the imports.
// functions: an array of objects that represent all of the functions
// declared in the file.
// global: an object representing the global object. Its .context property
// is an object containing a property for each global variable.
// id: "(JSLint)"
// json: true if the file is a JSON text.
// lines: an array of strings, the source.
// module: true if an import or export statement was used.
// ok: true if no warnings were generated. This is what you want.
// option: the option argument.
// property: a property object.
// stop: true if JSLint was unable to finish. You don't want this.
// tokens: an array of objects representing the tokens in the file.
// tree: the token objects arranged in a tree.
// warnings: an array of warning objects. A warning object can contain:
// name: "JSLintError"
// column: A column number in the file.
// line: A line number in the file.
// code: A warning code string.
// message: The warning message string.
// a: Exhibit A.
// b: Exhibit B.
// c: Exhibit C.
// d: Exhibit D.
// jslint works in several phases. In any of these phases, errors might be
// found. Sometimes JSLint is able to recover from an error and continue
// parsing. In some cases, it cannot and will stop early. If that should happen,
// repair your code and try again.
// Phases:
// 1. If the source is a single string, split it into an array of strings.
// 2. Turn the source into an array of tokens.
// 3. Furcate the tokens into a parse tree.
// 4. Walk the tree, traversing all of the nodes of the tree. It is a
// recursive traversal. Each node may be processed on the way down
// (preaction) and on the way up (postaction).
// 5. Check the whitespace between the tokens.
// jslint can also examine JSON text. It decides that a file is JSON text if
// the first token is "[" or "{". Processing of JSON text is much simpler than
// the processing of JavaScript programs. Only the first three phases are
// required.
// WARNING: JSLint will hurt your feelings.
/*jslint bitwise*/
/*property
a, and, arity, assign, b, bad_assignment_a, bad_directive_a, bad_get,
bad_module_name_a, bad_option_a, bad_property_a, bad_set, bitwise, block,
body, browser, c, calls, catch, charAt, charCodeAt, closer, closure, code,
column, complex, concat, constant, context, couch, create, d, dead,
default, devel, directive, directives, disrupt, dot, duplicate_a, edition,
ellipsis, else, empty_block, es6, escape_mega, eval, every, expected_a,
expected_a_at_b_c, expected_a_b, expected_a_b_from_c_d,
expected_a_before_b, expected_a_next_at_b, expected_digits_after_a,
expected_four_digits, expected_identifier_a, expected_line_break_a_b,
expected_regexp_factor_a, expected_space_a_b, expected_statements_a,
expected_string_a, expected_type_string_a, exports, expression, extra,
finally, flag, for, forEach, free, from, froms, fud, fudge, function,
function_in_loop, functions, g, global, i, id, identifier, import, inc,
indexOf, infix_in, init, initial, isArray, isNaN, join, json, keys, label,
label_a, lbp, led, length, level, line, lines, live, loop, m, margin, match,
maxerr, maxlen, message, misplaced_a, misplaced_directive_a,
missing_browser, missing_m, module, multivar, naked_block, name, names,
nested_comment, new, node, not_label_a, nr, nud, number_isNaN, ok, open,
option, out_of_scope_a,
parameters, pop, property, push, qmark, quote, redefinition_a_b, replace,
required_a_optional_b, reserved_a, right, role, search, signature, single,
slice, some, sort, split, statement, stop, strict, subscript_a, switch,
test, this, thru, toString, todo_comment, tokens, too_long, too_many,
too_many_digits, tree, try, type, u, unclosed_comment, unclosed_mega,
unclosed_string, undeclared_a, unexpected_a, unexpected_a_after_b,
unexpected_a_before_b, unexpected_at_top_level_a, unexpected_char_a,
unexpected_comment, unexpected_directive_a, unexpected_expression_a,
unexpected_label_a, unexpected_parens, unexpected_space_a_b,
unexpected_statement_a, unexpected_trailing_space, unexpected_typeof_a,
uninitialized_a, unreachable_a, unregistered_property_a, unsafe, unused_a,
use_double, use_spaces, use_strict, used, value, var_loop, var_switch,
variable, warning, warnings, weird_condition_a, weird_expression_a,
weird_loop, weird_relation_a, white, wrap_assignment, wrap_condition,
wrap_immediate, wrap_parameter, wrap_regexp, wrap_unary, wrapped, writable,
y
*/
var jslint = (function JSLint() {
"use strict";
function empty() {
// The empty function produces a new empty object that inherits nothing. This is
// much better than {} because confusions around accidental method names like
// "constructor" are completely avoided.
return Object.create(null);
}
function populate(object, array, value) {
// Augment an object by taking property names from an array of strings.
array.forEach(function (name) {
object[name] = value;
});
}
var allowed_option = {
// These are the options that are recognized in the option object or that may
// appear in a /*jslint*/ directive. Most options will have a boolean value,
// usually true. Some options will also predefine some number of global
// variables.
bitwise: true,
browser: [
"Audio",
"clearInterval",
"clearTimeout",
"document",
"event",
"FileReader",
"FormData",
"history",
"Image",
"localStorage",
"location",
"name",
"navigator",
"Option",
"screen",
"sessionStorage",
"setInterval",
"setTimeout",
"Storage",
"XMLHttpRequest"
],
couch: [
"emit", "getRow", "isArray", "log", "provides", "registerType",
"require", "send", "start", "sum", "toJSON"
],
devel: [
"alert", "confirm", "console", "prompt"
],
es6: [
"ArrayBuffer", "DataView", "Float32Array", "Float64Array",
"Generator", "GeneratorFunction", "Int8Array", "Int16Array",
"Int32Array", "Intl", "Map", "Promise", "Proxy", "Reflect",
"Set", "Symbol", "System", "Uint8Array", "Uint8ClampedArray",
"Uint16Array", "Uint32Array", "WeakMap", "WeakSet"
],
eval: true,
for: true,
fudge: true,
maxerr: 10000,
maxlen: 10000,
multivar: true,
node: [
"Buffer", "clearImmediate", "clearInterval", "clearTimeout",
"console", "exports", "global", "module", "process", "querystring",
"require", "setImmediate", "setInterval", "setTimeout",
"__dirname", "__filename"
],
single: true,
this: true,
white: true
};
var spaceop = {
// This is the set of infix operators that require a space on each side.
"!=": true,
"!==": true,
"%": true,
"%=": true,
"&": true,
"&=": true,
"&&": true,
"*": true,
"*=": true,
"+=": true,
"-=": true,
"/": true,
"/=": true,
"<": true,
"<=": true,
"<<": true,
"<<=": true,
"=": true,
"==": true,
"===": true,
"=>": true,
">": true,
">=": true,
">>": true,
">>=": true,
">>>": true,
">>>=": true,
"^": true,
"^=": true,
"|": true,
"|=": true,
"||": true
};
var bitwiseop = {
// These are the bitwise operators.
"~": true,
"^": true,
"^=": true,
"&": true,
"&=": true,
"|": true,
"|=": true,
"<<": true,
"<<=": true,
">>": true,
">>=": true,
">>>": true,
">>>=": true
};
var opener = {
// The open and close pairs.
"(": ")", // paren
"[": "]", // bracket
"{": "}", // brace
"${": "}" // mega
};
var relationop = {
// The relational operators.
"!=": true,
"!==": true,
"==": true,
"===": true,
"<": true,
"<=": true,
">": true,
">=": true
};
var standard = [
// These are the globals that are provided by the ES5 language standard.
"Array", "Boolean", "Date", "decodeURI", "decodeURIComponent",
"encodeURI", "encodeURIComponent", "Error", "EvalError", "isFinite",
"JSON", "Math", "Number", "Object", "parseInt", "parseFloat",
"RangeError", "ReferenceError", "RegExp", "String", "SyntaxError",
"TypeError", "URIError"
];
var bundle = {
// The bundle contains the raw text messages that are generated by jslint. It
// seems that they are all error messages and warnings. There are no "Atta
// boy!" or "You are so awesome!" messages. There is no positive reinforcement
// or encouragement. This relentless negativity can undermine self-esteem and
// wound the inner child. But if you accept it as sound advice rather than as
// personal criticism, it can make your programs better.
and: "The '&&' subexpression should be wrapped in parens.",
bad_assignment_a: "Bad assignment to '{a}'.",
bad_directive_a: "Bad directive '{a}'.",
bad_get: "A get function takes no parameters.",
bad_module_name_a: "Bad module name '{a}'.",
bad_option_a: "Bad option '{a}'.",
bad_property_a: "Bad property name '{a}'.",
bad_set: "A set function takes one parameter.",
duplicate_a: "Duplicate '{a}'.",
empty_block: "Empty block.",
es6: "Unexpected ES6 feature '{a}'.",
escape_mega: "Unexpected escapement in mega literal.",
expected_a: "Expected '{a}'.",
expected_a_at_b_c: "Expected '{a}' at column {b}, not column {c}.",
expected_a_b: "Expected '{a}' and instead saw '{b}'.",
expected_a_b_from_c_d: "Expected '{a}' to match '{b}' from line {c} and instead saw '{d}'.",
expected_a_before_b: "Expected '{a}' before '{b}'.",
expected_a_next_at_b: "Expected '{a}' at column {b} on the next line.",
expected_digits_after_a: "Expected digits after '{a}'.",
expected_four_digits: "Expected four digits after '\\u'.",
expected_identifier_a: "Expected an identifier and instead saw '{a}'.",
expected_line_break_a_b: "Expected a line break between '{a}' and '{b}'.",
expected_regexp_factor_a: "Expected a regexp factor and instead saw '{a}'.",
expected_space_a_b: "Expected one space between '{a}' and '{b}'.",
expected_statements_a: "Expected statements before '{a}'.",
expected_string_a: "Expected a string and instead saw '{a}'.",
expected_type_string_a: "Expected a type string and instead saw '{a}'.",
function_in_loop: "Don't make functions within a loop.",
infix_in: "Unexpected 'in'. Compare with undefined, or use the hasOwnProperty method instead.",
isNaN: "Use the isNaN function to compare with NaN.",
label_a: "'{a}' is a statement label.",
misplaced_a: "Place '{a}' at the outermost level.",
misplaced_directive_a: "Place the '/*{a}*/' directive before the first statement.",
missing_browser: "/*global*/ requires the Assume a browser option.",
missing_m: "Expected 'm' flag on a multiline regular expression.",
naked_block: "Naked block.",
nested_comment: "Nested comment.",
not_label_a: "'{a}' is not a label.",
number_isNaN: "Use Number.isNaN function to compare with NaN.",
out_of_scope_a: "'{a}' is out of scope.",
redefinition_a_b: "Redefinition of '{a}' from line {b}.",
required_a_optional_b: "Required parameter '{a}' after optional parameter '{b}'.",
reserved_a: "Reserved name '{a}'.",
subscript_a: "['{a}'] is better written in dot notation.",
todo_comment: "Unexpected TODO comment.",
too_long: "Line too long.",
too_many: "Too many warnings.",
too_many_digits: "Too many digits.",
unclosed_comment: "Unclosed comment.",
unclosed_mega: "Unclosed mega literal.",
unclosed_string: "Unclosed string.",
undeclared_a: "Undeclared '{a}'.",
unexpected_a: "Unexpected '{a}'.",
unexpected_a_after_b: "Unexpected '{a}' after '{b}'.",
unexpected_a_before_b: "Unexpected '{a}' before '{b}'.",
unexpected_at_top_level_a: "Expected '{a}' to be in a function.",
unexpected_char_a: "Unexpected character '{a}'.",
unexpected_comment: "Unexpected comment.",
unexpected_directive_a: "When using modules, don't use directive '/*{a}'.",
unexpected_expression_a: "Unexpected expression '{a}' in statement position.",
unexpected_label_a: "Unexpected label '{a}'.",
unexpected_parens: "Don't wrap function literals in parens.",
unexpected_space_a_b: "Unexpected space between '{a}' and '{b}'.",
unexpected_statement_a: "Unexpected statement '{a}' in expression position.",
unexpected_trailing_space: "Unexpected trailing space.",
unexpected_typeof_a: "Unexpected 'typeof'. Use '===' to compare directly with {a}.",
uninitialized_a: "Uninitialized '{a}'.",
unreachable_a: "Unreachable '{a}'.",
unregistered_property_a: "Unregistered property name '{a}'.",
unsafe: "Unsafe character '{a}'.",
unused_a: "Unused '{a}'.",
use_double: "Use double quotes, not single quotes.",
use_spaces: "Use spaces, not tabs.",
use_strict: "This function needs a \"use strict\" pragma.",
var_loop: "Don't declare variables in a loop.",
var_switch: "Don't declare variables in a switch.",
weird_condition_a: "Weird condition '{a}'.",
weird_expression_a: "Weird expression '{a}'.",
weird_loop: "Weird loop.",
weird_relation_a: "Weird relation '{a}'.",
wrap_assignment: "Don't wrap assignment statements in parens.",
wrap_condition: "Wrap the condition in parens.",
wrap_immediate: "Wrap an immediate function invocation in "
+ "parentheses to assist the reader in understanding that the "
+ "expression is the result of a function, and not the "
+ "function itself.",
wrap_parameter: "Wrap the parameter in parens.",
wrap_regexp: "Wrap this regexp in parens to avoid confusion.",
wrap_unary: "Wrap the unary expression in parens."
};
// Regular expression literals:
// supplant {variables}
var rx_supplant = /\{([^{}]*)\}/g;
// carriage return, carriage return linefeed, or linefeed
var rx_crlf = /\n|\r\n?/;
// unsafe characters that are silently deleted by one or more browsers
var rx_unsafe = /[\u0000-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/;
// identifier
var rx_identifier = /^([a-zA-Z_$][a-zA-Z0-9_$]*)$/;
var rx_module = /^[a-zA-Z0-9_$:.@\-\/]+$/;
var rx_bad_property = /^_|\$|Sync\$|_$/;
// star slash
var rx_star_slash = /\*\//;
// slash star
var rx_slash_star = /\/\*/;
// slash star or ending slash
var rx_slash_star_or_slash = /\/\*|\/$/;
// uncompleted work comment
var rx_todo = /\b(?:todo|TO\s?DO|HACK)\b/;
// tab
var rx_tab = /\t/g;
// directive
var rx_directive = /^(jslint|property|global)\s+(.*)$/;
var rx_directive_part = /^([a-zA-Z$_][a-zA-Z0-9$_]*)\s*(?::\s*(true|false|[0-9]+)\s*)?(?:,\s*)?(.*)$/;
// token (sorry it is so long)
var rx_token = /^((\s+)|([a-zA-Z_$][a-zA-Z0-9_$]*)|[(){}\[\]?,:;'"~`]|=(?:==?|>)?|\.+|\/[=*\/]?|\*[\/=]?|\+(?:=|\++)?|-(?:=|-+)?|[\^%]=?|&[&=]?|\|[|=]?|>{1,3}=?|<<?=?|!={0,2}|(0|[1-9][0-9]*))(.*)$/;
var rx_digits = /^([0-9]+)(.*)$/;
var rx_hexs = /^([0-9a-fA-F]+)(.*)$/;
var rx_octals = /^([0-7]+)(.*)$/;
var rx_bits = /^([01]+)(.*)$/;
// mega
var rx_mega = /[`\\]|\$\{/;
// indentation
var rx_colons = /^(.*)\?([:.]*)$/;
var rx_dot = /\.$/;
// JSON number
var rx_JSON_number = /^-?\d+(?:\.\d*)?(?:e[\-+]?\d+)?$/i;
// initial cap
var rx_cap = /^[A-Z]/;
function is_letter(string) {
return (
(string >= "a" && string <= "z\uffff")
|| (string >= "A" && string <= "Z\uffff")
);
}
function supplant(string, object) {
return string.replace(rx_supplant, function (found, filling) {
var replacement = object[filling];
return (replacement !== undefined)
? replacement
: found;
});
}
var anon = "anonymous"; // The guessed name for anonymous functions.
var blockage; // The current block.
var block_stack; // The stack of blocks.
var declared_globals; // The object containing the global declarations.
var directives; // The directive comments.
var directive_mode; // true if directives are still allowed.
var early_stop; // true if JSLint cannot finish.
var exports; // The exported names and values.
var froms; // The array collecting all import-from strings.
var fudge; // true if the natural numbers start with 1.
var functionage; // The current function.
var functions; // The array containing all of the functions.
var global; // The global object; the outermost context.
var json_mode; // true if parsing JSON.
var lines; // The array containing source lines.
var module_mode; // true if import or export was used.
var next_token; // The next token to be examined in the parse.
var option; // The options parameter.
var property; // The object containing the tallied property names.
var mega_mode; // true if currently parsing a megastring literal.
var stack; // The stack of functions.
var syntax; // The object containing the parser.
var token; // The current token being examined in the parse.
var token_nr; // The number of the next token.
var tokens; // The array of tokens.
var tenure; // The predefined property registry.
var tree; // The abstract parse tree.
var var_mode; // true if using var; false if using let.
var warnings; // The array collecting all generated warnings.
// Error reportage functions:
function artifact(the_token) {
// Return a string representing an artifact.
if (the_token === undefined) {
the_token = next_token;
}
return (the_token.id === "(string)" || the_token.id === "(number)")
? String(the_token.value)
: the_token.id;
}
function artifact_line(the_token) {
// Return the fudged line number of an artifact.
if (the_token === undefined) {
the_token = next_token;
}
return the_token.line + fudge;
}
function artifact_column(the_token) {
// Return the fudged column number of an artifact.
if (the_token === undefined) {
the_token = next_token;
}
return the_token.from + fudge;
}
function warn_at(code, line, column, a, b, c, d) {
// Report an error at some line and column of the program. The warning object
// resembles an exception.
var warning = { // ~~
name: "JSLintError",
column: column,
line: line,
code: code
};
if (a !== undefined) {
warning.a = a;
}
if (b !== undefined) {
warning.b = b;
}
if (c !== undefined) {
warning.c = c;
}
if (d !== undefined) {
warning.d = d;
}
warning.message = supplant(bundle[code] || code, warning);
warnings.push(warning);
return (
typeof option.maxerr === "number"
&& warnings.length === option.maxerr
) ? stop_at("too_many", line, column)
: warning;
}
function stop_at(code, line, column, a, b, c, d) {
// Same as warn_at, except that it stops the analysis.
throw warn_at(code, line, column, a, b, c, d);
}
function warn(code, the_token, a, b, c, d) {
// Same as warn_at, except the warning will be associated with a specific token.
// If there is already a warning on this token, suppress the new one. It is
// likely that the first warning will be the most meaningful.
if (the_token === undefined) {
the_token = next_token;
}
if (the_token.warning === undefined) {
the_token.warning = warn_at(
code,
the_token.line,
the_token.from,
a || artifact(the_token),
b,
c,
d
);
return the_token.warning;
}
}
function stop(code, the_token, a, b, c, d) {
// Similar to warn and stop_at. If the token already had a warning, that
// warning will be replaced with this new one. It is likely that the stopping
// warning will be the more meaningful.
if (the_token === undefined) {
the_token = next_token;
}
delete the_token.warning;
throw warn(code, the_token, a, b, c, d);
}
// Tokenize:
function tokenize(source) {
// tokenize takes a source and produces from it an array of token objects.
// JavaScript is notoriously difficult to tokenize because of the horrible
// interactions between automatic semicolon insertion, regular expression
// literals, and now megastring literals. JSLint benefits from eliminating
// automatic semicolon insertion and nested megastring literals, which allows
// full tokenization to precede parsing.
// If the source is not an array, then it is split into lines at the
// carriage return/linefeed.
lines = (Array.isArray(source))
? source
: source.split(rx_crlf);
tokens = [];
var char; // a popular character
var column = 0; // the column number of the next character
var first; // the first token
var from; // the starting column number of the token
var line = -1; // the line number of the next character
var nr = 0; // the next token number
var previous = global; // the previous token including comments
var prior = global; // the previous token excluding comments
var mega_from; // the starting column of megastring
var mega_line; // the starting line of megastring
var snippet; // a piece of string
var source_line; // the current line source string
function next_line() {
// Put the next line of source in source_line. If the line contains tabs,
// replace them with spaces and give a warning. Also warn if the line contains
// unsafe characters or is too damn long.
var at;
column = 0;
line += 1;
source_line = lines[line];
if (source_line !== undefined) {
at = source_line.search(rx_tab);
if (at >= 0) {
if (!option.white) {
warn_at("use_spaces", line, at + 1);
}
source_line = source_line.replace(rx_tab, " ");
}
at = source_line.search(rx_unsafe);
if (at >= 0) {
warn_at(
"unsafe",
line,
column + at,
"U+" + source_line.charCodeAt(at).toString(16)
);
}
if (option.maxlen && option.maxlen < source_line.length) {
warn_at("too_long", line, source_line.length);
} else if (!option.white && source_line.slice(-1) === " ") {
warn_at(
"unexpected_trailing_space",
line,
source_line.length - 1
);
}
}
return source_line;
}
// Most tokens, including the identifiers, operators, and punctuators, can be
// found with a regular expression. Regular expressions cannot correctly match
// regular expression literals, so we will match those the hard way. String
// literals and number literals can be matched by regular expressions, but they
// don't provide good warnings. The functions snip, next_char, prev_char,
// some_digits, and escape help in the parsing of literals.
function snip() {
// Remove the last character from snippet.
snippet = snippet.slice(0, -1);
}
function next_char(match) {
// Get the next character from the source line. Remove it from the source_line,
// and append it to the snippet. Optionally check that the previous character
// matched an expected value.
if (match !== undefined && char !== match) {
return stop_at(
(char === "")
? "expected_a"
: "expected_a_b",
line,
column - 1,
match,
char
);
}
if (source_line) {
char = source_line.charAt(0);
source_line = source_line.slice(1);
snippet += char;
} else {
char = "";
snippet += " ";
}
column += 1;
return char;
}
function back_char() {
// Back up one character by moving a character from the end of the snippet to
// the front of the source_line.
if (snippet) {
char = snippet.slice(-1);
source_line = char + source_line;
column -= 1;
snip();
} else {
char = "";
}
return char;
}
function some_digits(rx, quiet) {
var result = source_line.match(rx);
if (result) {
char = result[1];
column += char.length;
source_line = result[2];
snippet += char;
} else {
char = "";
if (!quiet) {
warn_at(
"expected_digits_after_a",
line,
column,
snippet
);
}
}
return char.length;
}
function escape(extra) {
switch (next_char("\\")) {
case "\\":
case "/":
case "b":
case "f":
case "n":
case "r":
case "t":
break;
case "u":
if (next_char("u") === "{") {
if (json_mode) {
warn_at("unexpected_a", line, column - 1, char);
}
if (some_digits(rx_hexs) > 5) {
warn_at("too_many_digits", line, column - 1);
}
if (!option.es6) {
warn_at("es6", line, column, "u{");
}
if (next_char() !== "}") {
stop_at("expected_a_before_b", line, column, "}", char);
}
next_char();
return;
}
back_char();
if (some_digits(rx_hexs, true) < 4) {
warn_at("expected_four_digits", line, column - 1);
}
break;
case "":
return stop_at("unclosed_string", line, column);
default:
if (!extra || extra.indexOf(char) < 0) {
warn_at(
"unexpected_a_before_b",
line,
column - 2,
"\\",
char
);
}
}
next_char();
}
function make(id, value, identifier) {
// Make the token object and append it to the tokens list.
var the_token = {
from: from,
id: id,
identifier: !!identifier,
line: line,
nr: nr,
thru: column
};
tokens[nr] = the_token;
nr += 1;
// Directives must appear before the first statement.
if (id !== "(comment)" && id !== ";") {
directive_mode = false;
}
// If the token is to have a value, give it one.
if (value !== undefined) {
the_token.value = value;
}
// If this token is an identifier that touches a preceding number, or
// a "/", comment, or regular expression literal that touches a preceding
// comment or regular expression literal, then give a missing space warning.
// This warning is not suppressed by option.white.
if (
previous.line === line
&& previous.thru === from
&& (id === "(comment)" || id === "(regexp)" || id === "/")
&& (previous.id === "(comment)" || previous.id === "(regexp)")
) {
warn(
"expected_space_a_b",
the_token,
artifact(previous),
artifact(the_token)
);
}
if (previous.id === "." && id === "(number)") {
warn("expected_a_before_b", previous, "0", ".");
}
if (prior.id === "." && the_token.identifier) {
the_token.dot = true;
}
// The previous token is used to detect adjacency problems.
previous = the_token;
// The prior token is a previous token that was not a comment. The prior token
// is used to disambiguate "/", which can mean division or regular expression
// literal.
if (previous.id !== "(comment)") {
prior = previous;
}
return the_token;
}
function parse_directive(the_comment, body) {
// JSLint recognizes three directives that can be encoded in comments. This
// function processes one item, and calls itself recursively to process the
// next one.
var result = body.match(rx_directive_part);
if (result) {
var allowed;
var name = result[1];
var value = result[2];
switch (the_comment.directive) {
case "jslint":
allowed = allowed_option[name];
switch (typeof allowed) {
case "boolean":
case "object":
switch (value) {
case "true":
case "":
case undefined:
option[name] = true;
if (Array.isArray(allowed)) {
populate(declared_globals, allowed, false);
}
break;
case "false":
option[name] = false;
break;
default:
warn(
"bad_option_a",
the_comment,
name + ":" + value
);
}
break;
case "number":
if (isFinite(+value)) {
option[name] = +value;
} else {
warn(
"bad_option_a",
the_comment,
name + ":" + value
);
}
break;
default:
warn("bad_option_a", the_comment, name);
}
break;
case "property":
if (tenure === undefined) {
tenure = empty();
}
tenure[name] = true;
break;
case "global":
if (value) {
warn("bad_option_a", the_comment, name + ":" + value);
}
declared_globals[name] = false;
module_mode = the_comment;
break;
}
return parse_directive(the_comment, result[3]);
}
if (body) {
return stop("bad_directive_a", the_comment, body);
}
}
function comment(snippet) {
// Make a comment object. Comments are not allowed in JSON text. Comments can
// include directives and notices of incompletion.
var the_comment = make("(comment)", snippet);
if (Array.isArray(snippet)) {
snippet = snippet.join(" ");
}
if (!option.devel && rx_todo.test(snippet)) {
warn("todo_comment", the_comment);
}
var result = snippet.match(rx_directive);
if (result) {
if (!directive_mode) {
warn_at("misplaced_directive_a", line, from, result[1]);
} else {
the_comment.directive = result[1];
parse_directive(the_comment, result[2]);
}
directives.push(the_comment);
}
return the_comment;
}
function regexp() {
// Parse a regular expression literal.
var multi_mode = false;
var result;
var value;
function quantifier() {
// Match an optional quantifier.
switch (char) {
case "?":
case "*":
case "+":
next_char();
break;
case "{":
if (some_digits(rx_digits, true) === 0) {
warn_at("expected_a", line, column, "0");
}
if (next_char() === ",") {
some_digits(rx_digits, true);
next_char();
}
next_char("}");
break;
default:
return;
}
if (char === "?") {
next_char("?");
}
}
function subklass() {
// Match a character in a character class.