Skip to content

Commit 07db1f2

Browse files
committed
Add JSDoc based types
1 parent cb91eba commit 07db1f2

File tree

5 files changed

+107
-20
lines changed

5 files changed

+107
-20
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/
3+
*.d.ts
34
*.log
45
.DS_Store
56
yarn.lock

index.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
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: 'mdxFlowExpression', data: {estree?: Estree}}} MDXFlowExpression
10+
* @typedef {Literal & {type: 'mdxSpanExpression', data: {estree?: Estree}}} MDXSpanExpression
11+
*/
12+
113
import stripIndent from 'strip-indent'
214

315
const eol = /\r?\n|\r/g
416

17+
/** @type {FromMarkdownExtension} */
518
export const mdxExpressionFromMarkdown = {
619
enter: {
720
mdxFlowExpression: enterMdxFlowExpression,
@@ -15,6 +28,7 @@ export const mdxExpressionFromMarkdown = {
1528
}
1629
}
1730

31+
/** @type {ToMarkdownExtension} */
1832
export const mdxExpressionToMarkdown = {
1933
handlers: {
2034
mdxFlowExpression: handleMdxExpression,
@@ -26,37 +40,53 @@ export const mdxExpressionToMarkdown = {
2640
]
2741
}
2842

43+
/** @type {FromMarkdownHandle} */
2944
function enterMdxFlowExpression(token) {
45+
// @ts-expect-error: fine.
3046
this.enter({type: 'mdxFlowExpression', value: ''}, token)
3147
this.buffer()
3248
}
3349

50+
/** @type {FromMarkdownHandle} */
3451
function enterMdxTextExpression(token) {
52+
// @ts-expect-error: fine.
3553
this.enter({type: 'mdxTextExpression', value: ''}, token)
3654
this.buffer()
3755
}
3856

57+
/** @type {FromMarkdownHandle} */
3958
function exitMdxExpression(token) {
4059
const value = this.resume()
4160
const node = this.exit(token)
4261

4362
node.value = token.type === 'mdxFlowExpression' ? dedent(value) : value
4463

64+
// @ts-expect-error: estree.
4565
if (token.estree) {
66+
// @ts-expect-error: estree.
4667
node.data = {estree: token.estree}
4768
}
4869
}
4970

71+
/** @type {FromMarkdownHandle} */
5072
function exitMdxExpressionData(token) {
5173
this.config.enter.data.call(this, token)
5274
this.config.exit.data.call(this, token)
5375
}
5476

77+
/**
78+
* @type {ToMarkdownHandle}
79+
* @param {MDXFlowExpression|MDXSpanExpression} node
80+
*/
5581
function handleMdxExpression(node) {
5682
const value = node.value || ''
5783
return '{' + (node.type === 'mdxFlowExpression' ? indent(value) : value) + '}'
5884
}
5985

86+
/**
87+
* @param {string} value
88+
* @returns {string}
89+
*/
6090
function dedent(value) {
6191
const firstLineEnding = /\r?\n|\r/.exec(value)
6292
const position = firstLineEnding
@@ -70,10 +100,16 @@ function dedent(value) {
70100
return value
71101
}
72102

103+
/**
104+
* @param {string} value
105+
* @returns {string}
106+
*/
73107
function indent(value) {
108+
/** @type {Array.<string>} */
74109
const result = []
75110
let start = 0
76111
let line = 0
112+
/** @type {RegExpExecArray|null} */
77113
let match
78114

79115
while ((match = eol.exec(value))) {
@@ -87,6 +123,10 @@ function indent(value) {
87123

88124
return result.join('')
89125

126+
/**
127+
* @param {string} slice
128+
* @returns {void}
129+
*/
90130
function one(slice) {
91131
result.push((line && slice ? ' ' : '') + slice)
92132
}

package.json

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,39 @@
2929
"sideEffects": false,
3030
"type": "module",
3131
"main": "index.js",
32+
"types": "index.d.ts",
3233
"files": [
34+
"index.d.ts",
3335
"index.js"
3436
],
3537
"dependencies": {
38+
"@types/estree-jsx": "^0.0.1",
3639
"strip-indent": "^4.0.0"
3740
},
3841
"devDependencies": {
42+
"@types/acorn": "^4.0.5",
43+
"@types/tape": "^4.0.0",
3944
"acorn": "^8.0.0",
4045
"c8": "^7.0.0",
41-
"mdast-util-from-markdown": "^0.8.0",
42-
"mdast-util-to-markdown": "^0.6.0",
43-
"micromark-extension-mdx-expression": "^0.3.0",
46+
"mdast-util-from-markdown": "^1.0.0",
47+
"mdast-util-to-markdown": "^1.0.0",
48+
"micromark-extension-mdx-expression": "^1.0.0",
4449
"prettier": "^2.0.0",
4550
"remark-cli": "^9.0.0",
4651
"remark-preset-wooorm": "^8.0.0",
52+
"rimraf": "^3.0.0",
4753
"tape": "^5.0.0",
54+
"type-coverage": "^2.0.0",
55+
"typescript": "^4.0.0",
4856
"unist-util-remove-position": "^4.0.0",
4957
"xo": "^0.39.0"
5058
},
5159
"scripts": {
60+
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
5261
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
5362
"test-api": "node --conditions development test.js",
5463
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test.js",
55-
"test": "npm run format && npm run test-coverage"
64+
"test": "npm run build && npm run format && npm run test-coverage"
5665
},
5766
"prettier": {
5867
"tabWidth": 2,
@@ -69,5 +78,11 @@
6978
"plugins": [
7079
"preset-wooorm"
7180
]
81+
},
82+
"typeCoverage": {
83+
"atLeast": 100,
84+
"detail": true,
85+
"strict": true,
86+
"ignoreCatch": true
7287
}
7388
}

test.js

Lines changed: 31 additions & 16 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 mdxExpression from 'micromark-extension-mdx-expression'
6+
import {mdxExpression} from 'micromark-extension-mdx-expression'
77
import {mdxExpressionFromMarkdown, mdxExpressionToMarkdown} from './index.js'
88

99
test('markdown -> mdast', (t) => {
@@ -156,13 +156,18 @@ test('markdown -> mdast', (t) => {
156156
mdastExtensions: [mdxExpressionFromMarkdown]
157157
}),
158158
true
159-
).children[0],
159+
),
160160
{
161-
type: 'paragraph',
161+
type: 'root',
162162
children: [
163-
{type: 'text', value: 'a '},
164-
{type: 'mdxTextExpression', value: '\t \n'},
165-
{type: 'text', value: ' c'}
163+
{
164+
type: 'paragraph',
165+
children: [
166+
{type: 'text', value: 'a '},
167+
{type: 'mdxTextExpression', value: '\t \n'},
168+
{type: 'text', value: ' c'}
169+
]
170+
}
166171
]
167172
},
168173
'should support an empty text expression (agnostic)'
@@ -175,12 +180,17 @@ test('markdown -> mdast', (t) => {
175180
mdastExtensions: [mdxExpressionFromMarkdown]
176181
}),
177182
true
178-
).children[0],
183+
),
179184
{
180-
type: 'paragraph',
185+
type: 'root',
181186
children: [
182-
{type: 'mdxTextExpression', value: ' a { b } c '},
183-
{type: 'text', value: '.'}
187+
{
188+
type: 'paragraph',
189+
children: [
190+
{type: 'mdxTextExpression', value: ' a { b } c '},
191+
{type: 'text', value: '.'}
192+
]
193+
}
184194
]
185195
},
186196
'should support an balanced braces in a flow expression (agnostic)'
@@ -193,12 +203,17 @@ test('markdown -> mdast', (t) => {
193203
mdastExtensions: [mdxExpressionFromMarkdown]
194204
}),
195205
true
196-
).children[0],
206+
),
197207
{
198-
type: 'paragraph',
208+
type: 'root',
199209
children: [
200-
{type: 'mdxTextExpression', value: ' a /* { */ '},
201-
{type: 'text', value: '.'}
210+
{
211+
type: 'paragraph',
212+
children: [
213+
{type: 'mdxTextExpression', value: ' a /* { */ '},
214+
{type: 'text', value: '.'}
215+
]
216+
}
202217
]
203218
},
204219
'should support a commented-out unbalanced brace in a flow expression (gnostic)'

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)