Skip to content

Commit ba48797

Browse files
committed
Add JSDoc based types
1 parent 1060fd1 commit ba48797

File tree

6 files changed

+68
-10
lines changed

6 files changed

+68
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
coverage/
22
node_modules/
33
*.log
4+
*.d.ts
45
.DS_Store
56
yarn.lock

index.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,55 @@
1+
/**
2+
* @typedef {import('mdast').Literal} Literal
3+
* @typedef {import('mdast-util-from-markdown').Extension} FromMarkdownExtension
4+
* @typedef {import('mdast-util-from-markdown').Handle} FromMarkdownHandle
5+
* @typedef {import('mdast-util-to-markdown').Options} ToMarkdownExtension
6+
* @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
10+
*/
11+
12+
/** @type {FromMarkdownExtension} */
113
export const mdxjsEsmFromMarkdown = {
214
enter: {mdxjsEsm: enterMdxjsEsm},
315
exit: {mdxjsEsm: exitMdxjsEsm, mdxjsEsmData: exitMdxjsEsmData}
416
}
517

18+
/** @type {ToMarkdownExtension} */
619
export const mdxjsEsmToMarkdown = {handlers: {mdxjsEsm: handleMdxjsEsm}}
720

21+
/** @type {FromMarkdownHandle} */
822
function enterMdxjsEsm(token) {
23+
// @ts-expect-error: custom.
924
this.enter({type: 'mdxjsEsm', value: ''}, token)
1025
this.buffer() // Capture EOLs
1126
}
1227

28+
/** @type {FromMarkdownHandle} */
1329
function exitMdxjsEsm(token) {
1430
const value = this.resume()
1531
const node = this.exit(token)
32+
/** @type {Estree?} */
33+
// @ts-expect-error: custom.
34+
const estree = token.estree
1635

1736
node.value = value
1837

19-
if (token.estree) {
20-
node.data = {estree: token.estree}
38+
if (estree) {
39+
node.data = {estree}
2140
}
2241
}
2342

43+
/** @type {FromMarkdownHandle} */
2444
function exitMdxjsEsmData(token) {
2545
this.config.enter.data.call(this, token)
2646
this.config.exit.data.call(this, token)
2747
}
2848

49+
/**
50+
* @type {ToMarkdownHandle}
51+
* @param {MDXJSEsm} node
52+
*/
2953
function handleMdxjsEsm(node) {
3054
return node.value || ''
3155
}

package.json

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,38 @@
3131
"sideEffects": false,
3232
"type": "module",
3333
"main": "index.js",
34+
"types": "index.d.ts",
3435
"files": [
36+
"index.d.ts",
3537
"index.js"
3638
],
39+
"dependencies": {
40+
"@types/estree-jsx": "^0.0.1",
41+
"@types/mdast": "^3.0.0"
42+
},
3743
"devDependencies": {
44+
"@types/tape": "^4.0.0",
3845
"acorn": "^8.0.0",
3946
"c8": "^7.0.0",
40-
"mdast-util-from-markdown": "^0.8.0",
41-
"mdast-util-to-markdown": "^0.6.0",
42-
"micromark-extension-mdxjs-esm": "^0.3.0",
47+
"mdast-util-from-markdown": "^1.0.0",
48+
"mdast-util-to-markdown": "^1.0.0",
49+
"micromark-extension-mdxjs-esm": "^1.0.0",
4350
"prettier": "^2.0.0",
4451
"remark-cli": "^9.0.0",
4552
"remark-preset-wooorm": "^8.0.0",
53+
"rimraf": "^3.0.0",
4654
"tape": "^5.0.0",
55+
"type-coverage": "^2.0.0",
56+
"typescript": "^4.0.0",
4757
"unist-util-remove-position": "^4.0.0",
4858
"xo": "^0.39.0"
4959
},
5060
"scripts": {
61+
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
5162
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
5263
"test-api": "node --conditions development test.js",
5364
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test.js",
54-
"test": "npm run format && npm run test-coverage"
65+
"test": "npm run build && npm run format && npm run test-coverage"
5566
},
5667
"prettier": {
5768
"tabWidth": 2,
@@ -68,5 +79,11 @@
6879
"plugins": [
6980
"preset-wooorm"
7081
]
82+
},
83+
"typeCoverage": {
84+
"atLeast": 100,
85+
"detail": true,
86+
"strict": true,
87+
"ignoreCatch": true
7188
}
7289
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import fs from 'fs'
5050
import * as acorn from 'acorn'
5151
import {fromMarkdown} from 'mdast-util-from-markdown'
5252
import {toMarkdown} from 'mdast-util-to-markdown'
53-
import mdxjsEsm from 'micromark-extension-mdxjs-esm'
53+
import {mdxjsEsm} from 'micromark-extension-mdxjs-esm'
5454
import {mdxjsEsmFromMarkdown, mdxjsEsmToMarkdown} from 'mdast-util-mdxjs-esm'
5555

5656
const doc = fs.readFileSync('example.mdx')

test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import test from 'tape'
22
import * as acorn from 'acorn'
3-
import fromMarkdown from 'mdast-util-from-markdown'
4-
import toMarkdown from 'mdast-util-to-markdown'
3+
import {fromMarkdown} from 'mdast-util-from-markdown'
4+
import {toMarkdown} from 'mdast-util-to-markdown'
55
import {removePosition} from 'unist-util-remove-position'
6-
import mdxjsEsm from 'micromark-extension-mdxjs-esm'
6+
import {mdxjsEsm} from 'micromark-extension-mdxjs-esm'
77
import {mdxjsEsmFromMarkdown, mdxjsEsmToMarkdown} from './index.js'
88

99
test('markdown -> mdast', (t) => {

tsconfig.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"include": ["*.js"],
3+
"compilerOptions": {
4+
"target": "ES2020",
5+
"lib": ["ES2020"],
6+
"module": "ES2020",
7+
"moduleResolution": "node",
8+
"allowJs": true,
9+
"checkJs": true,
10+
"declaration": true,
11+
"emitDeclarationOnly": true,
12+
"allowSyntheticDefaultImports": true,
13+
"skipLibCheck": true,
14+
"strict": true
15+
}
16+
}

0 commit comments

Comments
 (0)