@@ -293,6 +293,41 @@ function getQuantifierRepeatedElementReplacement(
293293 return { messageId : "combine" , raw }
294294}
295295
296+ /**
297+ * Whether the computed replacement is to be ignored.
298+ */
299+ function ignoreReplacement (
300+ left : Element ,
301+ right : Element ,
302+ result : Replacement ,
303+ ) : boolean {
304+ // There is a relatively common case for which we want to make
305+ // an exception: `aa?`
306+ // We will only suggest the replacement if the new raw is
307+ // shorter than the current one.
308+ if ( left . type === "Quantifier" ) {
309+ if (
310+ left . raw . length + right . raw . length <= result . raw . length &&
311+ isGroupOrCharacter ( right ) &&
312+ left . min === 0 &&
313+ left . max === 1
314+ ) {
315+ return true
316+ }
317+ }
318+ if ( right . type === "Quantifier" ) {
319+ if (
320+ left . raw . length + right . raw . length <= result . raw . length &&
321+ isGroupOrCharacter ( left ) &&
322+ right . min === 0 &&
323+ right . max === 1
324+ ) {
325+ return true
326+ }
327+ }
328+ return false
329+ }
330+
296331/**
297332 * Returns the replacement for the two adjacent elements.
298333 */
@@ -303,7 +338,7 @@ function getReplacement(
303338) : Replacement | null {
304339 if ( left . type === "Quantifier" && right . type === "Quantifier" ) {
305340 const result = getQuantifiersReplacement ( left , right , context )
306- if ( result ) return result
341+ if ( result && ! ignoreReplacement ( left , right , result ) ) return result
307342 }
308343
309344 if ( left . type === "Quantifier" ) {
@@ -313,7 +348,7 @@ function getReplacement(
313348 [ left , rightRep ] ,
314349 context ,
315350 )
316- if ( result ) return result
351+ if ( result && ! ignoreReplacement ( left , right , result ) ) return result
317352 }
318353 }
319354
@@ -324,22 +359,7 @@ function getReplacement(
324359 [ leftRep , right ] ,
325360 context ,
326361 )
327- if ( result ) {
328- // There is a relatively common case for which we want to make
329- // an exception: `aa?`
330- // We will only suggest the replacement if the new raw is
331- // shorter than the current one.
332- if (
333- isGroupOrCharacter ( left ) &&
334- right . min === 0 &&
335- right . max === 1 &&
336- left . raw . length + right . raw . length <= result . raw . length
337- ) {
338- return null
339- }
340-
341- return result
342- }
362+ if ( result && ! ignoreReplacement ( left , right , result ) ) return result
343363 }
344364 }
345365
0 commit comments