@@ -11,19 +11,17 @@ import test from 'tape'
11
11
import babel from '@babel/core'
12
12
import fauxEsmGenerate from '@babel/generator'
13
13
import { fromJs } from 'esast-util-from-js'
14
+ import { toJs , jsx } from 'estree-util-to-js'
15
+ import { attachComments } from 'estree-util-attach-comments'
14
16
import acornJsx from 'acorn-jsx'
15
17
// @ts -expect-error: untyped.
16
18
import toBabel from 'estree-to-babel'
17
19
import { walk } from 'estree-walker'
18
- import { VFile } from 'vfile'
19
20
import { h , s } from 'hastscript'
20
- import { fromParse5 } from 'hast-util-from-parse5'
21
21
import { fromMarkdown } from 'mdast-util-from-markdown'
22
22
import { toHast } from 'mdast-util-to-hast'
23
23
import { mdxFromMarkdown } from 'mdast-util-mdx'
24
24
import { mdxjs } from 'micromark-extension-mdxjs'
25
- import * as parse5 from 'parse5'
26
- import recast from 'recast'
27
25
import { visit } from 'unist-util-visit'
28
26
import { toEstree } from './index.js'
29
27
@@ -596,84 +594,6 @@ test('hast-util-to-estree', (t) => {
596
594
t . end ( )
597
595
} )
598
596
599
- test ( 'integration (recast)' , ( t ) => {
600
- t . deepEqual (
601
- recastSerialize ( toEstree ( h ( 'x' ) ) ) ,
602
- '<x />;' ,
603
- 'should format an element (void)'
604
- )
605
-
606
- t . deepEqual (
607
- recastSerialize ( toEstree ( h ( 'x' , 'y' ) ) ) ,
608
- '<x>{"y"}</x>;' ,
609
- 'should format an element w/ text child'
610
- )
611
-
612
- t . deepEqual (
613
- recastSerialize ( toEstree ( h ( 'x' , h ( 'y' , 'z' ) ) ) ) ,
614
- '<x><y>{"z"}</y></x>;' ,
615
- 'should format an element w/ element child'
616
- )
617
-
618
- t . deepEqual (
619
- recastSerialize ( toEstree ( h ( 'x' , { y : true , x : 'a' } ) ) ) ,
620
- '<x y x="a" />;' ,
621
- 'should format an element w/ props'
622
- )
623
-
624
- t . deepEqual (
625
- recastSerialize ( toEstree ( h ( 'x' , { style : 'a:b' } ) ) ) ,
626
- '<x\n style={{\n a: "b"\n }} />;' ,
627
- 'should format an element w/ style props'
628
- )
629
-
630
- t . deepEqual (
631
- recastSerialize ( toEstree ( h ( 'x' , [ { type : 'comment' , value : 'y' } ] ) ) ) ,
632
- '<x>{/*y*/}</x>;' ,
633
- 'should format a comment'
634
- )
635
-
636
- t . deepEqual (
637
- recastSerialize ( toEstree ( h ( 'x' , [ { type : 'comment' , value : 'y' } ] ) ) ) ,
638
- '<x>{/*y*/}</x>;' ,
639
- 'should format a comment'
640
- )
641
-
642
- t . deepEqual (
643
- recastSerialize ( toEstree ( { type : 'comment' , value : 'y' } ) ) ,
644
- '<>{/*y*/}</>;' ,
645
- 'should format just a comment'
646
- )
647
-
648
- const doc = '<!--a--><x><!--b--></x><!--c-->'
649
- t . deepEqual (
650
- recastSerialize (
651
- toEstree (
652
- fromParse5 (
653
- parse5 . parseFragment ( doc , { sourceCodeLocationInfo : true } ) ,
654
- new VFile ( doc )
655
- )
656
- )
657
- ) ,
658
- '<>{/*a*/}<x>{/*b*/}</x>{/*c*/}</>;' ,
659
- 'should format comments w/ positional info'
660
- )
661
-
662
- t . deepEqual (
663
- recastSerialize ( toEstree ( { type : 'root' , children : [ ] } ) ) ,
664
- '<></>;' ,
665
- 'should format a root'
666
- )
667
-
668
- t . deepEqual (
669
- recastSerialize ( toEstree ( s ( 'svg' , { viewBox : '0 0 1 1' } ) ) ) ,
670
- '<svg viewBox="0 0 1 1" />;' ,
671
- 'should format svg'
672
- )
673
-
674
- t . end ( )
675
- } )
676
-
677
597
test ( 'integration (babel)' , ( t ) => {
678
598
t . deepEqual (
679
599
generate ( toBabel ( toEstree ( h ( 'x' ) ) ) ) . code ,
@@ -748,45 +668,43 @@ test('integration (babel)', (t) => {
748
668
test ( 'integration (micromark-extension-mdxjs, mdast-util-mdx)' , ( t ) => {
749
669
t . deepEqual (
750
670
transform ( '## Hello, {props}!' ) ,
751
- '<><h2>{"Hello, "}{props}{"!"}</h2></>;' ,
671
+ '<><h2>{"Hello, "}{props}{"!"}</h2></>;\n ' ,
752
672
'should transform an MDX.js expression (text)'
753
673
)
754
674
755
675
t . deepEqual (
756
676
transform ( '{1 + 1}' ) ,
757
- '<>{1 + 1}</>;' ,
677
+ '<>{1 + 1}</>;\n ' ,
758
678
'should transform an MDX.js expression (flow)'
759
679
)
760
680
761
- // Note: `recast` can’t serialize the sole comment.
762
- // It’s there in the AST though.
763
681
t . deepEqual (
764
682
transform ( '## Hello, {/* x */}!' ) ,
765
- '<><h2>{"Hello, "}{}{"!"}</h2></>;' ,
683
+ '<><h2>{"Hello, "}{}{"!"}</h2></>;\n ' ,
766
684
'should transform an empty MDX.js expression'
767
685
)
768
686
769
687
t . deepEqual (
770
688
transform ( '{a + /* 1 */ 2}' ) ,
771
- '<>{a + /* 1 */\n 2}</>;' ,
689
+ '<>{a + 2}</>;\n ' ,
772
690
'should transform comments in an MDX expression'
773
691
)
774
692
775
693
t . deepEqual (
776
694
transform ( '## Hello, <x />' ) ,
777
- '<><h2>{"Hello, "}<x /></h2></>;' ,
695
+ '<><h2>{"Hello, "}<x /></h2></>;\n ' ,
778
696
'should transform a void MDX.js JSX element (text)'
779
697
)
780
698
781
699
t . deepEqual (
782
700
transform ( '## Hello, <x y z="a" />' ) ,
783
- '<><h2>{"Hello, "}<x y z="a" /></h2></>;' ,
701
+ '<><h2>{"Hello, "}<x y z="a" /></h2></>;\n ' ,
784
702
'should transform boolean and literal attributes on JSX elements'
785
703
)
786
704
787
705
t . deepEqual (
788
706
transform ( '## Hello, <x y={1 + 1} />' ) ,
789
- '<><h2>{"Hello, "}<x y={1 + 1} /></h2></>;' ,
707
+ '<><h2>{"Hello, "}<x y={1 + 1} /></h2></>;\n ' ,
790
708
'should transform attribute value expressions on JSX elements'
791
709
)
792
710
@@ -800,89 +718,89 @@ test('integration (micromark-extension-mdxjs, mdast-util-mdx)', (t) => {
800
718
801
719
t . deepEqual (
802
720
transform ( '<a b={1 + /* 1 */ 2} />' ) ,
803
- '<><a\n b={1 + /* 1 */\n 2} /></>;' ,
721
+ '<><a b={1 + 2} /></>;\n ' ,
804
722
'should transform comments in an MDX attribute value expression'
805
723
)
806
724
807
725
t . deepEqual (
808
726
transform ( '<x style={{color: "red"}} />' ) ,
809
- '<><x\n style={{\n color: "red"\n }} /></>;' ,
727
+ '<><x style={{\n color: "red"\n}} /></>;\n ' ,
810
728
'should transform object attribute value expressions'
811
729
)
812
730
813
731
t . deepEqual (
814
732
transform ( '## Hello, <x a={b} />' , true ) ,
815
- '<><h2>{"Hello, "}<x a={} /></h2></>;' ,
733
+ '<><h2>{"Hello, "}<x a={} /></h2></>;\n ' ,
816
734
'should transform attribute value expressions w/o estrees'
817
735
)
818
736
819
737
t . deepEqual (
820
738
transform ( '## Hello, <x {...props} />' ) ,
821
- '<><h2>{"Hello, "}<x {...props} /></h2></>;' ,
739
+ '<><h2>{"Hello, "}<x {...props} /></h2></>;\n ' ,
822
740
'should transform attribute expressions on JSX elements'
823
741
)
824
742
825
743
t . deepEqual (
826
744
transform ( '<a {...{c: /* 1 */ 1, d: 2 /* 2 */}} />' ) ,
827
- '<><a\n {...{\n c: /* 1 */\n 1,\n\n d: 2\n }/* 2 */ } /></>;' ,
745
+ '<><a {...{\n /*2 */\n c: 1,\n d: 2\n} } /></>;\n ' ,
828
746
'should transform comments in an MDX attribute expressions'
829
747
)
830
748
831
749
t . deepEqual (
832
750
transform ( '## Hello, <x {...props} />' , true ) ,
833
- '<><h2>{"Hello, "}<x {...{}} /></h2></>;' ,
751
+ '<><h2>{"Hello, "}<x {...{}} /></h2></>;\n ' ,
834
752
'should transform attribute expressions w/o estrees'
835
753
)
836
754
837
755
t . deepEqual (
838
756
transform ( '<a.b.c d>e</a.b.c>' ) ,
839
- '<><p><a.b.c d>{"e"}</a.b.c></p></>;' ,
757
+ '<><p><a.b.c d>{"e"}</a.b.c></p></>;\n ' ,
840
758
'should support member names'
841
759
)
842
760
843
761
t . deepEqual (
844
762
transform ( '<a:b d>e</a:b>' ) ,
845
- '<><p><a:b d>{"e"}</a:b></p></>;' ,
763
+ '<><p><a:b d>{"e"}</a:b></p></>;\n ' ,
846
764
'should support namespace names'
847
765
)
848
766
849
767
t . deepEqual (
850
768
transform ( '<x xml:lang="en" />' ) ,
851
- '<><x xml:lang="en" /></>;' ,
769
+ '<><x xml:lang="en" /></>;\n ' ,
852
770
'should support namespace attribute names'
853
771
)
854
772
855
773
t . deepEqual (
856
774
transform ( '<x>\n - y\n</x>' ) ,
857
- '<><x><ul>{"\\n"}<li>{"y"}</li>{"\\n"}</ul></x></>;' ,
775
+ '<><x><ul>{"\\n"}<li>{"y"}</li>{"\\n"}</ul></x></>;\n ' ,
858
776
'should transform children in MDX.js elements'
859
777
)
860
778
861
779
t . deepEqual (
862
780
transform ( '## Hello, <>{props}</>!' ) ,
863
- '<><h2>{"Hello, "}<>{props}</>{"!"}</h2></>;' ,
781
+ '<><h2>{"Hello, "}<>{props}</>{"!"}</h2></>;\n ' ,
864
782
'should transform MDX.js JSX fragments'
865
783
)
866
784
867
785
t . deepEqual (
868
786
transform (
869
787
'import x from "y"\nexport const name = "World"\n\n## Hello, {name}!'
870
788
) ,
871
- 'import x from "y";\nexport const name = "World";\n<><h2>{"Hello, "}{name}{"!"}</h2></>;' ,
789
+ 'import x from "y";\nexport const name = "World";\n<><h2>{"Hello, "}{name}{"!"}</h2></>;\n ' ,
872
790
'should transform MDX.js ESM'
873
791
)
874
792
875
793
t . deepEqual (
876
794
transform (
877
795
'import /* 1 */ name /* 2 */ from /* 3 */ "a" /* 4 */\n\n\n## Hello, {name}!'
878
796
) ,
879
- 'import /* 1 */\nname from /* 2 */\n/* 3 */\n "a";\n\n <><h2>{"Hello, "}{name}{"!"}</h2></>;' ,
797
+ 'import name from "a";\n<><h2>{"Hello, "}{name}{"!"}</h2></>;\n ' ,
880
798
'should transform comments in MDX.js ESM'
881
799
)
882
800
883
801
t . deepEqual (
884
802
transform ( 'import x from "y"\nexport const name = "World"' ) ,
885
- 'import x from "y";\nexport const name = "World";\n<></>;' ,
803
+ 'import x from "y";\nexport const name = "World";\n<></>;\n ' ,
886
804
'should transform *just* MDX.js ESM'
887
805
)
888
806
@@ -891,17 +809,17 @@ test('integration (micromark-extension-mdxjs, mdast-util-mdx)', (t) => {
891
809
'import x from "y"\nexport const name = "World"\n\n## Hello, {name}!' ,
892
810
true
893
811
) ,
894
- '<><h2>{"Hello, "}{}{"!"}</h2></>;' ,
812
+ '<><h2>{"Hello, "}{}{"!"}</h2></>;\n ' ,
895
813
'should transform ESM w/o estrees'
896
814
)
897
815
898
816
t . deepEqual (
899
817
transform ( '<svg viewBox="0 0 1 1"><rect /></svg>' ) ,
900
- '<><svg viewBox="0 0 1 1"><rect /></svg></>;' ,
818
+ '<><svg viewBox="0 0 1 1"><rect /></svg></>;\n ' ,
901
819
'should support svg'
902
820
)
903
821
904
- t . deepEqual ( transform ( '' ) , '<></>;' , 'should support an empty document' )
822
+ t . deepEqual ( transform ( '' ) , '<></>;\n ' , 'should support an empty document' )
905
823
906
824
t . end ( )
907
825
@@ -921,7 +839,11 @@ test('integration (micromark-extension-mdxjs, mdast-util-mdx)', (t) => {
921
839
if ( clean && hast ) visit ( hast , passThrough , acornClean )
922
840
923
841
// @ts -expect-error: it’s a node.
924
- return recastSerialize ( toEstree ( hast ) )
842
+ const program = toEstree ( hast )
843
+ attachComments ( program , program . comments )
844
+ delete program . comments
845
+
846
+ return toJs ( program , { handlers : jsx } ) . value
925
847
926
848
/**
927
849
* @param {HastNode } node
@@ -1201,12 +1123,3 @@ function acornParse(doc) {
1201
1123
)
1202
1124
return program
1203
1125
}
1204
-
1205
- /**
1206
- * @param {Program } tree
1207
- */
1208
- function recastSerialize ( tree ) {
1209
- /** @type {Array<Comment> } */
1210
- tree . comments = undefined
1211
- return recast . prettyPrint ( tree ) . code
1212
- }
0 commit comments