Skip to content

Commit a8b2b99

Browse files
committed
Replace another dev-dependency
1 parent a641347 commit a8b2b99

File tree

2 files changed

+32
-122
lines changed

2 files changed

+32
-122
lines changed

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,20 @@
6868
"esast-util-from-js": "^1.0.0",
6969
"estree-to-babel": "^3.0.0",
7070
"estree-util-build-jsx": "^2.0.0",
71-
"hast-util-from-parse5": "^7.0.0",
71+
"estree-util-to-js": "^1.0.0",
7272
"hastscript": "^7.0.0",
7373
"mdast-util-from-markdown": "^1.0.0",
7474
"mdast-util-mdx": "^2.0.0",
7575
"mdast-util-to-hast": "^12.0.0",
7676
"micromark-extension-mdxjs": "^1.0.0",
77-
"parse5": "^7.0.0",
7877
"prettier": "^2.0.0",
79-
"recast": "^0.21.0",
8078
"remark-cli": "^10.0.0",
8179
"remark-preset-wooorm": "^9.0.0",
8280
"rimraf": "^3.0.0",
8381
"tape": "^5.0.0",
8482
"type-coverage": "^2.0.0",
8583
"typescript": "^4.0.0",
8684
"unist-util-visit": "^4.0.0",
87-
"vfile": "^5.0.0",
8885
"xo": "^0.50.0"
8986
},
9087
"scripts": {

test.js

Lines changed: 31 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,17 @@ import test from 'tape'
1111
import babel from '@babel/core'
1212
import fauxEsmGenerate from '@babel/generator'
1313
import {fromJs} from 'esast-util-from-js'
14+
import {toJs, jsx} from 'estree-util-to-js'
15+
import {attachComments} from 'estree-util-attach-comments'
1416
import acornJsx from 'acorn-jsx'
1517
// @ts-expect-error: untyped.
1618
import toBabel from 'estree-to-babel'
1719
import {walk} from 'estree-walker'
18-
import {VFile} from 'vfile'
1920
import {h, s} from 'hastscript'
20-
import {fromParse5} from 'hast-util-from-parse5'
2121
import {fromMarkdown} from 'mdast-util-from-markdown'
2222
import {toHast} from 'mdast-util-to-hast'
2323
import {mdxFromMarkdown} from 'mdast-util-mdx'
2424
import {mdxjs} from 'micromark-extension-mdxjs'
25-
import * as parse5 from 'parse5'
26-
import recast from 'recast'
2725
import {visit} from 'unist-util-visit'
2826
import {toEstree} from './index.js'
2927

@@ -596,84 +594,6 @@ test('hast-util-to-estree', (t) => {
596594
t.end()
597595
})
598596

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-
677597
test('integration (babel)', (t) => {
678598
t.deepEqual(
679599
generate(toBabel(toEstree(h('x')))).code,
@@ -748,45 +668,43 @@ test('integration (babel)', (t) => {
748668
test('integration (micromark-extension-mdxjs, mdast-util-mdx)', (t) => {
749669
t.deepEqual(
750670
transform('## Hello, {props}!'),
751-
'<><h2>{"Hello, "}{props}{"!"}</h2></>;',
671+
'<><h2>{"Hello, "}{props}{"!"}</h2></>;\n',
752672
'should transform an MDX.js expression (text)'
753673
)
754674

755675
t.deepEqual(
756676
transform('{1 + 1}'),
757-
'<>{1 + 1}</>;',
677+
'<>{1 + 1}</>;\n',
758678
'should transform an MDX.js expression (flow)'
759679
)
760680

761-
// Note: `recast` can’t serialize the sole comment.
762-
// It’s there in the AST though.
763681
t.deepEqual(
764682
transform('## Hello, {/* x */}!'),
765-
'<><h2>{"Hello, "}{}{"!"}</h2></>;',
683+
'<><h2>{"Hello, "}{}{"!"}</h2></>;\n',
766684
'should transform an empty MDX.js expression'
767685
)
768686

769687
t.deepEqual(
770688
transform('{a + /* 1 */ 2}'),
771-
'<>{a + /* 1 */\n 2}</>;',
689+
'<>{a + 2}</>;\n',
772690
'should transform comments in an MDX expression'
773691
)
774692

775693
t.deepEqual(
776694
transform('## Hello, <x />'),
777-
'<><h2>{"Hello, "}<x /></h2></>;',
695+
'<><h2>{"Hello, "}<x /></h2></>;\n',
778696
'should transform a void MDX.js JSX element (text)'
779697
)
780698

781699
t.deepEqual(
782700
transform('## Hello, <x y z="a" />'),
783-
'<><h2>{"Hello, "}<x y z="a" /></h2></>;',
701+
'<><h2>{"Hello, "}<x y z="a" /></h2></>;\n',
784702
'should transform boolean and literal attributes on JSX elements'
785703
)
786704

787705
t.deepEqual(
788706
transform('## Hello, <x y={1 + 1} />'),
789-
'<><h2>{"Hello, "}<x y={1 + 1} /></h2></>;',
707+
'<><h2>{"Hello, "}<x y={1 + 1} /></h2></>;\n',
790708
'should transform attribute value expressions on JSX elements'
791709
)
792710

@@ -800,89 +718,89 @@ test('integration (micromark-extension-mdxjs, mdast-util-mdx)', (t) => {
800718

801719
t.deepEqual(
802720
transform('<a b={1 + /* 1 */ 2} />'),
803-
'<><a\n b={1 + /* 1 */\n 2} /></>;',
721+
'<><a b={1 + 2} /></>;\n',
804722
'should transform comments in an MDX attribute value expression'
805723
)
806724

807725
t.deepEqual(
808726
transform('<x style={{color: "red"}} />'),
809-
'<><x\n style={{\n color: "red"\n }} /></>;',
727+
'<><x style={{\n color: "red"\n}} /></>;\n',
810728
'should transform object attribute value expressions'
811729
)
812730

813731
t.deepEqual(
814732
transform('## Hello, <x a={b} />', true),
815-
'<><h2>{"Hello, "}<x a={} /></h2></>;',
733+
'<><h2>{"Hello, "}<x a={} /></h2></>;\n',
816734
'should transform attribute value expressions w/o estrees'
817735
)
818736

819737
t.deepEqual(
820738
transform('## Hello, <x {...props} />'),
821-
'<><h2>{"Hello, "}<x {...props} /></h2></>;',
739+
'<><h2>{"Hello, "}<x {...props} /></h2></>;\n',
822740
'should transform attribute expressions on JSX elements'
823741
)
824742

825743
t.deepEqual(
826744
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',
828746
'should transform comments in an MDX attribute expressions'
829747
)
830748

831749
t.deepEqual(
832750
transform('## Hello, <x {...props} />', true),
833-
'<><h2>{"Hello, "}<x {...{}} /></h2></>;',
751+
'<><h2>{"Hello, "}<x {...{}} /></h2></>;\n',
834752
'should transform attribute expressions w/o estrees'
835753
)
836754

837755
t.deepEqual(
838756
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',
840758
'should support member names'
841759
)
842760

843761
t.deepEqual(
844762
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',
846764
'should support namespace names'
847765
)
848766

849767
t.deepEqual(
850768
transform('<x xml:lang="en" />'),
851-
'<><x xml:lang="en" /></>;',
769+
'<><x xml:lang="en" /></>;\n',
852770
'should support namespace attribute names'
853771
)
854772

855773
t.deepEqual(
856774
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',
858776
'should transform children in MDX.js elements'
859777
)
860778

861779
t.deepEqual(
862780
transform('## Hello, <>{props}</>!'),
863-
'<><h2>{"Hello, "}<>{props}</>{"!"}</h2></>;',
781+
'<><h2>{"Hello, "}<>{props}</>{"!"}</h2></>;\n',
864782
'should transform MDX.js JSX fragments'
865783
)
866784

867785
t.deepEqual(
868786
transform(
869787
'import x from "y"\nexport const name = "World"\n\n## Hello, {name}!'
870788
),
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',
872790
'should transform MDX.js ESM'
873791
)
874792

875793
t.deepEqual(
876794
transform(
877795
'import /* 1 */ name /* 2 */ from /* 3 */ "a" /* 4 */\n\n\n## Hello, {name}!'
878796
),
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',
880798
'should transform comments in MDX.js ESM'
881799
)
882800

883801
t.deepEqual(
884802
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',
886804
'should transform *just* MDX.js ESM'
887805
)
888806

@@ -891,17 +809,17 @@ test('integration (micromark-extension-mdxjs, mdast-util-mdx)', (t) => {
891809
'import x from "y"\nexport const name = "World"\n\n## Hello, {name}!',
892810
true
893811
),
894-
'<><h2>{"Hello, "}{}{"!"}</h2></>;',
812+
'<><h2>{"Hello, "}{}{"!"}</h2></>;\n',
895813
'should transform ESM w/o estrees'
896814
)
897815

898816
t.deepEqual(
899817
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',
901819
'should support svg'
902820
)
903821

904-
t.deepEqual(transform(''), '<></>;', 'should support an empty document')
822+
t.deepEqual(transform(''), '<></>;\n', 'should support an empty document')
905823

906824
t.end()
907825

@@ -921,7 +839,11 @@ test('integration (micromark-extension-mdxjs, mdast-util-mdx)', (t) => {
921839
if (clean && hast) visit(hast, passThrough, acornClean)
922840

923841
// @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
925847

926848
/**
927849
* @param {HastNode} node
@@ -1201,12 +1123,3 @@ function acornParse(doc) {
12011123
)
12021124
return program
12031125
}
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

Comments
 (0)