@@ -63,7 +63,7 @@ export const findAndReplace =
63
63
* @param {Options } [options]
64
64
*/
65
65
function ( tree , find , replace , options ) {
66
- /** @type {Options } */
66
+ /** @type {Options|undefined } */
67
67
let settings
68
68
/** @type {FindAndReplaceSchema|FindAndReplaceList } */
69
69
let schema
@@ -97,7 +97,7 @@ export const findAndReplace =
97
97
let index = - 1
98
98
/** @type {Parent } */
99
99
let parent
100
- /** @type {Parent } */
100
+ /** @type {Parent|undefined } */
101
101
let grandparent
102
102
103
103
while ( ++ index < parents . length ) {
@@ -118,7 +118,9 @@ export const findAndReplace =
118
118
grandparent = parent
119
119
}
120
120
121
- return handler ( node , grandparent )
121
+ if ( grandparent ) {
122
+ return handler ( node , grandparent )
123
+ }
122
124
}
123
125
124
126
/**
@@ -129,28 +131,27 @@ export const findAndReplace =
129
131
function handler ( node , parent ) {
130
132
const find = pairs [ pairIndex ] [ 0 ]
131
133
const replace = pairs [ pairIndex ] [ 1 ]
132
- /** @type {Array.<PhrasingContent> } */
133
- let nodes = [ ]
134
134
let start = 0
135
135
let index = parent . children . indexOf ( node )
136
- /** @type {number } */
136
+ /** @type {Array.<PhrasingContent> } */
137
+ let nodes = [ ]
138
+ /** @type {number|undefined } */
137
139
let position
138
- /** @type {RegExpMatchArray } */
139
- let match
140
- /** @type {Array.<PhrasingContent>|PhrasingContent|string|false|undefined|null } */
141
- let value
142
140
143
141
find . lastIndex = 0
144
142
145
- match = find . exec ( node . value )
143
+ let match = find . exec ( node . value )
146
144
147
145
while ( match ) {
148
146
position = match . index
149
147
// @ts -expect-error this is perfectly fine, typescript.
150
- value = replace ( ...match , { index : match . index , input : match . input } )
148
+ let value = replace ( ...match , {
149
+ index : match . index ,
150
+ input : match . input
151
+ } )
151
152
152
- if ( typeof value === 'string' && value . length > 0 ) {
153
- value = { type : 'text' , value}
153
+ if ( typeof value === 'string' ) {
154
+ value = value . length > 0 ? { type : 'text' , value} : undefined
154
155
}
155
156
156
157
if ( value !== false ) {
@@ -161,8 +162,10 @@ export const findAndReplace =
161
162
} )
162
163
}
163
164
164
- if ( value ) {
165
- nodes = [ ] . concat ( nodes , value )
165
+ if ( Array . isArray ( value ) ) {
166
+ nodes . push ( ...value )
167
+ } else if ( value ) {
168
+ nodes . push ( value )
166
169
}
167
170
168
171
start = position + match [ 0 ] . length
@@ -196,24 +199,26 @@ export const findAndReplace =
196
199
* @returns {Pairs }
197
200
*/
198
201
function toPairs ( schema ) {
199
- let index = - 1
200
202
/** @type {Pairs } */
201
203
const result = [ ]
202
- /** @type {string } */
203
- let key
204
204
205
205
if ( typeof schema !== 'object' ) {
206
206
throw new TypeError ( 'Expected array or object as schema' )
207
207
}
208
208
209
209
if ( Array . isArray ( schema ) ) {
210
+ let index = - 1
211
+
210
212
while ( ++ index < schema . length ) {
211
213
result . push ( [
212
214
toExpression ( schema [ index ] [ 0 ] ) ,
213
215
toFunction ( schema [ index ] [ 1 ] )
214
216
] )
215
217
}
216
218
} else {
219
+ /** @type {string } */
220
+ let key
221
+
217
222
for ( key in schema ) {
218
223
if ( own . call ( schema , key ) ) {
219
224
result . push ( [ toExpression ( key ) , toFunction ( schema [ key ] ) ] )
@@ -237,11 +242,5 @@ function toExpression(find) {
237
242
* @returns {ReplaceFunction }
238
243
*/
239
244
function toFunction ( replace ) {
240
- return typeof replace === 'function' ? replace : returner
241
-
242
- /** @type {ReplaceFunction } */
243
- function returner ( ) {
244
- // @ts -expect-error it’s a string.
245
- return replace
246
- }
245
+ return typeof replace === 'function' ? replace : ( ) => replace
247
246
}
0 commit comments