File: modules.coffee

package info (click to toggle)
coffeescript 1.12.8~dfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,740 kB
  • sloc: makefile: 53; xml: 9; sh: 6
file content (917 lines) | stat: -rw-r--r-- 19,441 bytes parent folder | download | duplicates (2)
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
# Modules, a.k.a. ES2015 import/export
# ------------------------------------
#
# Remember, we’re not *resolving* modules, just outputting valid ES2015 syntax.


# This is the CoffeeScript import and export syntax, closely modeled after the ES2015 syntax
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export

# import "module-name"
# import defaultMember from "module-name"
# import * as name from "module-name"
# import { } from "module-name"
# import { member } from "module-name"
# import { member as alias } from "module-name"
# import { member1, member2 as alias2, … } from "module-name"
# import defaultMember, * as name from "module-name"
# import defaultMember, { … } from "module-name"

# export default expression
# export class name
# export { }
# export { name }
# export { name as exportedName }
# export { name as default }
# export { name1, name2 as exportedName2, name3 as default, … }
#
# export * from "module-name"
# export { … } from "module-name"
#
# As a subsitute for `export var name = …` and `export function name {}`,
# CoffeeScript also supports:
# export name = …

# CoffeeScript also supports optional commas within `{ … }`.


# Import statements

test "backticked import statement", ->
  input = """
    if Meteor.isServer
      `import { foo, bar as baz } from 'lib'`"""
  output = """
    if (Meteor.isServer) {
      import { foo, bar as baz } from 'lib';
    }"""
  eq toJS(input), output

test "import an entire module for side effects only, without importing any bindings", ->
  input = "import 'lib'"
  output = "import 'lib';"
  eq toJS(input), output

test "import default member from module, adding the member to the current scope", ->
  input = """
    import foo from 'lib'
    foo.fooMethod()"""
  output = """
    import foo from 'lib';

    foo.fooMethod();"""
  eq toJS(input), output

test "import an entire module's contents as an alias, adding the alias to the current scope", ->
  input = """
    import * as foo from 'lib'
    foo.fooMethod()"""
  output = """
    import * as foo from 'lib';

    foo.fooMethod();"""
  eq toJS(input), output

test "import empty object", ->
  input = "import { } from 'lib'"
  output = "import {} from 'lib';"
  eq toJS(input), output

test "import empty object", ->
  input = "import {} from 'lib'"
  output = "import {} from 'lib';"
  eq toJS(input), output

test "import a single member of a module, adding the member to the current scope", ->
  input = """
    import { foo } from 'lib'
    foo.fooMethod()"""
  output = """
    import {
      foo
    } from 'lib';

    foo.fooMethod();"""
  eq toJS(input), output

test "import a single member of a module as an alias, adding the alias to the current scope", ->
  input = """
    import { foo as bar } from 'lib'
    bar.barMethod()"""
  output = """
    import {
      foo as bar
    } from 'lib';

    bar.barMethod();"""
  eq toJS(input), output

test "import multiple members of a module, adding the members to the current scope", ->
  input = """
    import { foo, bar } from 'lib'
    foo.fooMethod()
    bar.barMethod()"""
  output = """
    import {
      foo,
      bar
    } from 'lib';

    foo.fooMethod();

    bar.barMethod();"""
  eq toJS(input), output

test "import multiple members of a module where some are aliased, adding the members or aliases to the current scope", ->
  input = """
    import { foo, bar as baz } from 'lib'
    foo.fooMethod()
    baz.bazMethod()"""
  output = """
    import {
      foo,
      bar as baz
    } from 'lib';

    foo.fooMethod();

    baz.bazMethod();"""
  eq toJS(input), output

test "import default member and other members of a module, adding the members to the current scope", ->
  input = """
    import foo, { bar, baz as qux } from 'lib'
    foo.fooMethod()
    bar.barMethod()
    qux.quxMethod()"""
  output = """
    import foo, {
      bar,
      baz as qux
    } from 'lib';

    foo.fooMethod();

    bar.barMethod();

    qux.quxMethod();"""
  eq toJS(input), output

test "import default member from a module as well as the entire module's contents as an alias, adding the member and alias to the current scope", ->
  input = """
    import foo, * as bar from 'lib'
    foo.fooMethod()
    bar.barMethod()"""
  output = """
    import foo, * as bar from 'lib';

    foo.fooMethod();

    bar.barMethod();"""
  eq toJS(input), output

test "multiline simple import", ->
  input = """
    import {
      foo,
      bar as baz
    } from 'lib'"""
  output = """
    import {
      foo,
      bar as baz
    } from 'lib';"""
  eq toJS(input), output

