Skip to content

Commit 19cf2fd

Browse files
committed
Refactor code-style
1 parent 83d27e1 commit 19cf2fd

File tree

3 files changed

+386
-253
lines changed

3 files changed

+386
-253
lines changed

lib/index.js

Lines changed: 59 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,40 @@
11
/**
2-
* @typedef {import('hast').Text} Text
3-
* @typedef {import('hast').Root} Root
4-
* @typedef {import('hast').Element} Element
5-
* @typedef {import('hast').Content} Content
62
* @typedef {import('hast').Nodes} Nodes
3+
* @typedef {import('hast').Parents} Parents
4+
* @typedef {import('hast').Root} Root
5+
* @typedef {import('hast').RootContent} RootContent
6+
* @typedef {import('hast').Text} Text
77
* @typedef {import('hast-util-is-element').Test} Test
88
* @typedef {import('unist-util-visit-parents').VisitorResult} VisitorResult
99
*/
1010

1111
/**
12+
* @typedef {RegExp | string} Find
13+
* Pattern to find.
14+
*
15+
* Strings are escaped and then turned into global expressions.
16+
*
17+
* @typedef {Array<FindAndReplaceTuple>} FindAndReplaceList
18+
* Several find and replaces, in array form.
19+
*
20+
* @typedef {Record<string, Replace>} FindAndReplaceSchema
21+
* Several find and replaces, in object form.
22+
*
23+
* @typedef {[Find, Replace]} FindAndReplaceTuple
24+
* Find and replace in tuple form.
25+
*
1226
* @typedef RegExpMatchObject
1327
* Info on the match.
1428
* @property {number} index
1529
* The index of the search at which the result was found.
1630
* @property {string} input
1731
* A copy of the search string in the text node.
18-
* @property {[Root, ...Array<Element>, Text]} stack
32+
* @property {[...Array<Parents>, Text]} stack
1933
* All ancestors of the text node, where the last node is the text itself.
2034
*
35+
* @typedef {ReplaceFunction | string} Replace
36+
* Thing to replace with.
37+
*
2138
* @callback ReplaceFunction
2239
* Callback called when a search matches.
2340
* @param {...any} parameters
@@ -26,27 +43,14 @@
2643
* * `value` (`string`) — whole match
2744
* * `...capture` (`Array<string>`) — matches from regex capture groups
2845
* * `match` (`RegExpMatchObject`) — info on the match
29-
* @returns {Array<Content> | Content | string | false | undefined | null}
46+
* @returns {Array<RootContent> | RootContent | string | false | null | undefined}
3047
* Thing to replace with.
3148
*
3249
* * when `null`, `undefined`, `''`, remove the match
3350
* * …or when `false`, do not replace at all
3451
* * …or when `string`, replace with a text node of that value
3552
* * …or when `Node` or `Array<Node>`, replace with those nodes
3653
*
37-
* @typedef {string | RegExp} Find
38-
* Pattern to find.
39-
*
40-
* Strings are escaped and then turned into global expressions.
41-
*
42-
* @typedef {Array<FindAndReplaceTuple>} FindAndReplaceList
43-
* Several find and replaces, in array form.
44-
* @typedef {Record<string, Replace>} FindAndReplaceSchema
45-
* Several find and replaces, in object form.
46-
* @typedef {[Find, Replace]} FindAndReplaceTuple
47-
* Find and replace in tuple form.
48-
* @typedef {string | ReplaceFunction} Replace
49-
* Thing to replace with.
5054
* @typedef {[RegExp, ReplaceFunction]} Pair
5155
* Normalized find and replace.
5256
* @typedef {Array<Pair>} Pairs
@@ -55,7 +59,7 @@
5559
* @typedef Options
5660
* Configuration.
5761
* @property {Test | null | undefined} [ignore]
58-
* Test for which elements to ignore.
62+
* Test for which elements to ignore (optional).
5963
*/
6064

