Skip to content

Commit 41553e9

Browse files
committed
Change types to use mdast types
1 parent fc69128 commit 41553e9

File tree

12 files changed

+72
-71
lines changed

12 files changed

+72
-71
lines changed

index.js

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/**
2-
* @typedef {import('unist').Node} Node
3-
* @typedef {import('unist').Parent} Parent
4-
* @typedef {import('unist-util-visit').Visitor<Node>} Visitor
2+
* @typedef {import('mdast').Root|import('mdast').Content} Node
3+
* @typedef {Extract<Node, import('mdast').Parent>} Parent
54
*
65
* @typedef ZoneInfo
76
* @property {number} start
@@ -32,50 +31,52 @@ export function zone(node, name, callback) {
3231
/** @type {Parent|undefined} */
3332
let scope
3433

35-
visit(node, gather)
34+
visit(
35+
node,
36+
/**
37+
* Gather one dimensional zones.
38+
*/
39+
(node, index, parent) => {
40+
const info = commentMarker(node)
41+
const match =
42+
info && info.name === name && info.attributes.match(/(start|end)\b/)
43+
const type = match && match[0]
3644

37-
/**
38-
* Gather one dimensional zones.
39-
* @type {Visitor}
40-
*/
41-
function gather(node, index, parent) {
42-
const info = commentMarker(node)
43-
const match =
44-
info && info.name === name && info.attributes.match(/(start|end)\b/)
45-
const type = match && match[0]
45+
if (parent && index !== null && type) {
46+
if (!scope && type === 'start') {
47+
level = 0
48+
marker = node
49+
scope = /** @type {Parent} */ (parent)
50+
}
4651

47-
if (parent && index !== null && type) {
48-
if (!scope && type === 'start') {
49-
level = 0
50-
marker = node
51-
scope = parent
52-
}
52+
if (typeof level === 'number' && marker && scope && parent === scope) {
53+
if (type === 'start') {
54+
level++
55+
} else {
56+
level--
57+
}
5358

54-
if (typeof level === 'number' && marker && scope && parent === scope) {
55-
if (type === 'start') {
56-
level++
57-
} else {
58-
level--
59-
}
59+
if (type === 'end' && !level) {
60+
// @ts-expect-error: Assume `scope` is a valid parent of `node`.
61+
const start = scope.children.indexOf(marker)
6062

61-
if (type === 'end' && !level) {
62-
const start = scope.children.indexOf(marker)
63+
const result = callback(
64+
marker,
65+
scope.children.slice(start + 1, index),
66+
node,
67+
{start, end: index, parent: scope}
68+
)
6369

64-
const result = callback(
65-
marker,
66-
scope.children.slice(start + 1, index),
67-
node,
68-
{start, end: index, parent}
69-
)
70+
if (result) {
71+
// @ts-expect-error: Assume the correct children are passed.
72+
scope.children.splice(start, index - start + 1, ...result)
73+
}
7074

71-
if (result) {
72-
scope.children.splice(start, index - start + 1, ...result)
75+
marker = undefined
76+
scope = undefined
7377
}
74-
75-
marker = undefined
76-
scope = undefined
7778
}
7879
}
7980
}
80-
}
81+
)
8182
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
"index.js"
3636
],
3737
"dependencies": {
38-
"@types/mdast": "^3.0.3",
39-
"@types/unist": "^2.0.3",
38+
"@types/mdast": "^3.0.0",
39+
"@types/unist": "^2.0.0",
4040
"mdast-comment-marker": "^2.0.0",
4141
"unist-util-visit": "^3.0.0"
4242
},

test/fixtures/mismatched/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @param {import('tape').Test} _
33
* @param {import('../../../index.js').zone} zone
4-
* @param {import('unist').Node} tree
4+
* @param {import('mdast').Root} tree
55
*/
66
export default function assertion(_, zone, tree) {
77
zone(tree, 'foo', handle)

test/fixtures/nodes/index.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import assert from 'assert'
2+
13
/**
24
* @param {import('tape').Test} t
35
* @param {import('../../../index.js').zone} zone
4-
* @param {import('unist').Node} tree
6+
* @param {import('mdast').Root} tree
57
*/
68
export default function assertion(t, zone, tree) {
79
let count = 0
@@ -16,12 +18,12 @@ export default function assertion(t, zone, tree) {
1618
st.equal(nodes.length, 1)
1719
const head = nodes[0]
1820
st.equal(head.type, 'paragraph')
19-
// @ts-expect-error: too vague.
21+
assert(head.type === 'paragraph')
2022
st.equal(head.children.length, 1)
21-
// @ts-expect-error: too vague.
22-
st.equal(head.children[0].type, 'text')
23-
// @ts-expect-error: too vague.
24-
st.equal(head.children[0].value, 'Foo.')
23+
const headHead = head.children[0]
24+
st.equal(headHead.type, 'text')
25+
assert(headHead.type === 'text')
26+
st.equal(headHead.value, 'Foo.')
2527
st.equal(++count, 1)
2628
}
2729
})

test/fixtures/non-marker/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @param {import('tape').Test} _
33
* @param {import('../../../index.js').zone} zone
4-
* @param {import('unist').Node} tree
4+
* @param {import('mdast').Root} tree
55
*/
66
export default function assertion(_, zone, tree) {
77
zone(tree, 'foo', handle)

test/fixtures/range-children/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @param {import('tape').Test} t
33
* @param {import('../../../index.js').zone} zone
4-
* @param {import('unist').Node} tree
4+
* @param {import('mdast').Root} tree
55
*/
66
export default function assertion(t, zone, tree) {
77
let count = 0

test/fixtures/remove-children/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @param {import('tape').Test} _
33
* @param {import('../../../index.js').zone} zone
4-
* @param {import('unist').Node} tree
4+
* @param {import('mdast').Root} tree
55
*/
66
export default function assertion(_, zone, tree) {
77
zone(tree, 'foo', handle)

test/fixtures/remove/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @param {import('tape').Test} _
33
* @param {import('../../../index.js').zone} zone
4-
* @param {import('unist').Node} tree
4+
* @param {import('mdast').Root} tree
55
*/
66
export default function assertion(_, zone, tree) {
77
zone(tree, 'foo', handle)

test/fixtures/replace-children/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @param {import('tape').Test} _
33
* @param {import('../../../index.js').zone} zone
4-
* @param {import('unist').Node} tree
4+
* @param {import('mdast').Root} tree
55
*/
66
export default function assertion(_, zone, tree) {
77
zone(tree, 'foo', handle)

test/fixtures/replace/index.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
/**
22
* @param {import('tape').Test} _
33
* @param {import('../../../index.js').zone} zone
4-
* @param {import('unist').Node} tree
4+
* @param {import('mdast').Root} tree
55
*/
66
export default function assertion(_, zone, tree) {
7-
zone(tree, 'foo', handle)
8-
9-
function handle() {
10-
return [
11-
{
12-
type: 'heading',
13-
depth: 2,
14-
children: [{type: 'text', value: 'Bar'}]
15-
}
16-
]
17-
}
7+
zone(tree, 'foo', () => [
8+
{
9+
type: 'heading',
10+
depth: 2,
11+
children: [{type: 'text', value: 'Bar'}]
12+
}
13+
])
1814
}

test/fixtures/simple/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import assert from 'assert'
2+
13
/**
24
* @param {import('tape').Test} t
35
* @param {import('../../../index.js').zone} zone
4-
* @param {import('unist').Node} tree
6+
* @param {import('mdast').Root} tree
57
*/
68
export default function assertion(t, zone, tree) {
79
t.test('range', (st) => {
@@ -12,11 +14,11 @@ export default function assertion(t, zone, tree) {
1214
/** @type {import('../../../index.js').Handler} */
1315
function handle(start, nodes, end) {
1416
st.equal(start.type, 'html')
15-
// @ts-expect-error: too vague.
17+
assert(start.type === 'html')
1618
st.equal(start.value, '<!--foo start bar="baz"-->')
1719
st.deepEqual(nodes, [])
1820
st.equal(end.type, 'html')
19-
// @ts-expect-error: too vague.
21+
assert(end.type === 'html')
2022
st.equal(end.value, '<!--foo end qux="quux"-->')
2123
}
2224
})

test/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @typedef {import('tape').Test} Test
3-
* @typedef {import('unist').Node} Node
3+
* @typedef {import('mdast').Root} Root
44
* @typedef {import('../index.js').zone} Zone
55
*/
66

@@ -28,7 +28,7 @@ test('mdast-zone', async (t) => {
2828
} catch {}
2929

3030
/* eslint-disable no-await-in-loop */
31-
/** @type {(t: Test, zone: Zone, node: Node) => void} */
31+
/** @type {(t: Test, zone: Zone, node: Root) => void} */
3232
const mod =
3333
// @ts-ignore hush.
3434
(await import(new URL('fixtures/' + name + '/index.js', import.meta.url)))
@@ -37,7 +37,7 @@ test('mdast-zone', async (t) => {
3737

3838
remark()
3939
.use(() => (tree) => {
40-
mod(t, zone, tree)
40+
mod(t, zone, /** @type {Root} */ (tree))
4141
})
4242
.process(
4343
fs.readFileSync(path.join(root, name, 'input.md')),

0 commit comments

Comments
 (0)