1
1
/**
2
- * @typedef {import('mdast').Parent } MdastParent
3
- * @typedef {import('mdast').Root } Root
4
- * @typedef {import('mdast').Content } Content
2
+ * @typedef {import('mdast').Nodes } Nodes
3
+ * @typedef {import('mdast').Parents } Parents
5
4
* @typedef {import('mdast').PhrasingContent } PhrasingContent
5
+ * @typedef {import('mdast').Root } Root
6
6
* @typedef {import('mdast').Text } Text
7
7
* @typedef {import('unist-util-visit-parents').Test } Test
8
8
* @typedef {import('unist-util-visit-parents').VisitorResult } VisitorResult
9
9
*/
10
10
11
11
/**
12
- * @typedef {Content | Root } Node
13
- * @typedef {Extract<Node, MdastParent> } Parent
14
- * @typedef {Exclude<Parent, Root> } ContentParent
15
- *
16
12
* @typedef RegExpMatchObject
17
13
* Info on the match.
18
14
* @property {number } index
19
15
* The index of the search at which the result was found.
20
16
* @property {string } input
21
17
* A copy of the search string in the text node.
22
- * @property {[Root, ...Array<ContentParent >, Text] } stack
18
+ * @property {[...Array<Parents >, Text] } stack
23
19
* All ancestors of the text node, where the last node is the text itself.
24
20
*
25
21
* @callback ReplaceFunction
@@ -90,18 +86,17 @@ const own = {}.hasOwnProperty
90
86
export const findAndReplace =
91
87
/**
92
88
* @type {(
93
- * (<Tree extends Node> (tree: Tree , find: Find, replace?: Replace | null | undefined, options?: Options | null | undefined) => Tree ) &
94
- * (<Tree extends Node> (tree: Tree , schema: FindAndReplaceSchema | FindAndReplaceList, options?: Options | null | undefined) => Tree )
89
+ * ((tree: Nodes , find: Find, replace?: Replace | null | undefined, options?: Options | null | undefined) => undefined ) &
90
+ * ((tree: Nodes , schema: FindAndReplaceSchema | FindAndReplaceList, options?: Options | null | undefined) => undefined )
95
91
* )}
96
92
**/
97
93
(
98
94
/**
99
- * @template {Node} Tree
100
- * @param {Tree } tree
95
+ * @param {Nodes } tree
101
96
* @param {Find | FindAndReplaceSchema | FindAndReplaceList } find
102
97
* @param {Replace | Options | null | undefined } [replace]
103
98
* @param {Options | null | undefined } [options]
104
- * @returns {Tree }
99
+ * @returns {undefined }
105
100
*/
106
101
function ( tree , find , replace , options ) {
107
102
/** @type {Options | null | undefined } */
@@ -131,13 +126,13 @@ export const findAndReplace =
131
126
visitParents ( tree , 'text' , visitor )
132
127
}
133
128
134
- // To do next major: don’t return the given tree .
129
+ // @ts -expect-error: To do: remove .
135
130
return tree
136
131
137
- /** @type {import('unist-util-visit-parents/complex-types.js ').BuildVisitor<Root, 'text'> } */
132
+ /** @type {import('unist-util-visit-parents').BuildVisitor<Root, 'text'> } */
138
133
function visitor ( node , parents ) {
139
134
let index = - 1
140
- /** @type {Parent | undefined } */
135
+ /** @type {Parents | undefined } */
141
136
let grandparent
142
137
143
138
while ( ++ index < parents . length ) {
@@ -167,7 +162,7 @@ export const findAndReplace =
167
162
*
168
163
* @param {Text } node
169
164
* Text node.
170
- * @param {Array<Parent > } parents
165
+ * @param {Array<Parents > } parents
171
166
* Parents.
172
167
* @returns {VisitorResult }
173
168
* Result.
@@ -193,7 +188,6 @@ export const findAndReplace =
193
188
const matchObject = {
194
189
index : match . index ,
195
190
input : match . input ,
196
- // @ts -expect-error: stack is fine.
197
191
stack : [ ...parents , node ]
198
192
}
199
193
let value = replace ( ...match , matchObject )
0 commit comments