@@ -63,7 +63,7 @@ export const findAndReplace =
6363 * @param {Options } [options]
6464 */
6565 function ( tree , find , replace , options ) {
66- /** @type {Options } */
66+ /** @type {Options|undefined } */
6767 let settings
6868 /** @type {FindAndReplaceSchema|FindAndReplaceList } */
6969 let schema
@@ -97,7 +97,7 @@ export const findAndReplace =
9797 let index = - 1
9898 /** @type {Parent } */
9999 let parent
100- /** @type {Parent } */
100+ /** @type {Parent|undefined } */
101101 let grandparent
102102
103103 while ( ++ index < parents . length ) {
@@ -118,7 +118,9 @@ export const findAndReplace =
118118 grandparent = parent
119119 }
120120
121- return handler ( node , grandparent )
121+ if ( grandparent ) {
122+ return handler ( node , grandparent )
123+ }
122124 }
123125
124126 /**
@@ -129,28 +131,27 @@ export const findAndReplace =
129131 function handler ( node , parent ) {
130132 const find = pairs [ pairIndex ] [ 0 ]
131133 const replace = pairs [ pairIndex ] [ 1 ]
132- /** @type {Array.<PhrasingContent> } */
133- let nodes = [ ]
134134 let start = 0
135135 let index = parent . children . indexOf ( node )
136- /** @type {number } */
136+ /** @type {Array.<PhrasingContent> } */
137+ let nodes = [ ]
138+ /** @type {number|undefined } */
137139 let position
138- /** @type {RegExpMatchArray } */
139- let match
140- /** @type {Array.<PhrasingContent>|PhrasingContent|string|false|undefined|null } */
141- let value
142140
143141 find . lastIndex = 0
144142
145- match = find . exec ( node . value )
143+ let match = find . exec ( node . value )
146144
147145 while ( match ) {
148146 position = match . index
149147 // @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+ } )
151152
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
154155 }
155156
156157 if ( value !== false ) {
@@ -161,8 +162,10 @@ export const findAndReplace =
161162 } )
162163 }
163164
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 )
166169 }
167170
168171 start = position + match [ 0 ] . length
@@ -196,24 +199,26 @@ export const findAndReplace =
196199 * @returns {Pairs }
197200 */
198201function toPairs ( schema ) {
199- let index = - 1
200202 /** @type {Pairs } */
201203 const result = [ ]
202- /** @type {string } */
203- let key
204204
205205 if ( typeof schema !== 'object' ) {
206206 throw new TypeError ( 'Expected array or object as schema' )
207207 }
208208
209209 if ( Array . isArray ( schema ) ) {
210+ let index = - 1
211+
210212 while ( ++ index < schema . length ) {
211213 result . push ( [
212214 toExpression ( schema [ index ] [ 0 ] ) ,
213215 toFunction ( schema [ index ] [ 1 ] )
214216 ] )
215217 }
216218 } else {
219+ /** @type {string } */
220+ let key
221+
217222 for ( key in schema ) {
218223 if ( own . call ( schema , key ) ) {
219224 result . push ( [ toExpression ( key ) , toFunction ( schema [ key ] ) ] )
@@ -237,11 +242,5 @@ function toExpression(find) {
237242 * @returns {ReplaceFunction }
238243 */
239244function 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
247246}
0 commit comments