test "multiline complex import", ->
  input = """
    import foo, {
      bar,
      baz as qux
    } from 'lib'"""
  output = """
    import foo, {
      bar,
      baz as qux
    } from 'lib';"""
  eq toJS(input), output

test "import with optional commas", ->
  input = "import { foo, bar, } from 'lib'"
  output = """
    import {
      foo,
      bar
    } from 'lib';"""
  eq toJS(input), output

test "multiline import without commas", ->
  input = """
    import {
      foo
      bar
    } from 'lib'"""
  output = """
    import {
      foo,
      bar
    } from 'lib';"""
  eq toJS(input), output

test "multiline import with optional commas", ->
  input = """
    import {
      foo,
      bar,
    } from 'lib'"""
  output = """
    import {
      foo,
      bar
    } from 'lib';"""
  eq toJS(input), output

test "a variable can be assigned after an import", ->
  input = """
    import { foo } from 'lib'
    bar = 5"""
  output = """
    var bar;

    import {
      foo
    } from 'lib';

    bar = 5;"""
  eq toJS(input), output

test "variables can be assigned before and after an import", ->
  input = """
    foo = 5
    import { bar } from 'lib'
    baz = 7"""
  output = """
    var baz, foo;

    foo = 5;

    import {
      bar
    } from 'lib';

    baz = 7;"""
  eq toJS(input), output

# Export statements

test "export empty object", ->
  input = "export { }"
  output = "export {};"
  eq toJS(input), output

test "export empty object", ->
  input = "export {}"
  output = "export {};"
  eq toJS(input), output

test "export named members within an object", ->
  input = "export { foo, bar }"
  output = """
    export {
      foo,
      bar
    };"""
  eq toJS(input), output

test "export named members as aliases, within an object", ->
  input = "export { foo as bar, baz as qux }"
  output = """
    export {
      foo as bar,
      baz as qux
    };"""
  eq toJS(input), output

test "export named members within an object, with an optional comma", ->
  input = "export { foo, bar, }"
  output = """
    export {
      foo,
      bar
    };"""
  eq toJS(input), output

test "multiline export named members within an object", ->
  input = """
    export {
      foo,
      bar
    }"""
  output = """
    export {
      foo,
      bar
    };"""
  eq toJS(input), output

test "multiline export named members within an object, with an optional comma", ->
  input = """
    export {
      foo,
      bar,
    }"""
  output = """
    export {
      foo,
      bar
    };"""
  eq toJS(input), output

test "export default string", ->
  input = "export default 'foo'"
  output = "export default 'foo';"
  eq toJS(input), output

test "export default number", ->
  input = "export default 5"
  output = "export default 5;"
  eq toJS(input), output

test "export default object", ->
  input = "export default { foo: 'bar', baz: 'qux' }"
  output = """
    export default {
      foo: 'bar',
      baz: 'qux'
    };"""
  eq toJS(input), output

test "export default implicit object", ->
  input = "export default foo: 'bar', baz: 'qux'"
  output = """
    export default {
      foo: 'bar',
      baz: 'qux'
    };"""
  eq toJS(input), output

test "export default multiline implicit object", ->
  input = """
    export default
      foo: 'bar'
      baz: 'qux'
    """
  output = """
    export default {
      foo: 'bar',
      baz: 'qux'
    };"""
  eq toJS(input), output

test "export default multiline implicit object with internal braces", ->
  input = """
    export default
      foo: yes
      bar: {
        baz
      }
      quz: no
    """
  output = """
    export default {
      foo: true,
      bar: {
        baz: baz
      },
      quz: false
    };"""
  eq toJS(input), output

test "export default assignment expression", ->
  input = "export default foo = 'bar'"
  output = """
    var foo;

    export default foo = 'bar';"""
  eq toJS(input), output

test "export assignment expression", ->
  input = "export foo = 'bar'"
  output = "export var foo = 'bar';"
  eq toJS(input), output

test "export multiline assignment expression", ->
  input = """
    export foo =
    'bar'"""
  output = "export var foo = 'bar';"
  eq toJS(input), output

test "export multiline indented assignment expression", ->
  input = """
    export foo =
      'bar'"""
  output = "export var foo = 'bar';"
  eq toJS(input), output

test "export default function", ->
  input = "export default ->"
  output = "export default function() {};"
  eq toJS(input), output

test "export default multiline function", ->
  input = """
    export default (foo) ->
      console.log foo"""
  output = """
    export default function(foo) {
      return console.log(foo);
    };"""
  eq toJS(input), output

test "export assignment function", ->
  input = """
    export foo = (bar) ->
      console.log bar"""
  output = """
    export var foo = function(bar) {
      return console.log(bar);
    };"""
  eq toJS(input), output

