Skip to content

Commit 7a4a5e1

Browse files
committed
Change to return undefined
1 parent 6b3e2d6 commit 7a4a5e1

File tree

5 files changed

+24
-66
lines changed

5 files changed

+24
-66
lines changed

index.test-d.ts

-15
This file was deleted.

lib/index.js

+11-32
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,29 @@
11
/**
22
* @typedef {import('mdast').Nodes} Nodes
3-
* @typedef {import('mdast').Paragraph} Paragraph
43
*/
54

65
import {visit} from 'unist-util-visit'
76

87
/**
98
* Remove empty paragraphs in `tree`.
109
*
11-
* @template {Nodes} Tree
12-
* Node type.
13-
* @param {Tree} tree
10+
* @param {Nodes} tree
1411
* Tree to change.
15-
* @returns {Tree extends Paragraph ? Tree | null : Tree}
16-
* Changed `tree`, or `null` if it was an empty paragraph.
12+
* @returns {undefined}
13+
* Nothing.
1714
*/
1815
export function squeezeParagraphs(tree) {
19-
if (emptyParagraph(tree)) {
20-
// @ts-expect-error: it’s an empty paragraph.
21-
return null
22-
}
23-
2416
visit(tree, function (node, index, parent) {
25-
if (index !== undefined && parent && emptyParagraph(node)) {
17+
if (
18+
index !== undefined &&
19+
parent &&
20+
node.type === 'paragraph' &&
21+
node.children.every(function (child) {
22+
return child.type === 'text' && /^\s*$/.test(child.value)
23+
})
24+
) {
2625
parent.children.splice(index, 1)
2726
return index
2827
}
2928
})
30-
31-
// @ts-expect-error: it’s not an empty paragraph.
32-
return tree
33-
}
34-
35-
/**
36-
* Check if a node is an empty paragraph.
37-
*
38-
* @param {Nodes} node
39-
* Node to check.
40-
* @returns {boolean}
41-
* Whether `node` was an empty paragraph.
42-
*/
43-
function emptyParagraph(node) {
44-
return (
45-
node.type === 'paragraph' &&
46-
node.children.every(function (child) {
47-
return child.type === 'text' && /^\s*$/.test(child.value)
48-
})
49-
)
5029
}

package.json

-13
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@
4949
"prettier": "^2.0.0",
5050
"remark-cli": "^11.0.0",
5151
"remark-preset-wooorm": "^9.0.0",
52-
"tape": "^5.0.0",
53-
"tsd": "^0.28.0",
5452
"type-coverage": "^2.0.0",
5553
"typescript": "^5.0.0",
5654
"unist-builder": "^4.0.0",
@@ -84,17 +82,6 @@
8482
"strict": true
8583
},
8684
"xo": {
87-
"overrides": [
88-
{
89-
"files": [
90-
"**/*.ts"
91-
],
92-
"rules": {
93-
"@typescript-eslint/ban-types": "off",
94-
"@typescript-eslint/consistent-type-assertions": "off"
95-
}
96-
}
97-
],
9885
"prettier": true
9986
}
10087
}

readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ Remove empty paragraphs in `tree`.
106106

107107
###### Returns
108108

109-
Changed `tree` ([`Node`][node]), or `null` if it was an empty paragraph.
109+
Nothing (`undefined`).
110110

111111
## Types
112112

test.js

+12-5
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ test('squeezeParagraphs', async function (t) {
2020
])
2121
])
2222

23+
squeezeParagraphs(tree)
24+
2325
assert.deepEqual(
24-
squeezeParagraphs(tree),
26+
tree,
2527
u('root', [
2628
u('paragraph', [u('text', 'first')]),
2729
u('paragraph', [
@@ -37,9 +39,14 @@ test('squeezeParagraphs', async function (t) {
3739
)
3840
})
3941

40-
await t.test('should return `null` for empty paragraphs', async function () {
41-
const tree = u('paragraph', [])
42+
await t.test(
43+
'should do nothing with a (empty or not) paragraph',
44+
async function () {
45+
const tree = u('paragraph', [])
4246

43-
assert.deepEqual(squeezeParagraphs(tree), null)
44-
})
47+
squeezeParagraphs(tree)
48+
49+
assert.deepEqual(tree, u('paragraph', []))
50+
}
51+
)
4552
})

0 commit comments

Comments
 (0)