Skip to content

Commit c18de9c

Browse files
committed
refactor(formula): report the first kept comparison, drop the unreachable ordering-over-equality preference (#38959 item 4, L2 S1)
The checker's first fault masks an equality met before an ordering, so no source reaches the preference; the walk reports the comparison it stops at. Claude-Session: https://claude.ai/code/session_01QSeKYnrQ7UMtHofKQVjcH2 Co-authored-by: Claude <noreply@anthropic.com>
1 parent 01316bb commit c18de9c

3 files changed

Lines changed: 16 additions & 24 deletions

File tree

‎packages/formula/src/cel-engine.ts‎

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,36 +1100,27 @@ function comparesDateTextAgainstUndeclared(
11001100
};
11011101
/** An element of the list `receiver` computes: `receiver[0]`. */
11021102
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 `+`).
1109-
let found = false;
1110-
let ordering: KeptDateTextComparison | undefined;
1111-
let equality: KeptDateTextComparison | undefined;
1103+
// [#38959 item 4] The comparison that keeps the verdict is reported, so the
1104+
// operator, operands and counterpart named are its own, never the Timestamp
1105+
// typing's first fault elsewhere in the source (`'Due: ' + record.due_date
1106+
// == 'x' && record.due_date < record.title` names `<`, not `+`).
1107+
let kept: KeptDateTextComparison | null = null;
11121108
/** A side's type as the Timestamp typing spells it: a date field's text is a Timestamp there. */
11131109
const spelled = (t: string | undefined): string =>
11141110
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-
};
11201111
const visit = (v: unknown, bound: ReadonlyMap<string, CelNode>): void => {
1121-
if (ordering) return;
1112+
if (kept) return;
11221113
if (Array.isArray(v)) { for (const child of v) visit(child, bound); return; }
11231114
if (!isCelNode(v)) return;
11241115
if (DATE_TEXT_COMPARISON_OPS.has(v.op) && Array.isArray(v.args) && v.args.length === 2) {
11251116
// Without `macroElements` a macro variable proves nothing, as before #38959.
1126-
if (!macroElements && bound.size > 0 && readsBound(v.args, bound)) { keep(v, { operator: v.op }); return; }
1117+
if (!macroElements && bound.size > 0 && readsBound(v.args, bound)) { kept = { operator: v.op }; return; }
11271118
const [sa, sb] = (v.args as unknown[]).map((operand) => substitute(operand, bound));
11281119
const [a, b] = [sa.node, sb.node];
11291120
const [ta, tb] = [typeOf(a), typeOf(b)];
11301121
// A macro variable whose element does not check proves nothing: keep the verdict.
11311122
if ((sa.read && ta === undefined) || (sb.read && tb === undefined)) {
1132-
keep(v, { operator: v.op, operands: `${spelled(ta)} ${v.op} ${spelled(tb)}` });
1123+
kept = { operator: v.op, operands: `${spelled(ta)} ${v.op} ${spelled(tb)}` };
11331124
return;
11341125
}
11351126
const dateFirst = ta === DATE_TEXT_CEL_TYPE && !declared(b, tb);
@@ -1139,11 +1130,11 @@ function comparesDateTextAgainstUndeclared(
11391130
// computed text (`isoDate(today())`, `upper(…)`) or a macro's element
11401131
// is no reference to parse.
11411132
const read = otherType === 'string' ? typedFieldRef(other, scope) : null;
1142-
keep(v, {
1133+
kept = {
11431134
operator: v.op,
11441135
operands: `${spelled(ta)} ${v.op} ${spelled(tb)}`,
11451136
...(read ? { counterpart: read.written } : {}),
1146-
});
1137+
};
11471138
return;
11481139
}
11491140
}
@@ -1159,7 +1150,7 @@ function comparesDateTextAgainstUndeclared(
11591150
visit(v.args, bound);
11601151
};
11611152
visit(ast, new Map());
1162-
return found ? ordering ?? equality ?? {} : null;
1153+
return kept;
11631154
}
11641155

11651156
/**

‎packages/formula/src/expression-refusal.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,10 @@ export interface ExpressionRefusalParams {
324324
* its object). Every param is that comparison's own, wherever else the
325325
* source faults the Timestamp typing first (`'Due: ' + record.due_date ==
326326
* 'x' && record.due_date < record.title` names `<`): `operator` is its
327-
* operator — an ordering wherever the source holds one, else the `==` / `!=`
328-
* the #32595 walk also keeps; `operands` are its operand types as the
327+
* operator — an ordering, or the `==` / `!=` the #32595 walk keeps where a
328+
* date field is equated with such a counterpart beside a fault the Timestamp
329+
* typing meets (`'Due: ' + record.due_date == 'x' && record.due_date ==
330+
* record.title`); `operands` are its operand types as the
329331
* Timestamp typing spells them (`google.protobuf.Timestamp > string`); and
330332
* `reference` is its text counterpart as written, present only where that
331333
* counterpart is a text field read (`record.title`, an index read by its

‎packages/formula/src/validate-date-text-arithmetic.test.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,8 +1192,7 @@ describe('a date ordered against a text field is served under its own code, with
11921192
expect(b.params).toEqual({ operands: 'google.protobuf.Timestamp > string', operator: '>' });
11931193
expect(b.message).toContain('`>` orders a date field against a text value.');
11941194
expect(b.message).not.toContain('record.title');
1195-
expect(run(concat), concat).toEqual({ ok: true, value: expect.any(Boolean) });
1196-
expect(run(computed), computed).toEqual({ ok: true, value: expect.any(Boolean) });
1195+
for (const s of [concat, computed]) expect(run(s), s).toEqual({ ok: true, value: expect.any(Boolean) });
11971196
});
11981197

11991198
it.each(SITES)('at %s a date-text fault in the same source is the finding, never the ordering sentence', (_site, hint) => {

0 commit comments

Comments
 (0)