@@ -87,16 +87,20 @@ function matchingParen(s: string, open: number): number {
8787 return - 1 ;
8888}
8989
90- /** `inner` with its depth-0 text (literals, quoted identifiers and bracketed groups blanked). */
90+ /**
91+ * `inner` with everything below depth 0 blanked (literals, quoted identifiers, bracketed
92+ * groups). Same length as `inner`, so an offset found here is the same offset in `inner`.
93+ */
9194function topLevelText ( inner : string ) : string {
9295 let out = "" ;
9396 let depth = 0 ;
9497 for ( let i = 0 ; i < inner . length ; i ++ ) {
9598 const ch = inner [ i ] ! ;
9699 if ( ch === "'" || ch === '"' ) {
97- i ++ ;
100+ const start = i ++ ;
98101 while ( i < inner . length && ! ( inner [ i ] === ch && inner [ i + 1 ] !== ch ) ) i += inner [ i ] === ch ? 2 : 1 ;
99- out += " " ;
102+ i = Math . min ( i , inner . length - 1 ) ;
103+ out += " " . repeat ( i - start + 1 ) ;
100104 } else if ( ch === "(" || ch === "[" ) { depth ++ ; out += " " ; }
101105 else if ( ch === ")" || ch === "]" ) { depth -- ; out += " " ; }
102106 else out += depth === 0 ? ch : " " ;
@@ -131,8 +135,10 @@ function foldNotDistinctFrom(expr: string): string {
131135 if ( close !== - 1 ) {
132136 const inner = expr . slice ( open + 1 , close ) ;
133137 const top = topLevelText ( inner ) ;
134- if ( top . match ( / \b i s \s + d i s t i n c t \s + f r o m \b / gi) ?. length === 1 && ! / \b (?: a n d | o r | n o t ) \b / i. test ( top ) ) {
135- out += `(${ inner . replace ( / \b i s \s + d i s t i n c t \s + f r o m \b / i, "is not distinct from" ) } )` ;
138+ const operators = [ ...top . matchAll ( / \b i s \s + d i s t i n c t \s + f r o m \b / gi) ] ;
139+ if ( operators . length === 1 && ! / \b (?: a n d | o r | n o t ) \b / i. test ( top ) ) {
140+ const op = operators [ 0 ] ! ;
141+ out += `(${ inner . slice ( 0 , op . index ) } is not distinct from${ inner . slice ( op . index + op [ 0 ] . length ) } )` ;
136142 i = close + 1 ;
137143 continue ;
138144 }
0 commit comments