Skip to content

Commit 7de649f

Browse files
committed
Add JSDoc based types
1 parent cdd3e04 commit 7de649f

File tree

5 files changed

+185
-52
lines changed

5 files changed

+185
-52
lines changed

.gitignore

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

index.js

+35-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1-
import defaultListItem from 'mdast-util-to-markdown/lib/handle/list-item.js'
1+
/**
2+
* @typedef {import('mdast').ListItem} ListItem
3+
* @typedef {import('mdast').Paragraph} Paragraph
4+
* @typedef {import('mdast').BlockContent} BlockContent
5+
* @typedef {import('mdast-util-from-markdown').Extension} FromMarkdownExtension
6+
* @typedef {import('mdast-util-from-markdown').Handle} FromMarkdownHandle
7+
* @typedef {import('mdast-util-to-markdown').Options} ToMarkdownExtension
8+
* @typedef {import('mdast-util-to-markdown').Handle} ToMarkdownHandle
9+
*/
210

11+
import {listItem} from 'mdast-util-to-markdown/lib/handle/list-item.js'
12+
13+
/** @type {FromMarkdownExtension} */
314
export const gfmTaskListItemFromMarkdown = {
415
exit: {
516
taskListCheckValueChecked: exitCheck,
@@ -8,23 +19,31 @@ export const gfmTaskListItemFromMarkdown = {
819
}
920
}
1021

22+
/** @type {ToMarkdownExtension} */
1123
export const gfmTaskListItemToMarkdown = {
1224
unsafe: [{atBreak: true, character: '-', after: '[:|-]'}],
1325
handlers: {listItem: listItemWithTaskListItem}
1426
}
1527

28+
/** @type {FromMarkdownHandle} */
1629
function exitCheck(token) {
1730
// We’re always in a paragraph, in a list item.
1831
this.stack[this.stack.length - 2].checked =
1932
token.type === 'taskListCheckValueChecked'
2033
}
2134

35+
/** @type {FromMarkdownHandle} */
2236
function exitParagraphWithTaskListItem(token) {
2337
const parent = this.stack[this.stack.length - 2]
38+
/** @type {Paragraph} */
39+
// @ts-expect-error: must be true.
2440
const node = this.stack[this.stack.length - 1]
41+
/** @type {BlockContent[]} */
42+
// @ts-expect-error: check whether `parent` is a `listItem` later.
2543
const siblings = parent.children
2644
const head = node.children[0]
2745
let index = -1
46+
/** @type {Paragraph|undefined} */
2847
let firstParaghraph
2948

3049
if (
@@ -35,8 +54,9 @@ function exitParagraphWithTaskListItem(token) {
3554
head.type === 'text'
3655
) {
3756
while (++index < siblings.length) {
38-
if (siblings[index].type === 'paragraph') {
39-
firstParaghraph = siblings[index]
57+
const sibling = siblings[index]
58+
if (sibling.type === 'paragraph') {
59+
firstParaghraph = sibling
4060
break
4161
}
4262
}
@@ -48,8 +68,11 @@ function exitParagraphWithTaskListItem(token) {
4868
if (head.value.length === 0) {
4969
node.children.shift()
5070
} else {
71+
// @ts-expect-error: must be true.
5172
head.position.start.column++
73+
// @ts-expect-error: must be true.
5274
head.position.start.offset++
75+
// @ts-expect-error: must be true.
5376
node.position.start = Object.assign({}, head.position.start)
5477
}
5578
}
@@ -58,16 +81,24 @@ function exitParagraphWithTaskListItem(token) {
5881
this.exit(token)
5982
}
6083

84+
/**
85+
* @type {ToMarkdownHandle}
86+
* @param {ListItem} node
87+
*/
6188
function listItemWithTaskListItem(node, parent, context) {
6289
const head = node.children[0]
63-
let value = defaultListItem(node, parent, context)
90+
let value = listItem(node, parent, context)
6491

6592
if (typeof node.checked === 'boolean' && head && head.type === 'paragraph') {
6693
value = value.replace(/^(?:[*+-]|\d+\.)([\r\n]| {1,3})/, check)
6794
}
6895

6996
return value
7097

98+
/**
99+
* @param {string} $0
100+
* @returns {string}
101+
*/
71102
function check($0) {
72103
return $0 + '[' + (node.checked ? 'x' : ' ') + '] '
73104
}

package.json

+19-5
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,36 @@
3232
"sideEffects": false,
3333
"type": "module",
3434
"main": "index.js",
35+
"types": "index.d.ts",
3536
"files": [
37+
"index.d.ts",
3638
"index.js"
3739
],
3840
"dependencies": {
39-
"mdast-util-to-markdown": "~0.6.0"
41+
"@types/mdast": "^3.0.3",
42+
"mdast-util-to-markdown": "^1.0.0"
4043
},
4144
"devDependencies": {
42-
"c8": "^7.7.3",
43-
"mdast-util-from-markdown": "^0.8.0",
44-
"micromark-extension-gfm-task-list-item": "^0.3.0",
45+
"@types/tape": "^4.0.0",
46+
"c8": "^7.0.0",
47+
"mdast-util-from-markdown": "^1.0.0",
48+
"micromark-extension-gfm-task-list-item": "^1.0.0",
4549
"prettier": "^2.0.0",
4650
"remark-cli": "^9.0.0",
4751
"remark-preset-wooorm": "^8.0.0",
52+
"rimraf": "^3.0.0",
4853
"tape": "^5.0.0",
54+
"type-coverage": "^2.0.0",
55+
"typescript": "^4.0.0",
4956
"unist-util-remove-position": "^4.0.0",
5057
"xo": "^0.39.0"
5158
},
5259
"scripts": {
60+
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
5361
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
5462
"test-api": "node --conditions development test.js",
5563
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test.js",
56-
"test": "npm run format && npm run test-coverage"
64+
"test": "npm run build && npm run format && npm run test-coverage"
5765
},
5866
"prettier": {
5967
"tabWidth": 2,
@@ -70,5 +78,11 @@
7078
"plugins": [
7179
"preset-wooorm"
7280
]
81+
},
82+
"typeCoverage": {
83+
"atLeast": 100,
84+
"detail": true,
85+
"strict": true,
86+
"ignoreCatch": true
7387
}
7488
}

test.js

+114-43
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import test from 'tape'
2-
import fromMarkdown from 'mdast-util-from-markdown'
3-
import toMarkdown from 'mdast-util-to-markdown'
2+
import {fromMarkdown} from 'mdast-util-from-markdown'
3+
import {toMarkdown} from 'mdast-util-to-markdown'
44
import {removePosition} from 'unist-util-remove-position'
5-
import gfmTaskListItem from 'micromark-extension-gfm-task-list-item'
5+
import {gfmTaskListItem} from 'micromark-extension-gfm-task-list-item'
66
import {
77
gfmTaskListItemFromMarkdown,
88
gfmTaskListItemToMarkdown
@@ -73,15 +73,28 @@ test('markdown -> mdast', (t) => {
7373
mdastExtensions: [gfmTaskListItemFromMarkdown]
7474
}),
7575
true
76-
).children[0].children[0],
76+
),
7777
{
78-
type: 'listItem',
79-
spread: false,
80-
checked: true,
78+
type: 'root',
8179
children: [
8280
{
83-
type: 'paragraph',
84-
children: [{type: 'text', value: 'after a blank line'}]
81+
type: 'list',
82+
ordered: false,
83+
start: null,
84+
spread: false,
85+
children: [
86+
{
87+
type: 'listItem',
88+
spread: false,
89+
checked: true,
90+
children: [
91+
{
92+
type: 'paragraph',
93+
children: [{type: 'text', value: 'after a blank line'}]
94+
}
95+
]
96+
}
97+
]
8598
}
8699
]
87100
},
@@ -95,12 +108,27 @@ test('markdown -> mdast', (t) => {
95108
mdastExtensions: [gfmTaskListItemFromMarkdown]
96109
}),
97110
true
98-
).children[0].children[0],
111+
),
99112
{
100-
type: 'listItem',
101-
spread: false,
102-
checked: true,
103-
children: [{type: 'paragraph', children: [{type: 'text', value: 'tab'}]}]
113+
type: 'root',
114+
children: [
115+
{
116+
type: 'list',
117+
ordered: false,
118+
start: null,
119+
spread: false,
120+
children: [
121+
{
122+
type: 'listItem',
123+
spread: false,
124+
checked: true,
125+
children: [
126+
{type: 'paragraph', children: [{type: 'text', value: 'tab'}]}
127+
]
128+
}
129+
]
130+
}
131+
]
104132
},
105133
'should support a task list item follwed by a tab'
106134
)
@@ -112,20 +140,36 @@ test('markdown -> mdast', (t) => {
112140
mdastExtensions: [gfmTaskListItemFromMarkdown]
113141
}),
114142
true
115-
).children[0].children[0],
143+
),
116144
{
117-
type: 'listItem',
118-
spread: false,
119-
checked: true,
145+
type: 'root',
120146
children: [
121147
{
122-
type: 'definition',
123-
identifier: 'x',
124-
label: 'x',
125-
title: null,
126-
url: 'definition'
127-
},
128-
{type: 'paragraph', children: [{type: 'text', value: 'tasklist'}]}
148+
type: 'list',
149+
ordered: false,
150+
start: null,
151+
spread: false,
152+
children: [
153+
{
154+
type: 'listItem',
155+
spread: false,
156+
checked: true,
157+
children: [
158+
{
159+
type: 'definition',
160+
identifier: 'x',
161+
label: 'x',
162+
title: null,
163+
url: 'definition'
164+
},
165+
{
166+
type: 'paragraph',
167+
children: [{type: 'text', value: 'tasklist'}]
168+
}
169+
]
170+
}
171+
]
172+
}
129173
]
130174
},
131175
'should support a task list item after a definition'
@@ -138,8 +182,13 @@ test('markdown -> mdast', (t) => {
138182
mdastExtensions: [gfmTaskListItemFromMarkdown]
139183
}),
140184
true
141-
).children[0],
142-
{type: 'paragraph', children: [{type: 'text', value: '[x] tasklist'}]},
185+
),
186+
{
187+
type: 'root',
188+
children: [
189+
{type: 'paragraph', children: [{type: 'text', value: '[x] tasklist'}]}
190+
]
191+
},
143192
'should not support a task list item when not in a list item'
144193
)
145194

