@@ -1022,12 +1022,17 @@ const DATE_TEXT_COMPARISON_OPS: ReadonlySet<string> = new Set([...ORDERING_OPS,
10221022 * gate asks for it, so its answer is the one it gave before (see
10231023 * {@link firstTypeMismatch}'s `options`).
10241024 */
1025- function comparesDateTextAgainstUndeclared ( source : string , env : Environment , macroElements = true ) : boolean {
1025+ function comparesDateTextAgainstUndeclared (
1026+ source : string ,
1027+ env : Environment ,
1028+ macroElements = true ,
1029+ scope : 'record' | 'flattened' = 'record' ,
1030+ ) : KeptDateTextComparison | null {
10261031 let ast : unknown ;
10271032 try {
10281033 ast = env . parse ( rewriteNullableTernary ( rewriteIndexedHas ( source ) ) ) . ast ;
10291034 } catch {
1030- return true ;
1035+ return { } ;
10311036 }
10321037 /** The CEL type `node` checks to on its own, or `undefined` when it does not check. */
10331038 const typeOf = ( node : unknown ) : string | undefined => {
@@ -1095,21 +1100,50 @@ function comparesDateTextAgainstUndeclared(source: string, env: Environment, mac
10951100 } ;
10961101 /** An element of the list `receiver` computes: `receiver[0]`. */
10971102 const elementOf = ( receiver : unknown ) : CelNode => ( { op : '[]' , args : [ receiver , { op : 'value' , args : 0n } ] } ) ;
1103+ // [#38959 item 4] The whole source is walked, not stopped at the first
1104+ // comparison that keeps the verdict: the one reported is the first ORDERING
1105+ // (`<` `<=` `>` `>=`), then the first equality, so the operator, operands and
1106+ // counterpart named are the comparison's own, never the Timestamp typing's
1107+ // first fault elsewhere in the source (`'Due: ' + record.due_date == 'x' &&
1108+ // record.due_date < record.title` names `<`, not `+`).
10981109 let found = false ;
1110+ let ordering : KeptDateTextComparison | undefined ;
1111+ let equality : KeptDateTextComparison | undefined ;
1112+ /** A side's type as the Timestamp typing spells it: a date field's text is a Timestamp there. */
1113+ const spelled = ( t : string | undefined ) : string =>
1114+ t === DATE_TEXT_CEL_TYPE ? 'google.protobuf.Timestamp' : t ?? 'dyn' ;
1115+ const keep = ( v : CelNode , kept : KeptDateTextComparison ) : void => {
1116+ found = true ;
1117+ if ( ORDERING_OPS . has ( v . op ) ) ordering ??= kept ;
1118+ else equality ??= kept ;
1119+ } ;
10991120 const visit = ( v : unknown , bound : ReadonlyMap < string , CelNode > ) : void => {
1100- if ( found ) return ;
1121+ if ( ordering ) return ;
11011122 if ( Array . isArray ( v ) ) { for ( const child of v ) visit ( child , bound ) ; return ; }
11021123 if ( ! isCelNode ( v ) ) return ;
11031124 if ( DATE_TEXT_COMPARISON_OPS . has ( v . op ) && Array . isArray ( v . args ) && v . args . length === 2 ) {
11041125 // Without `macroElements` a macro variable proves nothing, as before #38959.
1105- if ( ! macroElements && bound . size > 0 && readsBound ( v . args , bound ) ) { found = true ; return ; }
1126+ if ( ! macroElements && bound . size > 0 && readsBound ( v . args , bound ) ) { keep ( v , { operator : v . op } ) ; return ; }
11061127 const [ sa , sb ] = ( v . args as unknown [ ] ) . map ( ( operand ) => substitute ( operand , bound ) ) ;
11071128 const [ a , b ] = [ sa . node , sb . node ] ;
11081129 const [ ta , tb ] = [ typeOf ( a ) , typeOf ( b ) ] ;
11091130 // A macro variable whose element does not check proves nothing: keep the verdict.
1110- if ( ( sa . read && ta === undefined ) || ( sb . read && tb === undefined ) ) { found = true ; return ; }
1111- if ( ( ta === DATE_TEXT_CEL_TYPE && ! declared ( b , tb ) ) || ( tb === DATE_TEXT_CEL_TYPE && ! declared ( a , ta ) ) ) {
1112- found = true ;
1131+ if ( ( sa . read && ta === undefined ) || ( sb . read && tb === undefined ) ) {
1132+ keep ( v , { operator : v . op , operands : `${ spelled ( ta ) } ${ v . op } ${ spelled ( tb ) } ` } ) ;
1133+ return ;
1134+ }
1135+ const dateFirst = ta === DATE_TEXT_CEL_TYPE && ! declared ( b , tb ) ;
1136+ if ( dateFirst || ( tb === DATE_TEXT_CEL_TYPE && ! declared ( a , ta ) ) ) {
1137+ const [ other , otherType ] = dateFirst ? [ b , tb ] : [ a , ta ] ;
1138+ // The counterpart is named only where it is a text field read; a
1139+ // computed text (`isoDate(today())`, `upper(…)`) or a macro's element
1140+ // is no reference to parse.
1141+ const read = otherType === 'string' ? typedFieldRef ( other , scope ) : null ;
1142+ keep ( v , {
1143+ operator : v . op ,
1144+ operands : `${ spelled ( ta ) } ${ v . op } ${ spelled ( tb ) } ` ,
1145+ ...( read ? { counterpart : read . written } : { } ) ,
1146+ } ) ;
11131147 return ;
11141148 }
11151149 }
@@ -1125,7 +1159,21 @@ function comparesDateTextAgainstUndeclared(source: string, env: Environment, mac
11251159 visit ( v . args , bound ) ;
11261160 } ;
11271161 visit ( ast , new Map ( ) ) ;
1128- return found ;
1162+ return found ? ordering ?? equality ?? { } : null ;
1163+ }
1164+
1165+ /**
1166+ * [#38959 item 4] The comparison {@link comparesDateTextAgainstUndeclared}
1167+ * found keeping the #32595 verdict, as written: its `operator`, its
1168+ * `operands` spelled as the Timestamp typing spells them, and `counterpart`,
1169+ * the text field read it orders the date against (`record.title`, `title`),
1170+ * present only where the counterpart is one. Every member is absent where no
1171+ * comparison is reported (an unparseable source keeps the verdict unnamed).
1172+ */
1173+ interface KeptDateTextComparison {
1174+ readonly operator ?: string ;
1175+ readonly operands ?: string ;
1176+ readonly counterpart ?: string ;
11291177}
11301178
11311179/**
@@ -1440,6 +1488,16 @@ export function firstTypeMismatch(
14401488 * `type-mismatch*` sentence that says the expression faults.
14411489 */
14421490 dateTextOrdering ?: true ;
1491+ /**
1492+ * [#38959 item 4] With `dateTextOrdering`, the comparison that keeps the
1493+ * verdict, as written — its operator, its operands and its text counterpart
1494+ * ({@link KeptDateTextComparison}). `operator`, `operands` and `field` above
1495+ * are the Timestamp typing's first fault, which a mixed source need not
1496+ * place on that comparison (`'Due: ' + record.due_date == 'x' &&
1497+ * record.due_date < record.title` faults first on `+`), and `field` keeps the
1498+ * grade it gives (#30025), so the sentence reads this instead.
1499+ */
1500+ ordering ?: KeptDateTextComparison ;
14431501} | null {
14441502 if ( typeof source !== 'string' || ! source . trim ( ) ) return null ;
14451503 // An all-`dyn` record can never fault an overload — skip the parse entirely.
@@ -1490,17 +1548,22 @@ export function firstTypeMismatch(
14901548 * the mismatch kept is the ordering, not a fault the run meets.
14911549 */
14921550 let keptOrdering = false ;
1551+ let kept : KeptDateTextComparison | null = null ;
14931552 const confirmAgainstDateText = (
14941553 m : RegExpExecArray ,
14951554 recordRoots ?: Readonly < Record < string , FieldCelType > > ,
14961555 keepUndeclared = true ,
14971556 ) : RegExpExecArray | null => {
14981557 keptOrdering = false ;
1558+ kept = null ;
14991559 if ( ! DATE_CEL_TYPES . has ( m [ 1 ] ) && ! DATE_CEL_TYPES . has ( m [ 3 ] ) ) return m ;
15001560 const dateTextEnv = buildTypedEnv ( bareTypes , scope , 'date-text' , recordRoots ) ;
15011561 const r = unsoundOverload ( dateTextEnv ) ;
15021562 if ( r && textOrBool ( r ) ) return r [ 1 ] === DATE_TEXT_CEL_TYPE || r [ 3 ] === DATE_TEXT_CEL_TYPE ? m : r ;
1503- keptOrdering = keepUndeclared && comparesDateTextAgainstUndeclared ( source , dateTextEnv , options . macroElements ?? true ) ;
1563+ kept = keepUndeclared
1564+ ? comparesDateTextAgainstUndeclared ( source , dateTextEnv , options . macroElements ?? true , scope )
1565+ : null ;
1566+ keptOrdering = kept !== null ;
15041567 return keptOrdering ? m : null ;
15051568 } ;
15061569 try {
@@ -1524,7 +1587,7 @@ export function firstTypeMismatch(
15241587 return {
15251588 operator, operands, celType, category : 'type-mismatch' ,
15261589 field : offendingField ( source , bareTypes , celType , scope ) ,
1527- ...( keptOrdering ? { dateTextOrdering : true as const } : { } ) ,
1590+ ...( keptOrdering ? { dateTextOrdering : true as const , ordering : kept ?? { } } : { } ) ,
15281591 } ;
15291592 }
15301593 // [#29959] A flattened condition reads the same field as `record.<field>`
@@ -1578,7 +1641,7 @@ export function firstTypeMismatch(
15781641 operator : r [ 2 ] , operands : `${ r [ 1 ] } ${ r [ 2 ] } ${ r [ 3 ] } ` , celType, category : 'type-mismatch' ,
15791642 field : read ?. field ?? null ,
15801643 ...( read ? { root : read . root } : { } ) ,
1581- ...( keptOrdering ? { dateTextOrdering : true as const } : { } ) ,
1644+ ...( keptOrdering ? { dateTextOrdering : true as const , ordering : kept ?? { } } : { } ) ,
15821645 } ;
15831646 } catch {
15841647 // A parse/other fault is the syntax checker's job (celEngine.compile); this
0 commit comments