Skip to content

Commit

Permalink
Update @types/unist
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jul 7, 2023
1 parent b63722b commit 9e86da1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 47 deletions.
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,20 @@
"index.js"
],
"dependencies": {
"@types/unist": "^2.0.0",
"unist-util-is": "^5.0.0",
"unist-util-visit-parents": "^5.0.0"
"@types/unist": "^3.0.0",
"unist-util-is": "^6.0.0",
"unist-util-visit-parents": "^6.0.0"
},
"devDependencies": {
"@types/mdast": "^4.0.0",
"@types/node": "^20.0.0",
"c8": "^8.0.0",
"prettier": "^2.0.0",
"remark-cli": "^11.0.0",
"remark-preset-wooorm": "^9.0.0",
"type-coverage": "^2.0.0",
"typescript": "^5.0.0",
"unist-builder": "^3.0.0",
"unist-builder": "^4.0.0",
"xo": "^0.54.0"
},
"scripts": {
Expand Down
96 changes: 53 additions & 43 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/**
* @typedef {import('unist').Literal<string>} Literal
* @typedef {import('mdast').Text} Text
* @typedef {import('mdast').Emphasis} Emphasis
* @typedef {import('mdast').Root} Root
* @typedef {import('unist').Node} Node
*/

Expand All @@ -16,24 +18,26 @@ test('remove', async function (t) {
})

await t.test('should compare nodes by partial properties', function () {
const leaf1 = u('leaf', '1')
const leaf2 = u('leaf', '2')
const leaf1 = u('text', '1')
const leaf2 = u('text', '2')
const children = [leaf1, leaf2]
const tree = u('node', children)
/** @type {Emphasis} */
const tree = u('emphasis', children)

const next = remove(tree, {value: '2'})

assert.deepEqual(tree, u('node', [leaf1]))
assert.deepEqual(tree, u('emphasis', [leaf1]))
assert.equal(next, tree)
assert.equal(next.children, children)
assert.equal(next.children[0], leaf1)
})

await t.test('should remove parent nodes', function () {
const leaf1 = u('leaf', '1')
const leaf2 = u('leaf', '2')
const parent = u('parent', [leaf1])
const leaf1 = u('text', '1')
const leaf2 = u('text', '2')
const parent = u('emphasis', [leaf1])
const children = [parent, leaf2]
/** @type {Root} */
const tree = u('root', children)

const next = remove(tree, test)
Expand All @@ -55,103 +59,109 @@ test('remove', async function (t) {
await t.test(
'should return `undefined` if root node is removed',
function () {
const tree = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')])
/** @type {Root} */
const tree = u('root', [u('emphasis', [u('text', '1')]), u('text', '2')])
const next = remove(tree, 'root')

assert.equal(next, undefined)
}
)

await t.test('should cascade (remove) root nodes', function () {
const tree = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')])
const next = remove(tree, 'leaf')
/** @type {Root} */
const tree = u('root', [u('emphasis', [u('text', '1')]), u('text', '2')])
const next = remove(tree, 'text')

assert.equal(next, undefined)
})

await t.test(
'should not cascade (remove) nodes that were empty initially',
function () {
const tree = u('node', [u('node', []), u('node', [u('leaf')])])
/** @type {Root} */
const tree = u('root', [
u('emphasis', []),
u('emphasis', [u('text', 'x')])
])

remove(tree, 'leaf')
remove(tree, 'text')

assert.deepEqual(tree, u('node', [u('node', [])]))
assert.deepEqual(tree, u('root', [u('emphasis', [])]))
}
)

await t.test('should support type tests', function () {
const tree = u('node', [u('node', [u('leaf', '1')]), u('leaf', '2')])
/** @type {Root} */
const tree = u('root', [u('emphasis', [u('text', '1')]), u('text', '2')])

remove(tree, {cascade: false}, 'leaf')
remove(tree, {cascade: false}, 'text')

assert.deepEqual(tree, u('node', [u('node', [])]))
assert.deepEqual(tree, u('root', [u('emphasis', [])]))
})

await t.test('should support function tests', function () {
const tree = u('node', [u('node', [u('leaf', '1')]), u('leaf', '2')])
/** @type {Emphasis} */
const tree = u('emphasis', [
u('emphasis', [u('text', '1')]),
u('text', '2')
])

remove(tree, {cascade: false}, test)

assert.deepEqual(tree, u('node', [u('node', []), u('leaf', '2')]))
assert.deepEqual(tree, u('emphasis', [u('emphasis', []), u('text', '2')]))

/**
* @param {Node} node
* @returns {boolean}
*/
function test(node) {
return literal(node) && node.value === '1'
return node.type === 'text' && 'value' in node && node.value === '1'
}
})

await t.test('should support `cascade = true`', function () {
const tree = u('root', [u('node', [u('leaf', '1')]), u('leaf', '2')])
const next = remove(tree, {cascade: true}, 'leaf')
/** @type {Root} */
const tree = u('root', [u('emphasis', [u('text', '1')]), u('text', '2')])
const next = remove(tree, {cascade: true}, 'text')

assert.equal(next, undefined)
})

await t.test('should support `cascade = false`', function () {
const leaf1 = u('leaf', '1')
const leaf2 = u('leaf', '2')
const leaf1 = u('text', '1')
const leaf2 = u('text', '2')
const nodeChildren = [leaf1]
const node = u('node', nodeChildren)
const node = u('emphasis', nodeChildren)
const siblings = [node, leaf2]
/** @type {Root} */
const tree = u('root', siblings)

const next = remove(tree, {cascade: false}, 'leaf')
const next = remove(tree, {cascade: false}, 'text')

assert.deepEqual(tree, u('root', [u('node', [])]))
assert.deepEqual(tree, u('root', [u('emphasis', [])]))
assert.equal(next, tree)
assert.equal(next.children, siblings)
assert.equal(next.children[0], node)
assert.equal(next.children[0].children, nodeChildren)
})

await t.test('should support the example from readme', function () {
/** @type {Root} */
const tree = u('root', [
u('leaf', '1'),
u('node', [
u('leaf', '2'),
u('node', [u('leaf', '3'), u('other', '4')]),
u('node', [u('leaf', '5')])
u('text', '1'),
u('emphasis', [
u('text', '2'),
u('emphasis', [u('text', '3'), u('inlineCode', '4')]),
u('emphasis', [u('text', '5')])
]),
u('leaf', '6')
u('text', '6')
])

remove(tree, 'leaf')
remove(tree, 'text')

assert.deepEqual(
tree,
u('root', [u('node', [u('node', [u('other', '4')])])])
u('root', [u('emphasis', [u('emphasis', [u('inlineCode', '4')])])])
)
})
})

/**
* @param {Node} node
* @returns {node is Literal}
*/
function literal(node) {
return 'value' in node
}

0 comments on commit 9e86da1

Please sign in to comment.