@@ -397,7 +397,41 @@ <h1>coffeescript.coffee</h1>
397
397
options.bare = < span class ="hljs-literal "> yes</ span >
398
398
< span class ="hljs-keyword "> break</ span >
399
399
400
- fragments = parser.parse(tokens).compileToFragments options
400
+ nodes = parser.parse tokens</ pre > </ div > </ div >
401
+
402
+ </ li >
403
+
404
+
405
+ < li id ="section-16 ">
406
+ < div class ="annotation ">
407
+
408
+ < div class ="pilwrap ">
409
+ < a class ="pilcrow " href ="#section-16 "> ¶</ a >
410
+ </ div >
411
+ < p > If all that was requested was a POJO representation of the nodes, e.g.
412
+ the abstract syntax tree (AST), we can stop now and just return that
413
+ (after fixing the location data for the root/< code > File</ code > »< code > Program</ code > node,
414
+ which might’ve gotten misaligned from the original source due to the
415
+ < code > clean</ code > function in the lexer).</ p >
416
+
417
+ </ div >
418
+
419
+ < div class ="content "> < div class ='highlight '> < pre > < span class ="hljs-keyword "> if</ span > options.ast
420
+ nodes.allCommentTokens = helpers.extractAllCommentTokens tokens
421
+ sourceCodeNumberOfLines = (code.match(< span class ="hljs-regexp "> /\r?\n/g</ span > ) < span class ="hljs-keyword "> or</ span > < span class ="hljs-string "> ''</ span > ).length + < span class ="hljs-number "> 1</ span >
422
+ sourceCodeLastLine = < span class ="hljs-regexp "> /.*$/</ span > .exec(code)[< span class ="hljs-number "> 0</ span > ] < span class ="hljs-comment "> # `.*` matches all but line break characters.</ span >
423
+ ast = nodes.ast options
424
+ range = [< span class ="hljs-number "> 0</ span > , code.length]
425
+ ast.start = ast.program.start = range[< span class ="hljs-number "> 0</ span > ]
426
+ ast.end = ast.program.end = range[< span class ="hljs-number "> 1</ span > ]
427
+ ast.range = ast.program.range = range
428
+ ast.loc.start = ast.program.loc.start = {line: < span class ="hljs-number "> 1</ span > , column: < span class ="hljs-number "> 0</ span > }
429
+ ast.loc.end.line = ast.program.loc.end.line = sourceCodeNumberOfLines
430
+ ast.loc.end.column = ast.program.loc.end.column = sourceCodeLastLine.length
431
+ ast.tokens = tokens
432
+ < span class ="hljs-keyword "> return</ span > ast
433
+
434
+ fragments = nodes.compileToFragments options
401
435
402
436
currentLine = < span class ="hljs-number "> 0</ span >
403
437
currentLine += < span class ="hljs-number "> 1</ span > < span class ="hljs-keyword "> if</ span > options.header
@@ -409,11 +443,11 @@ <h1>coffeescript.coffee</h1>
409
443
</ li >
410
444
411
445
412
- < li id ="section-16 ">
446
+ < li id ="section-17 ">
413
447
< div class ="annotation ">
414
448
415
449
< div class ="pilwrap ">
416
- < a class ="pilcrow " href ="#section-16 "> ¶</ a >
450
+ < a class ="pilcrow " href ="#section-17 "> ¶</ a >
417
451
</ div >
418
452
< p > Update the sourcemap with data from each fragment.</ p >
419
453
@@ -424,11 +458,11 @@ <h1>coffeescript.coffee</h1>
424
458
</ li >
425
459
426
460
427
- < li id ="section-17 ">
461
+ < li id ="section-18 ">
428
462
< div class ="annotation ">
429
463
430
464
< div class ="pilwrap ">
431
- < a class ="pilcrow " href ="#section-17 "> ¶</ a >
465
+ < a class ="pilcrow " href ="#section-18 "> ¶</ a >
432
466
</ div >
433
467
< p > Do not include empty, whitespace, or semicolon-only fragments.</ p >
434
468
@@ -449,11 +483,11 @@ <h1>coffeescript.coffee</h1>
449
483
</ li >
450
484
451
485
452
- < li id ="section-18 ">
486
+ < li id ="section-19 ">
453
487
< div class ="annotation ">
454
488
455
489
< div class ="pilwrap ">
456
- < a class ="pilcrow " href ="#section-18 "> ¶</ a >
490
+ < a class ="pilcrow " href ="#section-19 "> ¶</ a >
457
491
</ div >
458
492
< p > Copy the code from each fragment into the final JavaScript.</ p >
459
493
@@ -474,11 +508,11 @@ <h1>coffeescript.coffee</h1>
474
508
</ li >
475
509
476
510
477
- < li id ="section-19 ">
511
+ < li id ="section-20 ">
478
512
< div class ="annotation ">
479
513
480
514
< div class ="pilwrap ">
481
- < a class ="pilcrow " href ="#section-19 "> ¶</ a >
515
+ < a class ="pilcrow " href ="#section-20 "> ¶</ a >
482
516
</ div >
483
517
< p > This only happens if run via the Node API and < code > transpile</ code > is set to
484
518
something other than an object.</ p >
@@ -490,11 +524,11 @@ <h1>coffeescript.coffee</h1>
490
524
</ li >
491
525
492
526
493
- < li id ="section-20 ">
527
+ < li id ="section-21 ">
494
528
< div class ="annotation ">
495
529
496
530
< div class ="pilwrap ">
497
- < a class ="pilcrow " href ="#section-20 "> ¶</ a >
531
+ < a class ="pilcrow " href ="#section-21 "> ¶</ a >
498
532
</ div >
499
533
< p > Get the reference to Babel that we have been passed if this compiler
500
534
is run via the CLI or Node API.</ p >
@@ -509,11 +543,11 @@ <h1>coffeescript.coffee</h1>
509
543
</ li >
510
544
511
545
512
- < li id ="section-21 ">
546
+ < li id ="section-22 ">
513
547
< div class ="annotation ">
514
548
515
549
< div class ="pilwrap ">
516
- < a class ="pilcrow " href ="#section-21 "> ¶</ a >
550
+ < a class ="pilcrow " href ="#section-22 "> ¶</ a >
517
551
</ div >
518
552
< p > See < a href ="https://github.com/babel/babel/issues/827#issuecomment-77573107 "> https://github.com/babel/babel/issues/827#issuecomment-77573107</ a > :
519
553
Babel can take a v3 source map object as input in < code > inputSourceMap</ code >
@@ -548,11 +582,11 @@ <h1>coffeescript.coffee</h1>
548
582
</ li >
549
583
550
584
551
- < li id ="section-22 ">
585
+ < li id ="section-23 ">
552
586
< div class ="annotation ">
553
587
554
588
< div class ="pilwrap ">
555
- < a class ="pilcrow " href ="#section-22 "> ¶</ a >
589
+ < a class ="pilcrow " href ="#section-23 "> ¶</ a >
556
590
</ div >
557
591
< p > Tokenize a string of CoffeeScript code, and return the array of tokens.</ p >
558
592
@@ -564,11 +598,11 @@ <h1>coffeescript.coffee</h1>
564
598
</ li >
565
599
566
600
567
- < li id ="section-23 ">
601
+ < li id ="section-24 ">
568
602
< div class ="annotation ">
569
603
570
604
< div class ="pilwrap ">
571
- < a class ="pilcrow " href ="#section-23 "> ¶</ a >
605
+ < a class ="pilcrow " href ="#section-24 "> ¶</ a >
572
606
</ div >
573
607
< p > Parse a string of CoffeeScript code or an array of lexed tokens, and
574
608
return the AST. You can then compile it by calling < code > .compile()</ code > on the root,
@@ -577,19 +611,17 @@ <h1>coffeescript.coffee</h1>
577
611
</ div >
578
612
579
613
< div class ="content "> < div class ='highlight '> < pre > exports.nodes = withPrettyErrors (source, options) ->
580
- < span class ="hljs-keyword "> if</ span > < span class ="hljs-keyword "> typeof</ span > source < span class ="hljs-keyword "> is</ span > < span class ="hljs-string "> 'string'</ span >
581
- parser.parse lexer.tokenize source, options
582
- < span class ="hljs-keyword "> else</ span >
583
- parser.parse source</ pre > </ div > </ div >
614
+ source = lexer.tokenize source, options < span class ="hljs-keyword "> if</ span > < span class ="hljs-keyword "> typeof</ span > source < span class ="hljs-keyword "> is</ span > < span class ="hljs-string "> 'string'</ span >
615
+ parser.parse source</ pre > </ div > </ div >
584
616
585
617
</ li >
586
618
587
619
588
- < li id ="section-24 ">
620
+ < li id ="section-25 ">
589
621
< div class ="annotation ">
590
622
591
623
< div class ="pilwrap ">
592
- < a class ="pilcrow " href ="#section-24 "> ¶</ a >
624
+ < a class ="pilcrow " href ="#section-25 "> ¶</ a >
593
625
</ div >
594
626
< p > This file used to export these methods; leave stubs that throw warnings
595
627
instead. These methods have been moved into < code > index.coffee</ code > to provide
@@ -605,11 +637,11 @@ <h1>coffeescript.coffee</h1>
605
637
</ li >
606
638
607
639
608
- < li id ="section-25 ">
640
+ < li id ="section-26 ">
609
641
< div class ="annotation ">
610
642
611
643
< div class ="pilwrap ">
612
- < a class ="pilcrow " href ="#section-25 "> ¶</ a >
644
+ < a class ="pilcrow " href ="#section-26 "> ¶</ a >
613
645
</ div >
614
646
< p > Instantiate a Lexer for our use here.</ p >
615
647
@@ -620,11 +652,11 @@ <h1>coffeescript.coffee</h1>
620
652
</ li >
621
653
622
654
623
- < li id ="section-26 ">
655
+ < li id ="section-27 ">
624
656
< div class ="annotation ">
625
657
626
658
< div class ="pilwrap ">
627
- < a class ="pilcrow " href ="#section-26 "> ¶</ a >
659
+ < a class ="pilcrow " href ="#section-27 "> ¶</ a >
628
660
</ div >
629
661
< p > The real Lexer produces a generic stream of tokens. This object provides a
630
662
thin wrapper around it, compatible with the Jison API. We can then pass it
@@ -633,6 +665,10 @@ <h1>coffeescript.coffee</h1>
633
665
</ div >
634
666
635
667
< div class ="content "> < div class ='highlight '> < pre > parser.lexer =
668
+ yylloc:
669
+ range: []
670
+ options:
671
+ ranges: < span class ="hljs-literal "> yes</ span >
636
672
lex: < span class ="hljs-function "> -></ span >
637
673
token = parser.tokens[@pos++]
638
674
< span class ="hljs-keyword "> if</ span > token
@@ -650,11 +686,11 @@ <h1>coffeescript.coffee</h1>
650
686
</ li >
651
687
652
688
653
- < li id ="section-27 ">
689
+ < li id ="section-28 ">
654
690
< div class ="annotation ">
655
691
656
692
< div class ="pilwrap ">
657
- < a class ="pilcrow " href ="#section-27 "> ¶</ a >
693
+ < a class ="pilcrow " href ="#section-28 "> ¶</ a >
658
694
</ div >
659
695
< p > Make all the AST nodes visible to the parser.</ p >
660
696
@@ -665,11 +701,11 @@ <h1>coffeescript.coffee</h1>
665
701
</ li >
666
702
667
703
668
- < li id ="section-28 ">
704
+ < li id ="section-29 ">
669
705
< div class ="annotation ">
670
706
671
707
< div class ="pilwrap ">
672
- < a class ="pilcrow " href ="#section-28 "> ¶</ a >
708
+ < a class ="pilcrow " href ="#section-29 "> ¶</ a >
673
709
</ div >
674
710
< p > Override Jison’s default error handling function.</ p >
675
711
@@ -680,11 +716,11 @@ <h1>coffeescript.coffee</h1>
680
716
</ li >
681
717
682
718
683
- < li id ="section-29 ">
719
+ < li id ="section-30 ">
684
720
< div class ="annotation ">
685
721
686
722
< div class ="pilwrap ">
687
- < a class ="pilcrow " href ="#section-29 "> ¶</ a >
723
+ < a class ="pilcrow " href ="#section-30 "> ¶</ a >
688
724
</ div >
689
725
< p > Disregard Jison’s message, it contains redundant line number information.
690
726
Disregard the token, we take its value directly from the lexer in case
@@ -708,11 +744,11 @@ <h1>coffeescript.coffee</h1>
708
744
</ li >
709
745
710
746
711
- < li id ="section-30 ">
747
+ < li id ="section-31 ">
712
748
< div class ="annotation ">
713
749
714
750
< div class ="pilwrap ">
715
- < a class ="pilcrow " href ="#section-30 "> ¶</ a >
751
+ < a class ="pilcrow " href ="#section-31 "> ¶</ a >
716
752
</ div >
717
753
< p > The second argument has a < code > loc</ code > property, which should have the location
718
754
data for this token. Unfortunately, Jison seems to send an outdated < code > loc</ code >
@@ -726,11 +762,11 @@ <h1>coffeescript.coffee</h1>
726
762
</ li >
727
763
728
764
729
- < li id ="section-31 ">
765
+ < li id ="section-32 ">
730
766
< div class ="annotation ">
731
767
732
768
< div class ="pilwrap ">
733
- < a class ="pilcrow " href ="#section-31 "> ¶</ a >
769
+ < a class ="pilcrow " href ="#section-32 "> ¶</ a >
734
770
</ div >
735
771
< p > Based on < a href ="http://v8.googlecode.com/svn/branches/bleeding_edge/src/messages.js "> http://v8.googlecode.com/svn/branches/bleeding_edge/src/messages.js</ a >
736
772
Modified to handle sourceMap</ p >
@@ -758,11 +794,11 @@ <h1>coffeescript.coffee</h1>
758
794
</ li >
759
795
760
796
761
- < li id ="section-32 ">
797
+ < li id ="section-33 ">
762
798
< div class ="annotation ">
763
799
764
800
< div class ="pilwrap ">
765
- < a class ="pilcrow " href ="#section-32 "> ¶</ a >
801
+ < a class ="pilcrow " href ="#section-33 "> ¶</ a >
766
802
</ div >
767
803
< p > Check for a sourceMap position</ p >
768
804
@@ -805,11 +841,11 @@ <h1>coffeescript.coffee</h1>
805
841
</ li >
806
842
807
843
808
- < li id ="section-33 ">
844
+ < li id ="section-34 ">
809
845
< div class ="annotation ">
810
846
811
847
< div class ="pilwrap ">
812
- < a class ="pilcrow " href ="#section-33 "> ¶</ a >
848
+ < a class ="pilcrow " href ="#section-34 "> ¶</ a >
813
849
</ div >
814
850
< p > Skip files that we didn’t compile, like Node system files that appear in
815
851
the stack trace, as they never have source maps.</ p >
@@ -824,11 +860,11 @@ <h1>coffeescript.coffee</h1>
824
860
</ li >
825
861
826
862
827
- < li id ="section-34 ">
863
+ < li id ="section-35 ">
828
864
< div class ="annotation ">
829
865
830
866
< div class ="pilwrap ">
831
- < a class ="pilcrow " href ="#section-34 "> ¶</ a >
867
+ < a class ="pilcrow " href ="#section-35 "> ¶</ a >
832
868
</ div >
833
869
< p > CoffeeScript compiled in a browser or via < code > CoffeeScript.compile</ code > or < code > .run</ code >
834
870
may get compiled with < code > options.filename</ code > that’s missing, which becomes
@@ -843,11 +879,11 @@ <h1>coffeescript.coffee</h1>
843
879
</ li >
844
880
845
881
846
- < li id ="section-35 ">
882
+ < li id ="section-36 ">
847
883
< div class ="annotation ">
848
884
849
885
< div class ="pilwrap ">
850
- < a class ="pilcrow " href ="#section-35 "> ¶</ a >
886
+ < a class ="pilcrow " href ="#section-36 "> ¶</ a >
851
887
</ div >
852
888
< p > Work backwards from the most recent anonymous source maps, until we find
853
889
one that works. This isn’t foolproof; there is a chance that multiple
@@ -864,11 +900,11 @@ <h1>coffeescript.coffee</h1>
864
900
</ li >
865
901
866
902
867
- < li id ="section-36 ">
903
+ < li id ="section-37 ">
868
904
< div class ="annotation ">
869
905
870
906
< div class ="pilwrap ">
871
- < a class ="pilcrow " href ="#section-36 "> ¶</ a >
907
+ < a class ="pilcrow " href ="#section-37 "> ¶</ a >
872
908
</ div >
873
909
< p > If all else fails, recompile this source to get a source map. We need the
874
910
previous section (for < code > <anonymous></ code > ) despite this option, because after it
@@ -891,11 +927,11 @@ <h1>coffeescript.coffee</h1>
891
927
</ li >
892
928
893
929
894
- < li id ="section-37 ">
930
+ < li id ="section-38 ">
895
931
< div class ="annotation ">
896
932
897
933
< div class ="pilwrap ">
898
- < a class ="pilcrow " href ="#section-37 "> ¶</ a >
934
+ < a class ="pilcrow " href ="#section-38 "> ¶</ a >
899
935
</ div >
900
936
< p > Based on < a href ="http://goo.gl/ZTx1p "> michaelficarra/CoffeeScriptRedux</ a >
901
937
NodeJS / V8 have no support for transforming positions in stack traces using
0 commit comments