@@ -150,15 +199,30 @@ test('markdown -> mdast', (t) => {
150199
mdastExtensions: [gfmTaskListItemFromMarkdown]
151200
}),
152201
true
153-
).children[0].children[0],
202+
),
154203
{
155-
type: 'listItem',
156-
spread: false,
157-
checked: true,
204+
type: 'root',
158205
children: [
159206
{
160-
type: 'paragraph',
161-
children: [{type: 'emphasis', children: [{type: 'text', value: 'b'}]}]
207+
type: 'list',
208+
ordered: false,
209+
start: null,
210+
spread: false,
211+
children: [
212+
{
213+
type: 'listItem',
214+
spread: false,
215+
checked: true,
216+
children: [
217+
{
218+
type: 'paragraph',
219+
children: [
220+
{type: 'emphasis', children: [{type: 'text', value: 'b'}]}
221+
]
222+
}
223+
]
224+
}
225+
]
162226
}
163227
]
164228
},
@@ -172,19 +236,26 @@ test('markdown -> mdast', (t) => {
172236
mdastExtensions: [gfmTaskListItemFromMarkdown]
173237
}),
174238
true
175-
).children[0].children[0],
239+
),
176240
{
177-
type: 'listItem',
178-
spread: true,
179-
checked: true,
241+
type: 'root',
180242
children: [
181243
{
182-
type: 'paragraph',
183-
children: [{type: 'text', value: 'a'}]
184-
},
185-
{
186-
type: 'paragraph',
187-
children: [{type: 'text', value: 'b'}]
244+
type: 'list',
245+
ordered: false,
246+
start: null,
247+
spread: false,
248+
children: [
249+
{
250+
type: 'listItem',
251+
spread: true,
252+
checked: true,
253+
children: [
254+
{type: 'paragraph', children: [{type: 'text', value: 'a'}]},
255+
{type: 'paragraph', children: [{type: 'text', value: 'b'}]}
256+
]
257+
}
258+
]
188259
}
189260
]
190261
},

tsconfig.json

+16
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)