File tree 4 files changed +35
-11
lines changed 4 files changed +35
-11
lines changed Original file line number Diff line number Diff line change
1
+ coverage /
2
+ node_modules /
1
3
.DS_Store
2
4
* .d.ts
3
5
* .log
4
- coverage /
5
- node_modules /
6
6
yarn.lock
Original file line number Diff line number Diff line change 1
1
/**
2
2
* @typedef {import('mdast').Root|import('mdast').Content } Node
3
+ * @typedef {import('mdast').Paragraph } Paragraph
3
4
*/
4
5
5
6
import { remove } from 'unist-util-remove'
6
7
7
8
/**
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 }
11
12
*/
12
13
export function squeezeParagraphs ( tree ) {
13
- return remove ( tree , { cascade : false } , ( node ) =>
14
+ /**
15
+ * @param {Node } node
16
+ * @returns {boolean }
17
+ */
18
+ const filter = ( node ) =>
14
19
Boolean (
15
20
node . type === 'paragraph' &&
16
21
node . children . every (
17
- ( /** @type {Node } */ node ) =>
18
- node . type === 'text' && / ^ \s * $ / . test ( node . value )
22
+ ( node ) => node . type === 'text' && / ^ \s * $ / . test ( node . value )
19
23
)
20
24
)
21
- )
25
+
26
+ // @ts -expect-error: `remove` can’t narrow the above test.
27
+ return remove ( tree , { cascade : false } , filter )
22
28
}
Original file line number Diff line number Diff line change
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 ) )
Original file line number Diff line number Diff line change 46
46
"@types/tape" : " ^4.0.0" ,
47
47
"c8" : " ^7.0.0" ,
48
48
"prettier" : " ^2.0.0" ,
49
- "remark-cli" : " ^9 .0.0" ,
49
+ "remark-cli" : " ^10 .0.0" ,
50
50
"remark-preset-wooorm" : " ^8.0.0" ,
51
51
"rimraf" : " ^3.0.0" ,
52
52
"tape" : " ^5.0.0" ,
53
+ "tsd" : " ^0.17.0" ,
53
54
"type-coverage" : " ^2.0.0" ,
54
55
"typescript" : " ^4.0.0" ,
55
56
"unist-builder" : " ^3.0.0" ,
56
57
"xo" : " ^0.42.0"
57
58
},
58
59
"scripts" : {
59
60
"prepack" : " npm run build && npm run format" ,
60
- "build" : " rimraf \" *.d.ts\" && tsc && type-coverage" ,
61
+ "build" : " rimraf \" *.d.ts\" && tsc && tsd && type-coverage" ,
61
62
"format" : " remark . -qfo && prettier . -w --loglevel warn && xo --fix" ,
62
63
"test-api" : " node test.js" ,
63
64
"test-coverage" : " c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js" ,
You can’t perform that action at this time.
0 commit comments