-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.js
executable file
·1370 lines (1363 loc) · 51.2 KB
/
test.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
var test = function(title, input, callback){
try {
var zeon = new Zeon(input, Zeon.getNewConfig());
zeon.parse();
zeon.startProcess();
callback(zeon);
//out('<b style="color:green;">PASS</b>: '+window.testgroup+': '+title)
} catch(e){
var msg = typeof e == 'string' ? e : 'js error thrown';
out('<b style="color:red;">FAIL</b>: '+window.testgroup+': '+title+': '+msg, typeof e != 'string' ? e : '');
}
};
var assert = function(desc, shouldBeTrue){
if (!shouldBeTrue) throw desc;
};
var reject = function(desc, shouldBeFalse){
if (shouldBeFalse) throw desc;
};
var get = function(zeon,arr){
if (!arr) console.log("missing zeon?");
reject('Has parse error', zeon.hasError);
if (typeof arr == 'string') arr = [arr];
// if arr is a function, it's actually the filter to apply...
return zeon.btree.filter(typeof arr == 'function' ? arr : function(t){ return arr.indexOf(t.value) >= 0; });
};
var out = function(s){
var e = document.createElement('div');
e.innerHTML = s;
document.getElementById('out').appendChild(e);
};
new function(){ testgroup = 'Simple assignments';
test('string',
'var x = \'\';\ny = \'\';\no.z = \'\';',
function(zeon){
reject('A parse error', zeon.hasError);
// checks regular var assignment, var declaration initializer and property assignment. objlit is a different case
var vars = get(zeon,['x','y','z']);
assert('Expecting exactly two tokens here', vars.length == 3);
// testing both regular variable assignment and variable declaration initialization
vars.forEach(function(v){
assert('Should have var type array', v.varType);
assert('Should have tracking object', v.trackingObject);
assert('Should have tracking object vartype', v.trackingObject.varType);
[v.varType, v.trackingObject.varType].forEach(function(vt){
assert('Expecting exactly one type', vt.length == 1);
assert('Should be of type string', vt[0] == 'string');
});
});
}
);
test('number',
'var x = 5;\ny = 5;\no.z = 5;',
function(zeon){
reject('A parse error', zeon.hasError);
// checks regular var assignment, var declaration initializer and property assignment. objlit is a different case
var vars = get(zeon,['x','y','z']);
assert('Expecting exactly two tokens here', vars.length == 3);
// testing both regular variable assignment and variable declaration initialization
vars.forEach(function(v){
assert('Should have var type array', v.varType);
assert('Should have tracking object', v.trackingObject);
assert('Should have tracking object vartype', v.trackingObject.varType);
[v.varType, v.trackingObject.varType].forEach(function(vt){
assert('Expecting exactly one type', vt.length == 1);
assert('Should be of type number', vt[0] == 'number');
});
});
}
);
test('null',
'var x = null;\ny = null;\no.z = null;',
function(zeon){
reject('A parse error', zeon.hasError);
// checks regular var assignment, var declaration initializer and property assignment. objlit is a different case
var vars = get(zeon,['x','y','z']);
assert('Expecting exactly two tokens here', vars.length == 3);
// testing both regular variable assignment and variable declaration initialization
vars.forEach(function(v){
assert('Should have var type array', v.varType);
assert('Should have tracking object', v.trackingObject);
assert('Should have tracking object vartype', v.trackingObject.varType);
[v.varType, v.trackingObject.varType].forEach(function(vt){
assert('Expecting exactly one type', vt.length == 1);
assert('Should be of type null', vt[0] == 'null');
});
});
}
);
test('undefined',
'var x = undefined;\ny = undefined;\no.z = undefined;',
function(zeon){
reject('A parse error', zeon.hasError);
// checks regular var assignment, var declaration initializer and property assignment. objlit is a different case
var vars = get(zeon,['x','y','z']);
assert('Expecting exactly two tokens here', vars.length == 3);
// testing both regular variable assignment and variable declaration initialization
vars.forEach(function(v){
assert('Should have var type array', v.varType);
assert('Should have tracking object', v.trackingObject);
assert('Should have tracking object vartype', v.trackingObject.varType);
[v.varType, v.trackingObject.varType].forEach(function(vt){
assert('Expecting exactly one type', vt.length == 1);
assert('Should be of type undefined', vt[0] == 'undefined');
});
});
}
);
test('boolean true',
'var x = true;\ny = true;\no.z = true;',
function(zeon){
reject('A parse error', zeon.hasError);
// checks regular var assignment, var declaration initializer and property assignment. objlit is a different case
var vars = get(zeon,['x','y','z']);
assert('Expecting exactly two tokens here', vars.length == 3);
// testing both regular variable assignment and variable declaration initialization
vars.forEach(function(v){
assert('Should have var type array', v.varType);
assert('Should have tracking object', v.trackingObject);
assert('Should have tracking object vartype', v.trackingObject.varType);
[v.varType, v.trackingObject.varType].forEach(function(vt){
assert('Expecting exactly one type', vt.length == 1);
assert('Should be of type boolean', vt[0] == 'boolean');
});
});
}
);
test('boolean false',
'var x = false;\ny = false;\no.z = false;',
function(zeon){
reject('A parse error', zeon.hasError);
// checks regular var assignment, var declaration initializer and property assignment. objlit is a different case
var vars = get(zeon,['x','y','z']);
assert('Expecting exactly two tokens here', vars.length == 3);
// testing both regular variable assignment and variable declaration initialization
vars.forEach(function(v){
assert('Should have var type array', v.varType);
assert('Should have tracking object', v.trackingObject);
assert('Should have tracking object vartype', v.trackingObject.varType);
[v.varType, v.trackingObject.varType].forEach(function(vt){
assert('Expecting exactly one type', vt.length == 1);
assert('Should be of type boolean', vt[0] == 'boolean');
});
});
}
);
test('object',
'var x = {};\ny = {};\no.z = {};',
function(zeon){
reject('A parse error', zeon.hasError);
// checks regular var assignment, var declaration initializer and property assignment. objlit is a different case
var vars = get(zeon,['x','y','z']);
assert('Expecting exactly two tokens here', vars.length == 3);
// testing both regular variable assignment and variable declaration initialization
vars.forEach(function(v){
assert('Should have var type array', v.varType);
assert('Should have tracking object', v.trackingObject);
assert('Should have tracking object vartype', v.trackingObject.varType);
[v.varType, v.trackingObject.varType].forEach(function(vt){
assert('Expecting exactly one type', vt.length == 1);
assert('Should be of type Object', vt[0] == 'Object');
});
});
}
);
test('Array',
'var x = [];\ny = [];\no.z = [];',
function(zeon){
reject('A parse error', zeon.hasError);
// checks regular var assignment, var declaration initializer and property assignment. objlit is a different case
var vars = get(zeon,['x','y','z']);
assert('Expecting exactly two tokens here', vars.length == 3);
// testing both regular variable assignment and variable declaration initialization
vars.forEach(function(v){
assert('Should have var type array', v.varType);
assert('Should have tracking object', v.trackingObject);
assert('Should have tracking object vartype', v.trackingObject.varType);
[v.varType, v.trackingObject.varType].forEach(function(vt){
assert('Expecting exactly one type', vt.length == 1);
assert('Should be of type Array', vt[0] == 'Array');
});
});
}
);
test('Function',
'var x = function(){};\ny = function(){};\no.z = function(){};',
function(zeon){
reject('A parse error', zeon.hasError);
// checks regular var assignment, var declaration initializer and property assignment. objlit is a different case
var vars = get(zeon,['x','y','z']);
assert('Expecting exactly two tokens here', vars.length == 3);
// testing both regular variable assignment and variable declaration initialization
vars.forEach(function(v){
assert('Should have var type array', v.varType);
assert('Should have tracking object', v.trackingObject);
assert('Should have tracking object vartype', v.trackingObject.varType);
[v.varType, v.trackingObject.varType].forEach(function(vt){
assert('Expecting exactly one type', vt.length == 1);
assert('Should be of type Function', vt[0] == 'Function');
});
});
}
);
};
new function(){ testgroup = 'Called tokens';
test('Any kind',
'aaa();\nbbb().ccc;\nddd.eee();\nfff.ggg().hhh;\niii().jjj();\nkkk[x]();\nlll[x].mmm();\nnnn.ooo()[x];',
function(zeon){
var vars = zeon.btree.filter(function(t){ return t.value.length == 3; }); // all var names in input are 3 letters...
assert('expecting aaa ~ ooo', vars.length == 15);
vars.forEach(function(token){
switch (token.value) {
case 'aaa':
assert('Token is called', token.tokenIsCalled);
assert('Has called target token', token.calledTargetToken);
assert('Called target token is itself', token.calledTargetToken == token);
break;
case 'bbb':
case 'eee': // overflows, == same
case 'ggg': // overflows, == same
case 'mmm': // overflows, == same
case 'ooo': // overflows, == same
assert('Token is called', token.tokenIsCalled);
reject('Has no called target token', token.calledTargetToken);
break;
case 'ccc':
case 'fff': // overflows, == same
case 'hhh': // overflows, == same
case 'kkk': // overflows, == same
case 'nnn': // overflows, == same
reject('Token is not called', token.tokenIsCalled);
reject('Has no called target token', token.calledTargetToken);
break;
case 'ddd':
reject('Token is not called', token.tokenIsCalled);
assert('Has called target token', token.calledTargetToken);
assert('Called target token is eee property', token.calledTargetToken.value == 'eee');
break;
case 'iii':
assert('Token is called', token.tokenIsCalled);
assert('Has called target token', token.calledTargetToken);
assert('Called target token is jjj property', token.calledTargetToken.value == 'jjj');
break;
case 'lll':
reject('Token is not called', token.tokenIsCalled);
assert('Has called target token', token.calledTargetToken);
assert('Called target token is mmm property', token.calledTargetToken.value == 'mmm');
break;
}
},this);
}
);
}
new function(){ testgroup = 'Warnings';
var hasWarning = function(zeon, tokenValue, title, okBefore){
var arr = get(zeon, tokenValue);
assert('query has items', arr.length);
arr.forEach(function(o,i){
// okBefore default is undefined, so >= will return false in that case, causing the assert to be the default
if (i >= okBefore) reject('has not '+(i||''), (o.warnings||[]).indexOf(title) >= 0);
else assert('has '+(i||''), (o.warnings||[]).indexOf(title) >= 0);
});
};
var noWarning = function(zeon, tokenValue, title, failBefore){
var arr = get(zeon, tokenValue);
assert('query has items', arr.length);
arr.forEach(function(o,i){
if (i >= failBefore) assert('has not '+(i||''), (o.warnings||[]).indexOf(title) >= 0);
else reject('has '+(i||''), (o.warnings||[]).indexOf(title) >= 0);
});
};
test('weak comparison',
'5 == "string";',
function(zeon){
hasWarning(zeon, '==', 'weak comparison');
}
);
test('weak comparison',
'5 === "string";',
function(zeon){
noWarning(zeon, '===', 'weak comparison');
}
);
test('typeof always string',
'typeof 5 == "string";',
function(zeon){
noWarning(zeon, '==', 'typeof always string');
}
);
test('typeof strong comparison',
'typeof 5 === "string";',
function(zeon){
hasWarning(zeon, '===', 'typeof always string');
}
);
test('missing block good',
'if (x) y;',
function(zeon){
hasWarning(zeon, ')', 'missing block good');
}
);
test('missing block bad',
'if (x)\n\ty;',
function(zeon){
hasWarning(zeon, ')', 'missing block bad');
}
);
test('assignment in header',
'if (x=5) y;',
function(zeon){
hasWarning(zeon, '=', 'assignment in header');
}
);
test('weak comparison',
'foo == bar',
function(zeon){
hasWarning(zeon, '==', 'weak comparison');
}
);
test('dangling underscore',
'foo_;',
function(zeon){
hasWarning(zeon, 'foo_', 'dangling underscore');
}
);
test('dot and not can be confusing',
'var r = /foo./;\nvar s = /foo[^x]/;\nvar t = /foo[.]/;\nvar u = /fo\\^oo/;\nvar w = /foo[b^r]/;',
function(zeon){
hasWarning(zeon, function(t){ return t.name == 1/*regex*/; }, 'dot and not can be confusing', 3);
}
);
test('inc dec operator',
'a++;++b;--c;d--;',
function(zeon){
hasWarning(zeon, ['++','--'], 'inc dec operator');
}
);
test('comma in group makes inc/dec fail',
'++(a,b); --(c,d); (e,f)++; (g,h)--;',
function(zeon){
hasWarning(zeon, ['++', '--'], 'comma in group makes inc/dec fail');
}
);
test('binary operator',
'~a|b&c^d<<e>>>f>>g',
function(zeon){
hasWarning(zeon, ['~','|','&','^','<<','<<<','>>'], 'binary operator');
}
);
test('use dot access',
'x["foo"];',
function(zeon){
hasWarning(zeon, '[', 'use dot access');
}
);
test('continue only in loops',
'a;continue;b; while(x) continue;',
function(zeon){
hasWarning(zeon, 'continue', 'continue only in loops', 1);
}
);
test('break needs loop or label',
'a;break;b; while(x) break; foo: break foo;',
function(zeon){
hasWarning(zeon, 'break', 'break needs loop or label', 1);
}
);
test('return only in function',
'foo;return;bar;function f(){ return; }',
function(zeon){
hasWarning(zeon, 'return', 'return only in function', 1);
}
);
test('trailing decimal',
'5.;',
function(zeon){
hasWarning(zeon, '5.', 'trailing decimal');
}
);
test('leading decimal',
'.50;',
function(zeon){
hasWarning(zeon, '.50', 'leading decimal');
}
);
test('regex confusion',
'a /= b;',
function(zeon){
hasWarning(zeon, '/=', 'regex confusion');
}
);
test('number dot',
'3..foo; 3 .foo;',
function(zeon){
hasWarning(zeon, '.', 'number dot');
}
);
test('assignment this',
'this = 5;',
function(zeon){
hasWarning(zeon, '=', 'assignment this');
}
);
test('bad string escapement',
'"foo\\qbar";', // also add and test for proper escapements
function(zeon){
hasWarning(zeon, function(t){ return t.isString; }, 'bad string escapement');
}
);
test('unlikely regex escapement',
'/fo\\o/;',
function(zeon){ // also add and test for proper escapements
hasWarning(zeon, function(t){ return t.name == 1/*regex*/; }, 'unlikely regex escapement');
}
);
test('avoid hex',
'0xf00;500;',
function(zeon){
hasWarning(zeon, function(t){ return t.isNumber; }, 'avoid hex', 1);
}
);
test('caller callee',
'foo.caller; foo.callee;',
function(zeon){
hasWarning(zeon, ['caller', 'callee'], 'caller callee');
}
);
/*
test('octal escape',
'09900;',
function(zeon){
hasWarning(zeon, '09900', 'octal escape');
}
);
*/
/*
test('00',
'00;0000;0000900;9000;',
function(zeon){
zeon.btree.filter(function(o){ return o.isNumber; }).forEach(function(o,i){
if (i < 3) assert('has '+i, (o.warnings||[]).indexOf('00') >= 0);
else reject('has '+i, (o.warnings||[]).indexOf('0') >= 0);
});
}
);
*/
test('regexp call',
'/foo/(bar);',
function(zeon){
hasWarning(zeon, '(', 'regexp call');
}
);
test('confusing plusses',
'a++ +b; c+ ++d; e+ +f; g++ + ++h;',
function(zeon){
get(zeon,['++', '+']).forEach(function(o,i){
if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8) assert('has '+i, (o.warnings||[]).indexOf('confusing plusses') >= 0);
else reject('has not '+i, (o.warnings||[]).indexOf('confusing plusses') >= 0);
});
}
);
test('confusing minusses',
'a-- -b; c- --c; e- -f; g-- - --h;',
function(zeon){
get(zeon,['--', '-']).forEach(function(o,i){
if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8) assert('has '+i, (o.warnings||[]).indexOf('confusing minusses') >= 0);
else reject('has not '+i, (o.warnings||[]).indexOf('confusing minusses') >= 0);
});
}
);
/*
test('double bang',
'!!x',
function(zeon){
get(zeon,['!']).forEach(function(o,i){
console.log(i, o)
if (i > 0) assert('has '+i, (o.warnings||[]).indexOf('double bang') >= 0);
else reject('has not '+i, (o.warnings||[]).indexOf('double bang') >= 0);
});
}
);
*/
test('unsafe char',
'"foo\u0001bar";/foo\u0604bar/;',
function(zeon){
hasWarning(zeon, function(t){ return t.value != ';'; }, 'unsafe char');
}
);
test('control char',
'"foo\u0001bar";/foo\u0001bar/;',
function(zeon){
hasWarning(zeon, function(t){ return t.value != ';'; }, 'control char');
}
);
test('invalid unicode escape in string',
'"foo\\ubar";"foo\\uabar";',
function(zeon){
hasWarning(zeon, function(t){ return t.value != ';'; }, 'invalid unicode escape in string');
}
);
test('invalid unicode escape in regex',
'/foo\\ubar/;/foo\\ubara/;',
function(zeon){
hasWarning(zeon, function(t){ return t.value != ';'; }, 'invalid unicode escape in regex');
}
);
test('invalid hex escape in string',
'"foo\\xbr";"foo\\xrally";',
function(zeon){
hasWarning(zeon, function(t){ return t.isString; }, 'invalid hex escape in string');
}
);
test('invalid hex escape in regex',
'/foo\\xbr/;/foo\\xrally/;',
function(zeon){
hasWarning(zeon,function(t){ return t.name == 1/*regex*/; }, 'invalid hex escape in regex');
}
);
test('catch var assignment',
'try { } catch (e) { e=5; }',
function(zeon){
hasWarning(zeon, '=', 'catch var assignment');
}
);
test('bad constructor',
'new String();',
function(zeon){
hasWarning(zeon, 'String', 'bad constructor');
}
);
test('array constructor',
'new Array(); new Array(5); new Array("5");',
function(zeon){
hasWarning(zeon, 'Array', 'array constructor');
}
);
test('error constructor',
'new Error();',
function(zeon){
hasWarning(zeon, 'Error', 'error constructor');
}
);
test('very bad constructor',
'new Math();',
function(zeon){
hasWarning(zeon, 'Math', 'very bad constructor');
}
);
test('function is eval',
'new Function();',
function(zeon){
hasWarning(zeon, 'Function', 'Function is eval');
}
);
test('function wrapped',
'var x = (function(){}); (function(){}); (function(){}());',
function(zeon){
hasWarning(zeon, function(t){ return t.isGroupStart; }, 'function wrapped', 2);
}
);
test('document.write',
'document.write("evil");',
function(zeon){
hasWarning(zeon, 'write', 'document.write');
}
);
test('iteration function',
'while(x) x=function(){ foo; }; for(var i=0; i<5; ++i) alert(function(){});', // todo: maybe add more than just this version?
function(zeon){
hasWarning(zeon, 'function', 'iteration function');
}
);
test('empty block',
'{}; if(x){};',
function(zeon){
hasWarning(zeon, '{', 'empty block');
}
);
test('eval',
'eval("devil");',
function(zeon){
hasWarning(zeon, 'eval', 'eval');
}
);
test('extra comma',
'[a,,b]; [a,,,b]; [a,,,];',
function(zeon){
var i = 0;
hasWarning(zeon, function(o){ if (o.value == ',') ++i; return i==1 || i==3 || i==4 || i==6 || i==7; }, 'extra comma');
}
);
test('double new',
'new new Foo;',
function(zeon){
noWarning(zeon, 'new', 'double new', 1);
}
);
test('double delete',
'delete delete foo;',
function(zeon){
noWarning(zeon, 'delete', 'double delete', 1);
}
);
test('undefined',
'var x = undefined;',
function(zeon){
hasWarning(zeon, 'undefined', 'undefined');
}
);
test('duplicate objlit prop',
'x={a:1,a:1};',
function(zeon){
noWarning(zeon, 'a', 'duplicate objlit prop', 1);
}
);
test('timer eval',
'setTimeout("foo", 1); var x = "foo"; setTimeout(x, 1);',
function(zeon){
hasWarning(zeon, 'setTimeout', 'timer eval');
}
);
test('group vars',
'var a; var b;',
function(zeon){
noWarning(zeon, 'var', 'group vars', 1);
}
);
test('func decl at top',
'x; function f(){}',
function(zeon){
hasWarning(zeon, 'function', 'func decl at top');
}
);
test('is label',
'foo: bar;',
function(zeon){
hasWarning(zeon, 'foo', 'is label');
}
);
test('math call',
'Math();',
function(zeon){
hasWarning(zeon, '(', 'math call');
}
);
test('new wants parens',
'new Foo; new Bar();',
function(zeon){
hasWarning(zeon, 'new', 'new wants parens', 1);
}
);
test('missing radix',
'parseInt("foo"); parseInt("foo", 5);',
function(zeon){
hasWarning(zeon, 'parseInt', 'missing radix', 1);
}
);
test('nested comment',
'/* /* */\n// /*\n// */\n// /* */\n/* */',
function(zeon){
// we must do it the hard way because we need to target wtree, not btree
var comments = zeon.wtree.filter(function(t){ return t.isComment; });
assert('has items', comments.length);
comments.some(function(t,i){
if (i < 4) return reject('has not '+i, !t.warnings || t.warnings.indexOf('nested comment') < 0);
return assert('has '+i, !t.warnings || t.warnings.indexOf('nested comment') < 0);
});
}
);
test('new statement',
'new Foo;',
function(zeon){
hasWarning(zeon, 'new', 'new statement');
}
);
test('dont use __proto__',
'foo.__proto__; __proto__ = 5;',
function(zeon){
hasWarning(zeon, '__proTo__', 'dont use __proto__');
}
);
test('empty switch',
'switch (e){}',
function(zeon){
hasWarning(zeon, 'switch', 'empty switch');
}
);
test('quasi empty switch',
'switch (x){ default: break; }',
function(zeon){
hasWarning(zeon, 'switch', 'quasi empty switch');
}
);
test('empty clause',
'switch(x){ case x: }',
function(zeon){
hasWarning(zeon, 'case', 'empty clause');
}
);
test('clause should break',
'switch(x){ case foo: alert(); case bar: fail; } switch(y){ default: foo; case x: bar; } switch(n){ case x: x; default: y; }',
function(zeon){
hasWarning(zeon, ['default','case'], 'clause should break');
}
);
test('clause should break (2)',
'function a() {\n\tswitch (x) {\n\t\tcase y:\n\t\t\tif (data) return;\n\t\t\telse break;\n\t\tcase y:\n\t\t\toops;\n\t\tcase z:\n\t\t\tnope;\n\n\t}\n}\n',
function(zeon){
noWarning(zeon, ['case'], 'clause should break', 1);
}
);
test('switch is an if',
'switch (x) { case foo: y; }',
function(zeon){
hasWarning(zeon, 'switch', 'switch is an if');
}
);
test('unwrapped for-in (1)',
'for (key in obj) alert(f);',
function(zeon){
hasWarning(zeon, 'for', 'unwrapped for-in');
}
);
test('unwrapped for-in (2)',
'for (x in y) { more; }',
function(zeon){
hasWarning(zeon, 'for', 'unwrapped for-in');
}
);
test('unwrapped for-in (3)',
'for (a in b) if (b.hasOwnProperty(a)) { stuff; }',
function(zeon){
noWarning(zeon, 'for', 'unwrapped for-in');
}
);
test('unwrapped for-in (4)',
'for (c in d) { if (b.hasOwnProperty(a)) stuff; fail; }',
function(zeon){
noWarning(zeon, 'for', 'unwrapped for-in');
}
);
test('in out of for',
'x in y;',
function(zeon){
hasWarning(zeon, 'in', 'in out of for');
}
);
test('use {}',
'new Object();',
function(zeon){
hasWarning(zeon, 'Object', 'use {}');
}
);
test('use []',
'new Array();',
function(zeon){
hasWarning(zeon, 'Array', 'use []');
}
);
test('double block',
'{{ i; think; }}',
function(zeon){
hasWarning(zeon, '{', 'double block', 1);
}
);
test('useless block',
'function f(){{}} {}',
function(zeon){
noWarning(zeon, '{', 'useless block', 1);
}
);
test('use capital namespacing',
'new foo; new Bar;',
function(zeon){
hasWarning(zeon, ['foo', 'Bar'], 'use capital namespacing', 1);
}
);
test('constructor called as a function (1)',
'function f(){} f.prototype = 5; f();',
function(zeon){
noWarning(zeon, 'f', 'constructor called as function', 2);
}
);
test('constructor called as a function (2)',
'function g(){}; new g; g();',
function(zeon){
noWarning(zeon, 'g', 'constructor called as function', 2);
}
);
test('cannot inc/dec on call expression',
'++a(); --b(); c()++; d()--;',
function(zeon){
hasWarning(zeon, ['++','--'], 'cannot inc/dec on call expression');
}
);
test('inc/dec only valid on vars',
'++"foo"; --"foo"; "foo"++; "foo"--; ++5; --5; 5--; 5++; ++true; --true; true++; true--; ++false; --false; false++; false--; ++null; --null; null++; null--; ++/foo/; --/foo/; /foo/++; /foo/--;',
function(zeon){
hasWarning(zeon, ['++','--'], 'inc/dec only valid on vars');
}
);
test('bad asi pattern',
'x\n/* foo */(function(){}); a\n(b);',
function(zeon){
hasWarning(zeon, function(t){ return t.isCallExpressionStart; }, 'bad asi pattern');
}
);
test('unlikely typeof result',
'typeof x == "crap"; typeof x == "string"',
function(zeon){
hasWarning(zeon, function(t){ return t.isString; }, 'unlikely typeof result', 1);
}
);
test('weird typeof op',
'typeof x % "foo"; typeof x in y; typeof x == y;',
function(zeon){
hasWarning(zeon, ['%','in','=='], 'weird typeof op', 2);
}
);
test('typeof always string',
'typeof x === "string"; typeof x !== "string"; typeof x == "string";',
function(zeon){
hasWarning(zeon, ['===','!==','=='], 'typeof always string', 2);
}
);
test('static expression',
'5+5; 5-"5"; "x"=="y"; true%/t/; / / / / /; (2^1)-(3>>4);(5&8)*x;',
function(zeon){
hasWarning(zeon, ['+','-','==','%','/','*','&','^','>>'], 'static expression', 9);
}
);
test('static condition',
'if (5) y; if(5+5)y;',
function(zeon){
hasWarning(zeon, '(', 'static condition');
}
);
test('pragma requires name parameter',
'//#define\n//#ifdef\n', // tofix: others
function(zeon){
// we must do it the hard way because we need to target wtree, not btree
var comments = zeon.wtree.filter(function(t){ return t.isComment; });
assert('has items', comments.length);
comments.some(function(t,i){
return assert('has '+i, t.warnings && t.warnings.indexOf('pragma requires name parameter') >= 0);
});
}
);
test('pragma requires value parameter',
'//#macro foo ',
function(zeon){
// we must do it the hard way because we need to target wtree, not btree
var comments = zeon.wtree.filter(function(t){ return t.isComment; });
assert('has items', comments.length);
comments.forEach(function(t,i){
assert('has '+i, t.warnings && t.warnings.indexOf('pragma requires value parameter') >= 0);
});
}
);
test('missing ifdef',
'//#endif\n', // tofix: need to add way more
function(zeon){
// we must do it the hard way because we need to target wtree, not btree
var comments = zeon.wtree.filter(function(t){ return t.isComment; });
assert('has items', comments.length);
comments.forEach(function(t,i){
assert('has '+i, t.warnings && t.warnings.indexOf('missing ifdef') >= 0);
});
}
);
test('missing inline',
'//#endline',
function(zeon){
// we must do it the hard way because we need to target wtree, not btree
var comments = zeon.wtree.filter(function(t){ return t.isComment; });
assert('has items', comments.length);
comments.forEach(function(t,i){
assert('has '+i, t.warnings && t.warnings.indexOf('missing inline') >= 0);
});
}
);
test('pragma start missing end',
'//#ifdef foo\n//#inline foo',
function(zeon){
// we must do it the hard way because we need to target wtree, not btree
var comments = zeon.wtree.filter(function(t){ return t.isComment; });
assert('has items', comments.length == 2);
comments.forEach(function(t,i){
assert('has '+i, t.warnings && t.warnings.indexOf('pragma start missing end') >= 0);
});
}
);
test('macro name should be identifier',
'//#macro fail-ing 4\n//#macro ok 6\n//#macro also.ok 7',
function(zeon){
// we must do it the hard way because we need to target wtree, not btree
var comments = zeon.wtree.filter(function(t){ return t.isComment; });
assert('has items', comments.length);
comments.forEach(function(t,i){
if (i == 0) assert('has '+i, t.warnings && t.warnings.indexOf('macro name should be identifier') >= 0);
else reject('has not '+i, t.warnings && t.warnings.indexOf('macro name should be identifier') >= 0);
});
}
);
test('is dev relic',
'foo; bar; baz; tmp; log; console; test; alert; temp;',
function(zeon){
hasWarning(zeon, function(t){ return t.name == 2/*identifier*/; }, 'is dev relic');
}
);
test('multiple operators on same level',
'5+5*5; 5*5+5; 5+5+5; (5+5)*5; 5+(5*5);',
function(zeon){
hasWarning(zeon, ['+','*'], 'multiple operators on same level', 6);
}
);
test('useless multiple throw args',
'throw foo,bar;',
function(zeon){
hasWarning(zeon, 'throw', 'useless multiple throw args');
}
);
test('unnecessary parentheses',
'typeof(x); throw(x); return(x); delete(x); new(x); void(x);',
function(zeon){
hasWarning(zeon, '(', 'unnecessary parentheses');
}
);
test('uninitialized value in loop (1)',
'var x; while (x) x = 5;',
function(zeon){
noWarning(zeon, 'x', 'uninitialized value in loop', 1);
}
);
test('uninitialized value in loop (2)',
'var y; for (;5;) alert(y);',
function(zeon){
noWarning(zeon, 'y', 'uninitialized value in loop', 1);
}
);
test('uninitialized value in loop (3)',
'var z; while (5) z();',
function(zeon){
noWarning(zeon, 'z', 'uninitialized value in loop');
}
);
test('uninitialized value in loop (4)',
'var a = 5; while (a) a = 5;',
function(zeon){
noWarning(zeon, 'a', 'uninitialized value in loop', 2);
}
);
test('uninitialized value in loop (5)',
'var b = 5; while (5) alert(b);',
function(zeon){
noWarning(zeon, '(', 'uninitialized value in loop', 1);
}
);
test('uninitialized value in loop (6)',
'var c = 5; while (5) c();',
function(zeon){
noWarning(zeon, 'c', 'uninitialized value in loop',1);
}
);
test('jsdoc mismatch',
'/** @param {number} foo **/function f(foo){ foo="str"; }',
function(zeon){
assert('has', (zeon.btree.filter(function(t){ return t.value == '==='; })[0].warnings||[]).indexOf('typeof always string') >= 0);
}
);
test('prop not declared on proto',
'FIXME todo',
function(zeon){
assert('has', (zeon.btree.filter(function(t){ return t.value == '==='; })[0].warnings||[]).indexOf('typeof always string') >= 0);
}
);
test('trailing comma',
'[a,];x={a:1,};',
function(zeon){
hasWarning(zeon, ',', 'trailing comma');
}
);
test('asi',
'foo\n{bar}baz',
function(zeon){
assert('has', (zeon.btree.filter(function(t){ return t.value == '==='; })[0].warnings||[]).indexOf('typeof always string') >= 0);
}
);
test('empty statement',
';if(x);',
function(zeon){
hasWarning(zeon, ';', 'empty statement');
}
);
test('premature usage',
'x; var x;',
function(zeon){
hasWarning(zeon, ',', 'trailing comma');