Skip to content

Commit ea665e0

Browse files
committed
Refactor code-style
1 parent 4bf14d8 commit ea665e0

File tree

3 files changed

+13
-20
lines changed

3 files changed

+13
-20
lines changed

lib/index.js

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
11
/**
2-
* @typedef {import('mdast').Content} Content
32
* @typedef {import('mdast').ListItem} ListItem
43
* @typedef {import('mdast').Paragraph} Paragraph
5-
* @typedef {import('mdast').Parent} Parent
6-
* @typedef {import('mdast').Root} Root
74
* @typedef {import('mdast-util-from-markdown').CompileContext} CompileContext
85
* @typedef {import('mdast-util-from-markdown').Extension} FromMarkdownExtension
96
* @typedef {import('mdast-util-from-markdown').Handle} FromMarkdownHandle
107
* @typedef {import('mdast-util-to-markdown').Options} ToMarkdownExtension
118
* @typedef {import('mdast-util-to-markdown').Handle} ToMarkdownHandle
129
*/
1310

14-
/**
15-
* @typedef {Extract<Root | Content, Parent>} Parents
16-
*/
17-
11+
import {ok as assert} from 'devlop'
1812
import {defaultHandlers} from 'mdast-util-to-markdown'
1913

20-
// To do: next major: rename `context` -> `state`, `safeOptions` -> `info`, use
21-
// `track` from `state`.
2214
// To do: next major: replace exports with functions.
23-
// To do: next major: use `defaulthandlers.listItem`.
2415

2516
/**
2617
* Extension for `mdast-util-from-markdown` to enable GFM task list items.
@@ -50,8 +41,9 @@ export const gfmTaskListItemToMarkdown = {
5041
* @type {FromMarkdownHandle}
5142
*/
5243
function exitCheck(token) {
53-
const node = /** @type {ListItem} */ (this.stack[this.stack.length - 2])
5444
// We’re always in a paragraph, in a list item.
45+
const node = this.stack[this.stack.length - 2]
46+
assert(node.type === 'listItem')
5547
node.checked = token.type === 'taskListCheckValueChecked'
5648
}
5749

@@ -60,14 +52,15 @@ function exitCheck(token) {
6052
* @type {FromMarkdownHandle}
6153
*/
6254
function exitParagraphWithTaskListItem(token) {
63-
const parent = /** @type {Parents} */ (this.stack[this.stack.length - 2])
55+
const parent = this.stack[this.stack.length - 2]
6456

6557
if (
6658
parent &&
6759
parent.type === 'listItem' &&
6860
typeof parent.checked === 'boolean'
6961
) {
70-
const node = /** @type {Paragraph} */ (this.stack[this.stack.length - 1])
62+
const node = this.stack[this.stack.length - 1]
63+
assert(node.type === 'paragraph')
7164
const head = node.children[0]
7265

7366
if (head && head.type === 'text') {
@@ -110,19 +103,19 @@ function exitParagraphWithTaskListItem(token) {
110103
* @type {ToMarkdownHandle}
111104
* @param {ListItem} node
112105
*/
113-
function listItemWithTaskListItem(node, parent, context, safeOptions) {
106+
function listItemWithTaskListItem(node, parent, state, info) {
114107
const head = node.children[0]
115108
const checkable =
116109
typeof node.checked === 'boolean' && head && head.type === 'paragraph'
117110
const checkbox = '[' + (node.checked ? 'x' : ' ') + '] '
118-
const tracker = context.createTracker(safeOptions)
111+
const tracker = state.createTracker(info)
119112

120113
if (checkable) {
121114
tracker.move(checkbox)
122115
}
123116

124-
let value = defaultHandlers.listItem(node, parent, context, {
125-
...safeOptions,
117+
let value = defaultHandlers.listItem(node, parent, state, {
118+
...info,
126119
...tracker.current()
127120
})
128121

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
],
4141
"dependencies": {
4242
"@types/mdast": "^4.0.0",
43+
"devlop": "^1.0.0",
4344
"mdast-util-from-markdown": "^2.0.0",
4445
"mdast-util-to-markdown": "^2.0.0"
4546
},

test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import assert from 'node:assert/strict'
22
import test from 'node:test'
3+
import {gfmTaskListItem} from 'micromark-extension-gfm-task-list-item'
34
import {fromMarkdown} from 'mdast-util-from-markdown'
45
import {toMarkdown} from 'mdast-util-to-markdown'
56
import {removePosition} from 'unist-util-remove-position'
6-
import {gfmTaskListItem} from 'micromark-extension-gfm-task-list-item'
77
import {
88
gfmTaskListItemFromMarkdown,
99
gfmTaskListItemToMarkdown
1010
} from './index.js'
11-
import * as mod from './index.js'
1211

1312
test('core', async function (t) {
1413
await t.test('should expose the public api', async function () {
15-
assert.deepEqual(Object.keys(mod).sort(), [
14+
assert.deepEqual(Object.keys(await import('./index.js')).sort(), [
1615
'gfmTaskListItemFromMarkdown',
1716
'gfmTaskListItemToMarkdown'
1817
])

0 commit comments

Comments
 (0)