1 | ion = require '../'
|
2 | index = require '../compiler'
|
3 |
|
4 | tests =
|
5 | "let x = 10": """
|
6 | 'use strict';
|
7 | let x = 10;
|
8 | """
|
9 | """
|
10 | for name, value of foo
|
11 | console.log(name + value)
|
12 | """: """
|
13 | 'use strict';
|
14 | for (let name in foo) {
|
15 | let value = foo[name];
|
16 | console.log(name + value);
|
17 | }
|
18 | """
|
19 | """
|
20 | for let name, value of {a:1,b:2,c:3}
|
21 | console.log(name + value)
|
22 | """: """
|
23 | 'use strict';
|
24 | {
|
25 | let _ref = {
|
26 | a: 1,
|
27 | b: 2,
|
28 | c: 3
|
29 | };
|
30 | for (let name in _ref) {
|
31 | let value = _ref[name];
|
32 | console.log(name + value);
|
33 | }
|
34 | }
|
35 | """
|
36 | """
|
37 | for let name in ["a","b","c"]
|
38 | console.log(name)
|
39 | """: """
|
40 | 'use strict';
|
41 | {
|
42 | let _ref = [
|
43 | 'a',
|
44 | 'b',
|
45 | 'c'
|
46 | ];
|
47 | for (let _i = 0; _i < _ref.length; _i++) {
|
48 | let name = _ref[_i];
|
49 | console.log(name);
|
50 | }
|
51 | }
|
52 | """
|
53 | """
|
54 | for name, index in ["a","b","c"]
|
55 | console.log(name)
|
56 | """: """
|
57 | 'use strict';
|
58 | {
|
59 | let _ref = [
|
60 | 'a',
|
61 | 'b',
|
62 | 'c'
|
63 | ];
|
64 | for (let _i = 0; _i < _ref.length; _i++) {
|
65 | let index = _i;
|
66 | let name = _ref[_i];
|
67 | console.log(name);
|
68 | }
|
69 | }
|
70 | """
|
71 | """
|
72 | let object =
|
73 | x: 1
|
74 | y: 2
|
75 | foo:
|
76 | z: 3
|
77 | """: """
|
78 | 'use strict';
|
79 | let object = {
|
80 | x: 1,
|
81 | y: 2,
|
82 | foo: { z: 3 }
|
83 | };
|
84 | """
|
85 | """
|
86 | let array = []
|
87 | 1
|
88 | 2
|
89 | 3
|
90 | """: """
|
91 | 'use strict';
|
92 | let array = [
|
93 | 1,
|
94 | 2,
|
95 | 3
|
96 | ];
|
97 | """
|
98 | """
|
99 | let kids = []
|
100 | {}
|
101 | name: "Alpha"
|
102 | age: 10
|
103 | {}
|
104 | name: "Beta"
|
105 | age: 8
|
106 | """: """
|
107 | 'use strict';
|
108 | let kids = [
|
109 | {
|
110 | name: 'Alpha',
|
111 | age: 10
|
112 | },
|
113 | {
|
114 | name: 'Beta',
|
115 | age: 8
|
116 | }
|
117 | ];
|
118 | """
|
119 | """
|
120 | try
|
121 | doSomething(1)
|
122 | catch e
|
123 | log(e)
|
124 | """: """
|
125 | 'use strict';
|
126 | try {
|
127 | doSomething(1);
|
128 | } catch (e) {
|
129 | log(e);
|
130 | }
|
131 | """
|
132 | """
|
133 | try
|
134 | doSomething(1)
|
135 | finally
|
136 | log(e)
|
137 | """: """
|
138 | 'use strict';
|
139 | try {
|
140 | doSomething(1);
|
141 | } finally {
|
142 | log(e);
|
143 | }
|
144 | """
|
145 | """
|
146 | try
|
147 | doSomething(1)
|
148 | catch e
|
149 | console.error(e)
|
150 | finally
|
151 | log(e)
|
152 | """: """
|
153 | 'use strict';
|
154 | try {
|
155 | doSomething(1);
|
156 | } catch (e) {
|
157 | console.error(e);
|
158 | } finally {
|
159 | log(e);
|
160 | }
|
161 | """
|
162 | """
|
163 | for key, name of foo
|
164 | if name is 'a'
|
165 | break
|
166 | else if name is 'b'
|
167 | continue
|
168 | else if name is 'c'
|
169 | return
|
170 | else if name is 'd'
|
171 | throw new Error("D")
|
172 | else
|
173 | return
|
174 | x: 1
|
175 | y: 2
|
176 | """: """
|
177 | 'use strict';
|
178 | for (let key in foo) {
|
179 | let name = foo[key];
|
180 | if (name === 'a') {
|
181 | break;
|
182 | } else if (name === 'b') {
|
183 | continue;
|
184 | } else if (name === 'c') {
|
185 | return;
|
186 | } else if (name === 'd') {
|
187 | throw new Error('D');
|
188 | } else {
|
189 | return {
|
190 | x: 1,
|
191 | y: 2
|
192 | };
|
193 | }
|
194 | }
|
195 | """
|
196 | """
|
197 | console.log("Hello {{name}}")
|
198 | """: """
|
199 | 'use strict';
|
200 | console.log('Hello ' + name);
|
201 | """
|
202 | """
|
203 | console.log("{{name}}")
|
204 | """: """
|
205 | 'use strict';
|
206 | console.log('' + name);
|
207 | """
|
208 | """
|
209 | console.log("{{ 1 }}{{ 2 }}")
|
210 | """: """
|
211 | 'use strict';
|
212 | console.log('' + 1 + 2);
|
213 | """
|
214 | """
|
215 | return ""
|
216 | <html>
|
217 | <head><title>{{ title }}</title></head>
|
218 | <body>
|
219 | {{ body }}
|
220 | </body>
|
221 | </html>
|
222 | """: """
|
223 | 'use strict';
|
224 | return '<html>\\n <head><title>' + title + '</title></head>\\n <body>\\n ' + body + '\\n </body>\\n</html>';
|
225 | """
|
226 | """
|
227 | return ''
|
228 | <html>
|
229 | <head><title>{{ title }}</title></head>
|
230 | <body>
|
231 | {{ body }}
|
232 | </body>
|
233 | </html>
|
234 | """: """
|
235 | 'use strict';
|
236 | return '<html>\\n <head><title>{{ title }}</title></head>\\n <body>\\n {{ body }}\\n </body>\\n</html>';
|
237 | """
|
238 | """
|
239 | do -> x
|
240 | """: """
|
241 | 'use strict';
|
242 | (function () {
|
243 | return x;
|
244 | }());
|
245 | """
|
246 | """
|
247 | do (x, y) => x + y
|
248 | """: """
|
249 | 'use strict';
|
250 | const ion = require('ion');
|
251 | ion.bind(function (x, y) {
|
252 | return x + y;
|
253 | }, this)(x, y);
|
254 | """
|
255 | """
|
256 | const ion = import "ion"
|
257 | """: """
|
258 | 'use strict';
|
259 | const ion = require('ion');
|
260 | """
|
261 | """
|
262 | export
|
263 | secret: 97542
|
264 | """: """
|
265 | 'use strict';
|
266 | module.exports = exports = { secret: 97542 };
|
267 | """
|
268 | """
|
269 | export let x = 1, y = 2
|
270 | """: """
|
271 | 'use strict';
|
272 | let x = exports.x = 1, y = exports.y = 2;
|
273 | """
|
274 | """
|
275 | export const
|
276 | x = 1
|
277 | y = 2
|
278 | z = 3
|
279 | """: """
|
280 | 'use strict';
|
281 | const x = exports.x = 1, y = exports.y = 2, z = exports.z = 3;
|
282 | """
|
283 | """
|
284 | let {x,y} = {x:1,y:2}
|
285 | """: """
|
286 | 'use strict';
|
287 | let _ref = {
|
288 | x: 1,
|
289 | y: 2
|
290 | };
|
291 | let x = _ref.x;
|
292 | let y = _ref.y;
|
293 | """
|
294 | """
|
295 | for key, {x:[a,b],y:{c:d}} of points
|
296 | console.log(x, y)
|
297 | """: """
|
298 | 'use strict';
|
299 | for (let key in points) {
|
300 | let _ref = points[key];
|
301 | let a = _ref.x[0];
|
302 | let b = _ref.x[1];
|
303 | let d = _ref.y.c;
|
304 | console.log(x, y);
|
305 | }
|
306 | """
|
307 | """
|
308 | for {x:[a,b],y:{c:d}}, index in points
|
309 | console.log(x, y)
|
310 | """: """
|
311 | 'use strict';
|
312 | for (let _i = 0; _i < points.length; _i++) {
|
313 | let index = _i;
|
314 | let _ref = points[_i];
|
315 | let a = _ref.x[0];
|
316 | let b = _ref.x[1];
|
317 | let d = _ref.y.c;
|
318 | console.log(x, y);
|
319 | }
|
320 | """
|
321 | """
|
322 | foo ? bar
|
323 | """: """
|
324 | 'use strict';
|
325 | foo != null ? foo : bar;
|
326 | """
|
327 | """
|
328 | foo ?? bar
|
329 | """: """
|
330 | 'use strict';
|
331 | foo != void 0 ? foo : bar;
|
332 | """
|
333 | """
|
334 | let x
|
335 | x ?= y
|
336 | """: """
|
337 | 'use strict';
|
338 | let x;
|
339 | x = x != null ? x : y;
|
340 | """
|
341 | """
|
342 | let x
|
343 | x ??= y
|
344 | """: """
|
345 | 'use strict';
|
346 | let x;
|
347 | x = x != void 0 ? x : y;
|
348 | """
|
349 | """
|
350 | for const x, index in foo
|
351 | log(x)
|
352 | """: """
|
353 | 'use strict';
|
354 | for (let _i = 0; _i < foo.length; _i++) {
|
355 | const index = _i;
|
356 | const x = foo[_i];
|
357 | log(x);
|
358 | }
|
359 | """
|
360 | """
|
361 | let x = 1, y = 2
|
362 | [x,y] = [y,x]
|
363 | """: """
|
364 | 'use strict';
|
365 | let x = 1, y = 2;
|
366 | const _ref = [
|
367 | y,
|
368 | x
|
369 | ];
|
370 | x = _ref[0];
|
371 | y = _ref[1];
|
372 | """
|
373 | """
|
374 | a?.b
|
375 | """: """
|
376 | 'use strict';
|
377 | a != null ? a.b : void 0;
|
378 | """
|
379 | """
|
380 | a?.b.c?.d
|
381 | """: """
|
382 | 'use strict';
|
383 | a != null ? a.b.c != null ? a.b.c.d : void 0 : void 0;
|
384 | """
|
385 | """
|
386 | a?()
|
387 | """: """
|
388 | 'use strict';
|
389 | a != null ? a() : void 0;
|
390 | """
|
391 | """
|
392 | a?.b?.c?()
|
393 | """: """
|
394 | 'use strict';
|
395 | a != null ? a.b != null ? a.b.c != null ? a.b.c() : void 0 : void 0 : void 0;
|
396 | """
|
397 | """
|
398 | a?.b().c?()
|
399 | """: """
|
400 | 'use strict';
|
401 | a != null ? a.b().c != null ? a.b().c() : void 0 : void 0;
|
402 | """
|
403 | """
|
404 | let y = (x) -> 2
|
405 | """: """
|
406 | 'use strict';
|
407 | let y = function (x) {
|
408 | return 2;
|
409 | };
|
410 | """
|
411 | """
|
412 | s?
|
413 | """: """
|
414 | 'use strict';
|
415 | s != null;
|
416 | """
|
417 | """
|
418 | # also test comments
|
419 | let regex = /foo/
|
420 | """: """
|
421 | 'use strict';
|
422 | let regex = /foo/;
|
423 | """
|
424 | """
|
425 | for let i = 0; i < 10; i++
|
426 | console.log(i)
|
427 | """: """
|
428 | 'use strict';
|
429 | for (let i = 0; i < 10; i++) {
|
430 | console.log(i);
|
431 | }
|
432 | """
|
433 | """
|
434 | for key of object if key[0] isnt '_' for c in key
|
435 | console.log(c)
|
436 | """: """
|
437 | 'use strict';
|
438 | for (let key in object) {
|
439 | if (key[0] !== '_') {
|
440 | for (let _i = 0; _i < key.length; _i++) {
|
441 | let c = key[_i];
|
442 | console.log(c);
|
443 | }
|
444 | }
|
445 | }
|
446 | """
|
447 | """
|
448 | console.log([key for key of object if key is cool])
|
449 | """: """
|
450 | 'use strict';
|
451 | let _ref = [];
|
452 | for (let key in object) {
|
453 | if (key === cool) {
|
454 | _ref.push(key);
|
455 | }
|
456 | }
|
457 | console.log(_ref);
|
458 | """
|
459 | """
|
460 | console.log(
|
461 | 1
|
462 | 2
|
463 | {}
|
464 | x: 1
|
465 | y: 2
|
466 | )
|
467 | """: """
|
468 | 'use strict';
|
469 | console.log(1, 2, {
|
470 | x: 1,
|
471 | y: 2
|
472 | });
|
473 | """
|
474 | """
|
475 | let x = ->
|
476 | try
|
477 | foo()
|
478 | bar()
|
479 | catch e
|
480 | baz()
|
481 | """: """
|
482 | 'use strict';
|
483 | let x = function () {
|
484 | try {
|
485 | foo();
|
486 | bar();
|
487 | } catch (e) {
|
488 | baz();
|
489 | }
|
490 | };
|
491 | """
|
492 |
|
493 | """
|
494 | if foo
|
495 | # bar
|
496 | """: """
|
497 | 'use strict';
|
498 | if (foo) {
|
499 | }
|
500 | """
|
501 |
|
502 | """
|
503 | let trim = (a = "") -> a.trim()
|
504 | """: """
|
505 | 'use strict';
|
506 | let trim = function (a) {
|
507 | if (a == null)
|
508 | a = '';
|
509 | return a.trim();
|
510 | };
|
511 | """
|
512 |
|
513 | """
|
514 | foo(
|
515 | 1
|
516 | 2
|
517 | )
|
518 | """: """
|
519 | 'use strict';
|
520 | foo(1, 2);
|
521 | """
|
522 | """
|
523 | compile(
|
524 | foo: 1
|
525 | bar: 2
|
526 | baz:
|
527 | a: 1
|
528 | b: 2
|
529 | )
|
530 | """: """
|
531 | 'use strict';
|
532 | compile({
|
533 | foo: 1,
|
534 | bar: 2,
|
535 | baz: {
|
536 | a: 1,
|
537 | b: 2
|
538 | }
|
539 | });
|
540 | """
|
541 | """
|
542 | let array = [1,2,3]
|
543 | 4
|
544 | 5
|
545 | 6
|
546 | """: """
|
547 | 'use strict';
|
548 | let array = [
|
549 | 1,
|
550 | 2,
|
551 | 3,
|
552 | 4,
|
553 | 5,
|
554 | 6
|
555 | ];
|
556 | """
|
557 | """
|
558 | let point = new Point(10, 20)
|
559 | z: 30
|
560 | """: """
|
561 | 'use strict';
|
562 | const ion = require('ion');
|
563 | let point = ion.patch(new Point(10, 20), { z: 30 });
|
564 | """
|
565 | """
|
566 | let object = {x:1, y:2}
|
567 | z: 3
|
568 | """: """
|
569 | 'use strict';
|
570 | let object = {
|
571 | x: 1,
|
572 | y: 2,
|
573 | z: 3
|
574 | };
|
575 | """
|
576 | """
|
577 | let origin = new Point
|
578 | x: 1
|
579 | y: 2
|
580 | """: """
|
581 | 'use strict';
|
582 | const ion = require('ion');
|
583 | let origin = ion.patch(new Point(), {
|
584 | x: 1,
|
585 | y: 2
|
586 | });
|
587 | """
|
588 | """
|
589 | let origin = new Line
|
590 | a: new Point
|
591 | x: 0
|
592 | y: 0
|
593 | b: new Point
|
594 | x: 10
|
595 | y: 20
|
596 | """: """
|
597 | 'use strict';
|
598 | const ion = require('ion');
|
599 | let origin = ion.patch(new Line(), {
|
600 | a: ion.patch(new Point(), {
|
601 | x: 0,
|
602 | y: 0
|
603 | }),
|
604 | b: ion.patch(new Point(), {
|
605 | x: 10,
|
606 | y: 20
|
607 | })
|
608 | });
|
609 | """
|
610 | """
|
611 | input:
|
612 | # ignore this comment
|
613 | x: 10
|
614 | y: 20
|
615 | z:
|
616 | # also ignore this one
|
617 | a: 1
|
618 | b: 2
|
619 | w: new Point
|
620 | x: 0
|
621 | y: 0
|
622 | """: """
|
623 | 'use strict';
|
624 | const ion = require('ion');
|
625 | {
|
626 | input.x = 10;
|
627 | input.y = 20;
|
628 | input.z = ion.patch(input.z, {
|
629 | a: 1,
|
630 | b: 2
|
631 | });
|
632 | input.w = ion.patch(new Point(), {
|
633 | x: 0,
|
634 | y: 0
|
635 | });
|
636 | }
|
637 | """
|
638 | """
|
639 | let point = new Point
|
640 | [x]: 1
|
641 | [y]: 2
|
642 | """: """
|
643 | 'use strict';
|
644 | let point = new Point();
|
645 | {
|
646 | point[x] = 1;
|
647 | point[y] = 2;
|
648 | }
|
649 | """
|
650 | """
|
651 | let self = @
|
652 | let x = @x
|
653 | let y = @.y
|
654 | let z = this.z
|
655 | """: """
|
656 | 'use strict';
|
657 | let self = this;
|
658 | let x = this.x;
|
659 | let y = this.y;
|
660 | let z = this.z;
|
661 | """
|
662 | """
|
663 | let x = {}
|
664 | [key]: value
|
665 | """: """
|
666 | 'use strict';
|
667 | let x = {};
|
668 | x[key] = value;
|
669 | """
|
670 | """
|
671 | if foo
|
672 | return {}
|
673 | for key, value of object
|
674 | [key]: value
|
675 | """: """
|
676 | 'use strict';
|
677 | if (foo) {
|
678 | let _ref = {};
|
679 | for (let key in object) {
|
680 | let value = object[key];
|
681 | _ref[key] = value;
|
682 | }
|
683 | return _ref;
|
684 | }
|
685 | """
|
686 |
|
687 | """
|
688 | for x, y, z of foo
|
689 | log(foo)
|
690 | """: {line:1, column:11}
|
691 | """
|
692 | export let x
|
693 | """: {line:1, column:12}
|
694 | """
|
695 | export const x
|
696 | """: {line:1, column:14}
|
697 | """
|
698 | export const x = 1
|
699 | export {y:2}
|
700 | """: {line:2,column:1}
|
701 | """
|
702 | const x = 1
|
703 | x = 2
|
704 | """: {line:2, column:1}
|
705 | """
|
706 | const double = (x) ->
|
707 | x *= 2
|
708 | return x
|
709 | """: """
|
710 | 'use strict';
|
711 | const double = function (x) {
|
712 | x *= 2;
|
713 | return x;
|
714 | };
|
715 | """
|
716 | """
|
717 | x = 1
|
718 | """: {line:1, column:1}
|
719 | """
|
720 | let x = 1
|
721 | let x = 2
|
722 | """: {line:2, column:5}
|
723 |
|
724 | """
|
725 | let x = 1
|
726 | const double = (x) ->
|
727 | return x
|
728 | """: """
|
729 | 'use strict';
|
730 | let x = 1;
|
731 | const double = function (x) {
|
732 | return x;
|
733 | };
|
734 | """
|
735 | """
|
736 | console.log(x)
|
737 | if a
|
738 | let x = 1
|
739 | """: {line:1, column:13}
|
740 | """
|
741 | if typeof a is 'string' and void a and delete a.b
|
742 | log(a)
|
743 | """: """
|
744 | 'use strict';
|
745 | if (typeof a === 'string' && void a && delete a.b) {
|
746 | log(a);
|
747 | }
|
748 | """
|
749 |
|
750 | """
|
751 | if 1
|
752 | # 1
|
753 | # 2
|
754 | x = 12
|
755 | """: {line:4, column:5}
|
756 |
|
757 | """
|
758 | export const
|
759 | BlockStatement =
|
760 | isBlock: true
|
761 | newScope: tr ue
|
762 | """: {line:4, column:22}
|
763 | """
|
764 | export class Foo extends import 'Bar'
|
765 | constructor: (x,y) ->
|
766 | @x = x
|
767 | @y = y
|
768 | properties:
|
769 | x: 1
|
770 | y: 2
|
771 | getXY: -> [@x,@y]
|
772 | isThisPropertyStatic: true
|
773 | """: """
|
774 | 'use strict';
|
775 | const ion = require('ion');
|
776 | const Foo = ion.defineClass({
|
777 | name: 'Foo',
|
778 | constructor: function Foo(x, y) {
|
779 | this.x = x;
|
780 | this.y = y;
|
781 | },
|
782 | properties: {
|
783 | x: 1,
|
784 | y: 2,
|
785 | getXY: function () {
|
786 | return [
|
787 | this.x,
|
788 | this.y
|
789 | ];
|
790 | }
|
791 | },
|
792 | isThisPropertyStatic: true
|
793 | }, require('Bar'));
|
794 | module.exports = exports = Foo;
|
795 | """
|
796 | """
|
797 | const double(a) -> a * 2
|
798 | """: """
|
799 | 'use strict';
|
800 | function double(a) {
|
801 | return a * 2;
|
802 | }
|
803 | """
|
804 | """
|
805 | const double(a) -> a * 2
|
806 | double = 12
|
807 | """: {line:2, column: 1}
|
808 | """
|
809 | let object =
|
810 | const double(a) -> a * 2
|
811 | if a
|
812 | [key]: value
|
813 | else
|
814 | foo: double(2)
|
815 | """: """
|
816 | 'use strict';
|
817 | let object = {};
|
818 | {
|
819 | function double(a) {
|
820 | return a * 2;
|
821 | }
|
822 | if (a) {
|
823 | object[key] = value;
|
824 | } else {
|
825 | object.foo = double(2);
|
826 | }
|
827 | }
|
828 | """
|
829 | """
|
830 | let items = []
|
831 | for key, value of window
|
832 | value
|
833 | """: """
|
834 | 'use strict';
|
835 | let items = [];
|
836 | for (let key in window) {
|
837 | let value = window[key];
|
838 | items.push(value);
|
839 | }
|
840 | """
|
841 | """
|
842 | let foo = div()
|
843 | span()
|
844 | 'Hello'
|
845 | """: """
|
846 | 'use strict';
|
847 | const ion = require('ion');
|
848 | let foo = div();
|
849 | let _ref = span();
|
850 | ion.add(_ref, 'Hello');
|
851 | ion.add(foo, _ref);
|
852 | """
|
853 |
|
854 | """
|
855 | const ion = import './'
|
856 | let foo = div()
|
857 | span()
|
858 | 'Hello'
|
859 | """: """
|
860 | 'use strict';
|
861 | const ion = require('./');
|
862 | let foo = div();
|
863 | let _ref = span();
|
864 | ion.add(_ref, 'Hello');
|
865 | ion.add(foo, _ref);
|
866 | """
|
867 | """
|
868 | const translate({x,y}) ->
|
869 | x++
|
870 | y++
|
871 | return {x,y}
|
872 | """: """
|
873 | 'use strict';
|
874 | function translate(_ref) {
|
875 | let x = _ref.x;
|
876 | let y = _ref.y;
|
877 | x++;
|
878 | y++;
|
879 | return {
|
880 | x: x,
|
881 | y: y
|
882 | };
|
883 | }
|
884 | """
|
885 | """
|
886 | let x = foo(
|
887 | ''
|
888 | multiline string literal
|
889 | ""
|
890 | multiline string template
|
891 | )
|
892 | """: """
|
893 | 'use strict';
|
894 | let x = foo('multiline string literal', 'multiline string template');
|
895 | """
|
896 | """
|
897 | assert x is 2
|
898 | """: """
|
899 | 'use strict';
|
900 | if (!(x === 2))
|
901 | throw new Error('Assertion Failed: (x is 2)');
|
902 | """
|
903 | """
|
904 | export class Point
|
905 | constructor: ->
|
906 | # call super with arguments object
|
907 | super
|
908 | # call super again with explicit arguments
|
909 | super(width, height)
|
910 | # calling twice is silly, but legal
|
911 | properties:
|
912 | x: 0
|
913 | y: 0
|
914 | superIdentifier: (x, y) -> super
|
915 | superExplicit: (a, b) -> super(a, b)
|
916 | """: """
|
917 | 'use strict';
|
918 | const ion = require('ion');
|
919 | const Point = ion.defineClass({
|
920 | name: 'Point',
|
921 | constructor: function Point() {
|
922 | Point.super.apply(this, arguments);
|
923 | Point.super.call(this, width, height);
|
924 | },
|
925 | properties: {
|
926 | x: 0,
|
927 | y: 0,
|
928 | superIdentifier: function (x, y) {
|
929 | return Point.super.prototype.superIdentifier.apply(this, arguments);
|
930 | },
|
931 | superExplicit: function (a, b) {
|
932 | return Point.super.prototype.superExplicit.call(this, a, b);
|
933 | }
|
934 | }
|
935 | });
|
936 | module.exports = exports = Point;
|
937 | """
|
938 | """
|
939 | const spreadFunction1(a, b, ...c) ->
|
940 | log(1)
|
941 | const spreadFunction2(a, b, ...c, d, e) ->
|
942 | log(2)
|
943 | const spreadFunction3(a,b, ...c, {d,e}) ->
|
944 | log(3)
|
945 | """: """
|
946 | 'use strict';
|
947 | function spreadFunction1(a, b, ___c) {
|
948 | let c = Array.prototype.slice.call(arguments, 2);
|
949 | log(1);
|
950 | }
|
951 | function spreadFunction2(a, b, ___c, d, e) {
|
952 | let c = Array.prototype.slice.call(arguments, 2, arguments.length - 2);
|
953 | d = arguments[arguments.length - 2];
|
954 | e = arguments[arguments.length - 1];
|
955 | log(2);
|
956 | }
|
957 | function spreadFunction3(a, b, ___c, _ref) {
|
958 | let c = Array.prototype.slice.call(arguments, 2, arguments.length - 1);
|
959 | _ref = arguments[arguments.length - 1];
|
960 | let d = _ref.d;
|
961 | let e = _ref.e;
|
962 | log(3);
|
963 | }
|
964 | """
|
965 | """
|
966 | # default value for a should be set before b
|
967 | const foo(a = 0, b = a) -> a + b
|
968 | """: """
|
969 | 'use strict';
|
970 | function foo(a, b) {
|
971 | if (a == null)
|
972 | a = 0;
|
973 | if (b == null)
|
974 | b = a;
|
975 | return a + b;
|
976 | }
|
977 | """
|
978 | """
|
979 | export template ->
|
980 | # cannot define classes in templates
|
981 | class Poo
|
982 | """: {line: 3, column: 5}
|
983 | """
|
984 | export template ->
|
985 | # cannot for loop in templates
|
986 | for let i = 0; i < 10; i++
|
987 | console.log(i)
|
988 | """: {line: 3, column: 5}
|
989 | """
|
990 | export template ->
|
991 | # cannot export in templates
|
992 | export x
|
993 | """: {line: 3, column: 5}
|
994 | """
|
995 | export template ->
|
996 | # cannot try/catch in templates
|
997 | try
|
998 | return 0
|
999 | catch e
|
1000 | return 1
|
1001 | """: {line: 3, column: 5}
|
1002 | """
|
1003 | export template ->
|
1004 | # cannot throw errors in templates
|
1005 | throw new Error
|
1006 | """: {line: 3, column: 5}
|
1007 |
|
1008 |
|
1009 |
|
1010 |
|
1011 | """
|
1012 | export template ->
|
1013 | const x = 12
|
1014 | # cannot assign to const variables, make sure enforced within template
|
1015 | x = 10
|
1016 | return x
|
1017 | """: {line: 4, column: 5}
|
1018 | """
|
1019 | export template ->
|
1020 | let x = 12
|
1021 | # cannot assign to let variables either.
|
1022 | x = 12
|
1023 | return x
|
1024 | """: {line: 4, column: 5}
|
1025 | """
|
1026 | export template ->
|
1027 | let x = {y:10}
|
1028 | # cannot assign to anything really.
|
1029 | x.y = 12
|
1030 | return x.y
|
1031 | """: {line: 4, column: 5}
|
1032 | """
|
1033 | export template (a) ->
|
1034 | # cannot assign to parameters either
|
1035 | a = 10
|
1036 | return a
|
1037 | """: {line: 3, column: 5}
|
1038 | """
|
1039 | export class Foo
|
1040 | constructor: ->
|
1041 | # there was a problem with existential operators not processing within class definitions
|
1042 | if properties?
|
1043 | log(properties)
|
1044 | """: """
|
1045 | 'use strict';
|
1046 | const ion = require('ion');
|
1047 | const Foo = ion.defineClass({
|
1048 | name: 'Foo',
|
1049 | constructor: function Foo() {
|
1050 | if (properties != null) {
|
1051 | log(properties);
|
1052 | }
|
1053 | }
|
1054 | });
|
1055 | module.exports = exports = Foo;
|
1056 | """
|
1057 | """
|
1058 | const ctor = @@
|
1059 | const ctorName = @@name
|
1060 | """: """
|
1061 | 'use strict';
|
1062 | const ctor = this.constructor;
|
1063 | const ctorName = this.constructor.name;
|
1064 | """
|
1065 | """
|
1066 | const inlineThrow() -> throw new Error('inline throw')
|
1067 | """: """
|
1068 | 'use strict';
|
1069 | function inlineThrow() {
|
1070 | throw new Error('inline throw');
|
1071 | }
|
1072 | """
|
1073 | """
|
1074 | class DynamicExpression
|
1075 | watchValue: ->
|
1076 | let x = @x ?= []
|
1077 | """: """
|
1078 | 'use strict';
|
1079 | const ion = require('ion');
|
1080 | const DynamicExpression = ion.defineClass({
|
1081 | name: 'DynamicExpression',
|
1082 | watchValue: function () {
|
1083 | let x = this.x = this.x != null ? this.x : [];
|
1084 | }
|
1085 | });
|
1086 | DynamicExpression;
|
1087 | """
|
1088 | """
|
1089 | let a = new Point(
|
1090 | 1
|
1091 | 2
|
1092 | )
|
1093 | """: """
|
1094 | 'use strict';
|
1095 | let a = new Point(1, 2);
|
1096 | """
|
1097 | """
|
1098 | let x = [y for y in z]
|
1099 | """: """
|
1100 | 'use strict';
|
1101 | let _ref = [];
|
1102 | for (let _i = 0; _i < z.length; _i++) {
|
1103 | let y = z[_i];
|
1104 | _ref.push(y);
|
1105 | }
|
1106 | let x = _ref;
|
1107 | """
|
1108 | """
|
1109 | return
|
1110 | z: []
|
1111 | let items = [3,2,1]
|
1112 | for item in items
|
1113 | item * 2
|
1114 | """: """
|
1115 | 'use strict';
|
1116 | let _ref = [];
|
1117 | {
|
1118 | let items = [
|
1119 | 3,
|
1120 | 2,
|
1121 | 1
|
1122 | ];
|
1123 | for (let _i = 0; _i < items.length; _i++) {
|
1124 | let item = items[_i];
|
1125 | _ref.push(item * 2);
|
1126 | }
|
1127 | }
|
1128 | return { z: _ref };
|
1129 | """
|
1130 | """
|
1131 | let x = `y == null`
|
1132 | """: """
|
1133 | 'use strict';
|
1134 | let x = y == null;
|
1135 | """
|
1136 | """
|
1137 | # should get accurate error locations even from inline javascript expressions
|
1138 | let x = `y := null`
|
1139 | """: {line: 2, column: 13}
|
1140 | """
|
1141 | let x = 0 in Array
|
1142 | let y = "foo" instanceof String
|
1143 | """: """
|
1144 | 'use strict';
|
1145 | let x = 0 in Array;
|
1146 | let y = 'foo' instanceof String;
|
1147 | """
|
1148 | """
|
1149 | let output
|
1150 | output :=
|
1151 | x: 1
|
1152 | y: 2
|
1153 | """: """
|
1154 | 'use strict';
|
1155 | const ion = require('ion');
|
1156 | let output;
|
1157 | output = ion.patch(output, {
|
1158 | x: 1,
|
1159 | y: 2
|
1160 | });
|
1161 | """
|
1162 | """
|
1163 | output:
|
1164 | for a in b
|
1165 | [c]: d
|
1166 | """: """
|
1167 | 'use strict';
|
1168 | for (let _i = 0; _i < b.length; _i++) {
|
1169 | let a = b[_i];
|
1170 | output[c] = d;
|
1171 | }
|
1172 | """
|
1173 | """
|
1174 | output: {}
|
1175 | x: 1
|
1176 | """: {line: 1, column: 9}
|
1177 | """
|
1178 | [output]:
|
1179 | x: 1
|
1180 | """: {line: 1, column: 2}
|
1181 | """
|
1182 | #
|
1183 | #
|
1184 |
|
1185 | #
|
1186 | """: """
|
1187 | 'use strict';
|
1188 | """
|
1189 | """
|
1190 | [a for a in b]
|
1191 | [a for a in c]
|
1192 | """: """
|
1193 | 'use strict';
|
1194 | let _ref = [];
|
1195 | for (let _i = 0; _i < b.length; _i++) {
|
1196 | let a = b[_i];
|
1197 | _ref.push(a);
|
1198 | }
|
1199 | _ref;
|
1200 | let _ref2 = [];
|
1201 | for (let _i2 = 0; _i2 < c.length; _i2++) {
|
1202 | let a = c[_i2];
|
1203 | _ref2.push(a);
|
1204 | }
|
1205 | _ref2;
|
1206 | """
|
1207 |
|
1208 |
|
1209 |
|
1210 |
|
1211 |
|
1212 |
|
1213 | """
|
1214 | let array = []
|
1215 | 1, 0, 0
|
1216 | 0, 1, 0
|
1217 | 0, 0, 1
|
1218 | """: """
|
1219 | 'use strict';
|
1220 | let array = [
|
1221 | 1,
|
1222 | 0,
|
1223 | 0,
|
1224 | 0,
|
1225 | 1,
|
1226 | 0,
|
1227 | 0,
|
1228 | 0,
|
1229 | 1
|
1230 | ];
|
1231 | """
|
1232 | """
|
1233 | import(foo).bar
|
1234 | """: """
|
1235 | 'use strict';
|
1236 | require(foo).bar;
|
1237 | """
|
1238 | """
|
1239 | let x = []
|
1240 | ->
|
1241 | """: """
|
1242 | 'use strict';
|
1243 | let x = [function () {
|
1244 | }];
|
1245 | """
|
1246 | """
|
1247 | x:
|
1248 | delete: true
|
1249 | """: """
|
1250 | 'use strict';
|
1251 | x.delete = true;
|
1252 | """
|
1253 | """
|
1254 | return
|
1255 | style:
|
1256 | fontSize: "0.7em"
|
1257 | "delete"
|
1258 | """: """
|
1259 | 'use strict';
|
1260 | const ion = require('ion');
|
1261 | let _ref = {};
|
1262 | {
|
1263 | _ref.style = ion.patch(_ref.style, { fontSize: '0.7em' });
|
1264 | ion.add(_ref, 'delete');
|
1265 | }
|
1266 | return _ref;
|
1267 | """
|
1268 | """
|
1269 | content:
|
1270 | name: 'foo'
|
1271 | 1
|
1272 | 2
|
1273 | """: """
|
1274 | 'use strict';
|
1275 | const ion = require('ion');
|
1276 | {
|
1277 | content.name = 'foo';
|
1278 | ion.add(content, 1);
|
1279 | ion.add(content, 2);
|
1280 | }
|
1281 | """
|
1282 | """
|
1283 | for name, file of directory
|
1284 | write(name, file)
|
1285 | else
|
1286 | delete(name)
|
1287 | """: """
|
1288 | 'use strict';
|
1289 | for (let name in directory) {
|
1290 | let file = directory[name];
|
1291 | write(name, file);
|
1292 | }
|
1293 | """
|
1294 |
|
1295 |
|
1296 |
|
1297 |
|
1298 |
|
1299 | """
|
1300 | foo(
|
1301 | bar()
|
1302 | baz(
|
1303 | 1
|
1304 | 2
|
1305 | )
|
1306 | )
|
1307 | """: """
|
1308 | 'use strict';
|
1309 | foo(bar(), baz(1, 2));
|
1310 | """
|
1311 |
|
1312 | "\n console.log('ion')": """
|
1313 | 'use strict';
|
1314 | console.log('ion');
|
1315 | """
|
1316 |
|
1317 |
|
1318 |
|
1319 |
|
1320 |
|
1321 |
|
1322 | if global.window?
|
1323 | return
|
1324 |
|
1325 | exports.test = ->
|
1326 | for input, expected of tests
|
1327 | options = {target:'es6'}
|
1328 | if expected is null
|
1329 | loc = false
|
1330 | console.log '---------------------------------------------------'
|
1331 | console.log JSON.stringify index.compile(input, ion.patch({postprocess:false,loc:loc}, options)), null, ' '
|
1332 | console.log '-Postprocessed-------------------------------------'
|
1333 | console.log JSON.stringify index.compile(input, ion.patch({generate:false,loc:loc}, options)), null, ' '
|
1334 | console.log '---------------------------------------------------'
|
1335 | console.log index.compile input, ion.patch({loc:loc}, options)
|
1336 | else if typeof expected is 'object'
|
1337 |
|
1338 | error = null
|
1339 | try
|
1340 | index.compile input, options
|
1341 | catch e
|
1342 | error = e
|
1343 |
|
1344 | for key, value of expected
|
1345 | if value isnt e[key]
|
1346 | throw new Error "\n#{JSON.stringify e}\n!=\n#{JSON.stringify expected}"
|
1347 | if not error?
|
1348 | throw new Error "Expected an error: #{JSON.stringify expected}"
|
1349 | else
|
1350 | output = index.compile input, options
|
1351 | if output.trim() isnt expected.trim()
|
1352 | console.log '-Output---------------------------------------------'
|
1353 | console.log output
|
1354 | throw new Error "\n#{output}\n!=\n#{expected}"
|
1355 | return
|