test "export assignment function which contains assignments in its body", ->
  input = """
    export foo = (bar) ->
      baz = '!'
      console.log bar + baz"""
  output = """
    export var foo = function(bar) {
      var baz;
      baz = '!';
      return console.log(bar + baz);
    };"""
  eq toJS(input), output

test "export default predefined function", ->
  input = """
    foo = (bar) ->
      console.log bar
    export default foo"""
  output = """
    var foo;

    foo = function(bar) {
      return console.log(bar);
    };

    export default foo;"""
  eq toJS(input), output

# Uncomment this test once ES2015+ `class` support is added

# test "export default class", ->
#   input = """
#     export default class foo extends bar
#       baz: ->
#         console.log 'hello, world!'"""
#   output = """
#     export default class foo extends bar {
#       baz: function {
#         return console.log('hello, world!');
#       }
#     }"""
#   eq toJS(input), output

# Very limited tests for now, testing that `export class foo` either compiles
# identically (ES2015+) or at least into some function, leaving the specifics
# vague in case the CoffeeScript `class` interpretation changes
test "export class", ->
  input = """
    export class foo
      baz: ->
        console.log 'hello, world!'"""
  output = toJS input
  ok /^export (class foo|var foo = \(function)/.test toJS input

test "export class that extends", ->
  input = """
    export class foo extends bar
      baz: ->
        console.log 'hello, world!'"""
  output = toJS input
  ok /export (class foo|var foo = \(function)/.test(output) and \
    not /var foo(;|,)/.test output

test "export default class that extends", ->
  input = """
    export default class foo extends bar
      baz: ->
        console.log 'hello, world!'"""
  ok /export default (class foo|foo = \(function)/.test toJS input

test "export default named member, within an object", ->
  input = "export { foo as default, bar }"
  output = """
    export {
      foo as default,
      bar
    };"""
  eq toJS(input), output


# Import and export in the same statement

test "export an entire module's contents", ->
  input = "export * from 'lib'"
  output = "export * from 'lib';"
  eq toJS(input), output

test "export members imported from another module", ->
  input = "export { foo, bar } from 'lib'"
  output = """
    export {
      foo,
      bar
    } from 'lib';"""
  eq toJS(input), output

test "export as aliases members imported from another module", ->
  input = "export { foo as bar, baz as qux } from 'lib'"
  output = """
    export {
      foo as bar,
      baz as qux
    } from 'lib';"""
  eq toJS(input), output

test "export list can contain CoffeeScript keywords", ->
  input = "export { unless } from 'lib'"
  output = """
    export {
      unless
    } from 'lib';"""
  eq toJS(input), output

test "export list can contain CoffeeScript keywords when aliasing", ->
  input = "export { when as bar, baz as unless } from 'lib'"
  output = """
    export {
      when as bar,
      baz as unless
    } from 'lib';"""
  eq toJS(input), output


# Edge cases

test "multiline import with comments", ->
  input = """
    import {
      foo, # Not as good as bar
      bar as baz # I prefer qux
    } from 'lib'"""
  output = """
    import {
      foo,
      bar as baz
    } from 'lib';"""
  eq toJS(input), output

test "`from` not part of an import or export statement can still be assigned", ->
  from = 5
  eq 5, from

test "a variable named `from` can be assigned after an import", ->
  input = """
    import { foo } from 'lib'
    from = 5"""
  output = """
    var from;

    import {
      foo
    } from 'lib';

    from = 5;"""
  eq toJS(input), output

test "`from` can be assigned after a multiline import", ->
  input = """
    import {
      foo
    } from 'lib'
    from = 5"""
  output = """
    var from;

    import {
      foo
    } from 'lib';

    from = 5;"""
  eq toJS(input), output

test "`from` can be imported as a member name", ->
  input = "import { from } from 'lib'"
  output = """
    import {
      from
    } from 'lib';"""
  eq toJS(input), output

test "`from` can be imported as a member name and aliased", ->
  input = "import { from as foo } from 'lib'"
  output = """
    import {
      from as foo
    } from 'lib';"""
  eq toJS(input), output

test "`from` can be used as an alias name", ->
  input = "import { foo as from } from 'lib'"
  output = """
    import {
      foo as from
    } from 'lib';"""
  eq toJS(input), output

test "`as` can be imported as a member name", ->
  input = "import { as } from 'lib'"
  output = """
    import {
      as
    } from 'lib';"""
  eq toJS(input), output

test "`as` can be imported as a member name and aliased", ->
  input = "import { as as foo } from 'lib'"
  output = """
    import {
      as as foo
    } from 'lib';"""
  eq toJS(input), output

test "`as` can be used as an alias name", ->
  input = "import { foo as as } from 'lib'"
  output = """
    import {
      foo as as
    } from 'lib';"""
  eq toJS(input), output

test "CoffeeScript keywords can be used as imported names in import lists", ->
  input = """
    import { unless as bar } from 'lib'
    bar.barMethod()"""
  output = """
    import {
      unless as bar
    } from 'lib';

    bar.barMethod();"""
  eq toJS(input), output

test "`*` can be used in an expression on the same line as an export keyword", ->
  input = "export foo = (x) -> x * x"
  output = """
    export var foo = function(x) {
      return x * x;
    };"""
  eq toJS(input), output
  input = "export default foo = (x) -> x * x"
  output = """
    var foo;

    export default foo = function(x) {
      return x * x;
    };"""
  eq toJS(input), output

test "`*` and `from` can be used in an export default expression", ->
  input = """
    export default foo.extend
      bar: ->
        from = 5
        from = from * 3"""
  output = """
    export default foo.extend({
      bar: function() {
        var from;
        from = 5;
        return from = from * 3;
      }
    });"""
  eq toJS(input), output

test "wrapped members can be imported multiple times if aliased", ->
  input = "import { foo, foo as bar } from 'lib'"
  output = """
    import {
      foo,
      foo as bar
    } from 'lib';"""
  eq toJS(input), output

test "default and wrapped members can be imported multiple times if aliased", ->
  input = "import foo, { foo as bar } from 'lib'"
  output = """
    import foo, {
      foo as bar
    } from 'lib';"""
  eq toJS(input), output

test "import a member named default", ->
  input = "import { default } from 'lib'"
  output = """
    import {
      default
    } from 'lib';"""
  eq toJS(input), output

test "import an aliased member named default", ->
  input = "import { default as def } from 'lib'"
  output = """
    import {
      default as def
    } from 'lib';"""
  eq toJS(input), output

test "export a member named default", ->
  input = "export { default }"
  output = """
    export {
      default
    };"""
  eq toJS(input), output

test "export an aliased member named default", ->
  input = "export { def as default }"
  output = """
    export {
      def as default
    };"""
  eq toJS(input), output

test "import an imported member named default", ->
  input = "import { default } from 'lib'"
  output = """
    import {
      default
    } from 'lib';"""
  eq toJS(input), output

test "import an imported aliased member named default", ->
  input = "import { default as def } from 'lib'"
  output = """
    import {
      default as def
    } from 'lib';"""
  eq toJS(input), output

test "export an imported member named default", ->
  input = "export { default } from 'lib'"
  output = """
    export {
      default
    } from 'lib';"""
  eq toJS(input), output

test "export an imported aliased member named default", ->
  input = "export { default as def } from 'lib'"
  output = """
    export {
      default as def
    } from 'lib';"""
  eq toJS(input), output

test "#4394: export shouldn't prevent variable declarations", ->
  input = """
    x = 1
    export { x }
  """
  output = """
    var x;

    x = 1;

    export {
      x
    };
  """
  eq toJS(input), output

test "#4451: `default` in an export statement is only treated as a keyword when it follows `export` or `as`", ->
  input = "export default { default: 1 }"
  output = """
    export default {
      "default": 1
    };
  """
  eq toJS(input), output

test "#4491: import- and export-specific lexing should stop after import/export statement", ->
  input = """
    import {
      foo,
      bar as baz
    } from 'lib'

    foo as
    3 * as 4
    from 'foo'
    """
  output = """
    import {
      foo,
      bar as baz
    } from 'lib';

    foo(as);

    3 * as(4);

    from('foo');
    """
  eq toJS(input), output

  input = """
    import { foo, bar as baz } from 'lib'

    foo as
    3 * as 4
    from 'foo'
    """
  output = """
    import {
      foo,
      bar as baz
    } from 'lib';

    foo(as);

    3 * as(4);

    from('foo');
    """
  eq toJS(input), output

  input = """
    import * as lib from 'lib'

    foo as
    3 * as 4
    from 'foo'
    """
  output = """
    import * as lib from 'lib';

    foo(as);

    3 * as(4);

    from('foo');
    """
  eq toJS(input), output

  input = """
    export {
      foo,
      bar
    }

    foo as
    3 * as 4
    from 'foo'
    """
  output = """
    export {
      foo,
      bar
    };

    foo(as);

    3 * as(4);

    from('foo');
    """
  eq toJS(input), output

  input = """
    export * from 'lib'

    foo as
    3 * as 4
    from 'foo'
    """
  output = """
    export * from 'lib';

    foo(as);

    3 * as(4);

    from('foo');
    """
  eq toJS(input), output