6165
import {visitParents} from 'unist-util-visit-parents'
@@ -80,18 +84,29 @@ export const defaultIgnore = ['math', 'script', 'style', 'svg', 'title']
8084
* nodes.
8185
* Partial matches are not supported.
8286
*
83-
* @template {Nodes} Tree
84-
* Node type.
85-
* @param {Tree} tree
87+
* @overload
88+
* @param {Nodes} tree
89+
* @param {Find} find
90+
* @param {Replace | null | undefined} [replace]
91+
* @param {Options | null | undefined} [options]
92+
* @returns {undefined}
93+
*
94+
* @overload
95+
* @param {Nodes} tree
96+
* @param {FindAndReplaceSchema | FindAndReplaceList} schema
97+
* @param {Options | null | undefined} [options]
98+
* @returns {undefined}
99+
*
100+
* @param {Nodes} tree
86101
* Tree to change.
87-
* @param {Find | FindAndReplaceSchema | FindAndReplaceList} find
102+
* @param {Find | FindAndReplaceList | FindAndReplaceSchema} find
88103
* Patterns to find.
89-
* @param {Replace | Options | null | undefined} [replace]
104+
* @param {Options | Replace | null | undefined} [replace]
90105
* Things to replace with (when `find` is `Find`) or configuration.
91106
* @param {Options | null | undefined} [options]
92107
* Configuration (when `find` is not `Find`).
93-
* @returns {Tree}
94-
* Given, modified, tree.
108+
* @returns {undefined}
109+
* Nothing.
95110
*/
96111
export function findAndReplace(tree, find, replace, options) {
97112
/** @type {Options | null | undefined} */
@@ -121,23 +136,24 @@ export function findAndReplace(tree, find, replace, options) {
121136
visitParents(tree, 'text', visitor)
122137
}
123138

124-
// To do next major: don’t return the given tree.
139+
// @ts-expect-error: To do next major: don’t return the given tree.
125140
return tree
126141

127-
/** @type {import('unist-util-visit-parents').BuildVisitor<Nodes, 'text'>} */
142+
/** @type {import('unist-util-visit-parents').BuildVisitor<Root, 'text'>} */
128143
function visitor(node, parents) {
129144
let index = -1
130-
/** @type {Root | Element | undefined} */
145+
/** @type {Parents | undefined} */
131146
let grandparent
132147

133148
while (++index < parents.length) {
134149
const parent = parents[index]
150+
/** @type {Array<Nodes> | undefined} */
151+
const siblings = grandparent ? grandparent.children : undefined
135152

136153
if (
137154
ignored(
138155
parent,
139-
// @ts-expect-error: TS doesn’t understand but it’s perfect.
140-
grandparent ? grandparent.children.indexOf(parent) : undefined,
156+
siblings ? siblings.indexOf(parent) : undefined,
141157
grandparent
142158
)
143159
) {
@@ -157,7 +173,7 @@ export function findAndReplace(tree, find, replace, options) {
157173
*
158174
* @param {Text} node
159175
* Text node.
160-
* @param {Array<Root | Element>} parents
176+
* @param {Array<Parents>} parents
161177
* Parents.
162178
* @returns {VisitorResult}
163179
* Result.
@@ -167,9 +183,11 @@ export function findAndReplace(tree, find, replace, options) {
167183
const find = pairs[pairIndex][0]
168184
const replace = pairs[pairIndex][1]
169185
let start = 0
170-
const index = parent.children.indexOf(node)
186+
/** @type {Array<Nodes>} */
187+
const siblings = parent.children
188+
const index = siblings.indexOf(node)
171189
let change = false
172-
/** @type {Array<Content>} */
190+
/** @type {Array<RootContent>} */
173191
let nodes = []
174192

175193
find.lastIndex = 0
@@ -182,7 +200,6 @@ export function findAndReplace(tree, find, replace, options) {
182200
const matchObject = {
183201
index: match.index,
184202
input: match.input,
185-
// @ts-expect-error: stack is fine.
186203
stack: [...parents, node]
187204
}
188205
let value = replace(...match, matchObject)
@@ -231,7 +248,7 @@ export function findAndReplace(tree, find, replace, options) {
231248
/**
232249
* Turn a schema into pairs.
233250
*
234-
* @param {FindAndReplaceSchema | FindAndReplaceList} schema
251+
* @param {FindAndReplaceList | FindAndReplaceSchema} schema
235252
* Schema.
236253
* @returns {Pairs}
237254
* Clean pairs.
@@ -288,5 +305,9 @@ function toExpression(find) {
288305
* Function.
289306
*/
290307
function toFunction(replace) {
291-
return typeof replace === 'function' ? replace : () => replace
308+
return typeof replace === 'function'
309+
? replace
310+
: function () {
311+
return replace
312+
}
292313
}

readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,9 @@ browser.
311311
```js
312312
const tree = h('p', 'This and that.')
313313

314-
findAndReplace(tree, 'and', () => h('script', 'alert(1)'))
314+
findAndReplace(tree, 'and', function () {
315+
return h('script', 'alert(1)')
316+
})
315317
```
316318

317319
Yields:

0 commit comments

Comments
 (0)