Skip to content

Commit b1e2811

Browse files
committed
Change types to base what is returned on tree
If a paragraph is passed, `null` is added to the potential return types. If a non-paragraph is passed, the input type is returned.
1 parent e9a4570 commit b1e2811

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
coverage/
2+
node_modules/
13
.DS_Store
24
*.d.ts
35
*.log
4-
coverage/
5-
node_modules/
66
yarn.lock

index.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
/**
22
* @typedef {import('mdast').Root|import('mdast').Content} Node
3+
* @typedef {import('mdast').Paragraph} Paragraph
34
*/
45

56
import {remove} from 'unist-util-remove'
67

78
/**
8-
* @template {Node} T
9-
* @param {T} tree
10-
* @returns {T|null}
9+
* @template {Node} Tree
10+
* @param {Tree} tree
11+
* @returns {Tree extends Paragraph ? Tree | null : Tree}
1112
*/
1213
export function squeezeParagraphs(tree) {
13-
return remove(tree, {cascade: false}, (node) =>
14+
/**
15+
* @param {Node} node
16+
* @returns {boolean}
17+
*/
18+
const filter = (node) =>
1419
Boolean(
1520
node.type === 'paragraph' &&
1621
node.children.every(
17-
(/** @type {Node} */ node) =>
18-
node.type === 'text' && /^\s*$/.test(node.value)
22+
(node) => node.type === 'text' && /^\s*$/.test(node.value)
1923
)
2024
)
21-
)
25+
26+
// @ts-expect-error: `remove` can’t narrow the above test.
27+
return remove(tree, {cascade: false}, filter)
2228
}

index.test-d.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {expectType} from 'tsd'
2+
import {Root, Heading, Paragraph} from 'mdast'
3+
import {squeezeParagraphs} from './index.js'
4+
5+
const root: Root = {type: 'root', children: []}
6+
const paragraph: Paragraph = {type: 'paragraph', children: []}
7+
const heading: Heading = {type: 'heading', depth: 1, children: []}
8+
/* eslint-disable @typescript-eslint/consistent-type-assertions */
9+
const headingOrParagraph = {type: 'paragraph', children: []} as
10+
| Heading
11+
| Paragraph
12+
/* eslint-enable @typescript-eslint/consistent-type-assertions */
13+
14+
expectType<Root>(squeezeParagraphs(root))
15+
expectType<Paragraph | null>(squeezeParagraphs(paragraph))
16+
expectType<Heading>(squeezeParagraphs(heading))
17+
expectType<Heading | Paragraph | null>(squeezeParagraphs(headingOrParagraph))

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,19 @@
4646
"@types/tape": "^4.0.0",
4747
"c8": "^7.0.0",
4848
"prettier": "^2.0.0",
49-
"remark-cli": "^9.0.0",
49+
"remark-cli": "^10.0.0",
5050
"remark-preset-wooorm": "^8.0.0",
5151
"rimraf": "^3.0.0",
5252
"tape": "^5.0.0",
53+
"tsd": "^0.17.0",
5354
"type-coverage": "^2.0.0",
5455
"typescript": "^4.0.0",
5556
"unist-builder": "^3.0.0",
5657
"xo": "^0.42.0"
5758
},
5859
"scripts": {
5960
"prepack": "npm run build && npm run format",
60-
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
61+
"build": "rimraf \"*.d.ts\" && tsc && tsd && type-coverage",
6162
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
6263
"test-api": "node test.js",
6364
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",

0 commit comments

Comments
 (0)