Skip to content

Commit fa935dd

Browse files
committed
Add esm to mdast node type registry
When using this utility, the esm type will automatically be added to mdast in the correct places. See DefinitelyTyped/DefinitelyTyped#54421 for more information.
1 parent 4ffc1be commit fa935dd

File tree

5 files changed

+22
-9
lines changed

5 files changed

+22
-9
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
coverage/
22
node_modules/
33
*.log
4-
*.d.ts
4+
index.d.ts
5+
test.d.ts
56
.DS_Store
67
yarn.lock

complex-types.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {Literal} from 'mdast'
2+
import {Program} from 'estree-jsx'
3+
4+
export interface MDXJSEsm extends Literal {
5+
type: 'mdxjsEsm'
6+
data?: {estree?: Program} & Literal['data']
7+
}
8+
9+
declare module 'mdast' {
10+
interface BlockContentMap {
11+
mdxjsEsm: MDXJSEsm
12+
}
13+
}

index.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
/**
2-
* @typedef {import('mdast').Literal} Literal
32
* @typedef {import('mdast-util-from-markdown').Extension} FromMarkdownExtension
43
* @typedef {import('mdast-util-from-markdown').Handle} FromMarkdownHandle
54
* @typedef {import('mdast-util-to-markdown').Options} ToMarkdownExtension
65
* @typedef {import('mdast-util-to-markdown').Handle} ToMarkdownHandle
7-
* @typedef {import('estree-jsx').Program} Estree
8-
*
9-
* @typedef {Literal & {type: 'mdxjsEsm', data: {estree?: Estree}}} MDXJSEsm
6+
* @typedef {import('estree-jsx').Program} Program
7+
* @typedef {import('./complex-types').MDXJSEsm} MDXJSEsm
108
*/
119

1210
/** @type {FromMarkdownExtension} */
@@ -20,16 +18,15 @@ export const mdxjsEsmToMarkdown = {handlers: {mdxjsEsm: handleMdxjsEsm}}
2018

2119
/** @type {FromMarkdownHandle} */
2220
function enterMdxjsEsm(token) {
23-
// @ts-expect-error: custom.
2421
this.enter({type: 'mdxjsEsm', value: ''}, token)
2522
this.buffer() // Capture EOLs
2623
}
2724

2825
/** @type {FromMarkdownHandle} */
2926
function exitMdxjsEsm(token) {
3027
const value = this.resume()
31-
const node = this.exit(token)
32-
/** @type {Estree?} */
28+
const node = /** @type {MDXJSEsm} */ (this.exit(token))
29+
/** @type {Program|undefined} */
3330
// @ts-expect-error: custom.
3431
const estree = token.estree
3532

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"main": "index.js",
3434
"types": "index.d.ts",
3535
"files": [
36+
"complex-types.d.ts",
3637
"index.d.ts",
3738
"index.js"
3839
],
@@ -58,7 +59,7 @@
5859
"xo": "^0.39.0"
5960
},
6061
"scripts": {
61-
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
62+
"build": "rimraf \"{index,test}.d.ts\" && tsc && type-coverage",
6263
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
6364
"test-api": "node --conditions development test.js",
6465
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test.js",

test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ test('mdast -> markdown', (t) => {
215215
{
216216
type: 'root',
217217
children: [
218+
// @ts-expect-error: `value` missing.
218219
{type: 'mdxjsEsm'},
219220
{type: 'paragraph', children: [{type: 'text', value: 'd'}]}
220221
]

0 commit comments

Comments
 (0)