Skip to content

Commit 78f7160

Browse files
fix(core): a date macro inside a longer string is a literal on the client too (#36068)
<!-- claude-projects-attribution: {"github_login":"os-tesla"} --> _Requested by **Tesla** · [project thread](https://claude.ai/code/project/chan_01AXsQkLFKRdQCqQ3RTr6Jao?thread=cmsg_01AXsQkLFKRdQCqQ3RTr6JaoH1SLb7yGpZj1NnaFvhbT6W)_ Fixes #35754 Clause-②: no Metadata-protocol: none ## 维护者速读 - 控制台里筛选值若只是「包含」日期宏的一段文字(例如 from {today} on),改前浏览器会把其中的宏换成日期,改后原样保留,与服务器的读法一致;整值为一个宏的写法照常解析。 - 属于筛选占位符契约一致性的收口:同一个筛选在控制台和服务器上应命中同一批记录。 - 只有把日期宏夹在长文本里当筛选值的作者会看到差异(以前也只在控制台生效、服务器不认),影响面小,可直接撤回本改动恢复旧行为。 - 无。 ## What changes The spec reads a filter value as a placeholder only when the value is ENTIRELY one wrapped token (`FILTER_TOKEN_WRAPPED_RE` in `@objectstack/spec/data`: "a value that merely CONTAINS braces (`'acme {x} deal'`) is untouched, as are `{a}{b}` and `{{x}}`"), and the server resolver `resolveFilterTokens` in `@objectstack/core` applies that reading. The client resolver did not: after its whole-value match, `resolveDateMacros` in `@object-ui/core` ran a second, global replace over the string, so `'from {today} on'` became `'from 2026-05-25 on'` in the console and stayed literal on the server. The card's question (which side is the contract) is answered by the spec text above, so the client aligns with it. - `resolveDateMacros` (`ui/packages/core/src/utils/date-macros.ts`): the global pass is removed. A string resolves only when it is one whole `{token}` / `${token}`; anything else is returned as written. The header says so and names the spec reading. - `substituteTokens` in `ui/packages/core/src/utils/compare-to.ts` (the previous-period token rename behind `shiftFilterByCompareTo`) had the same embedded pass. With the resolver fixed alone, `'from {today} on'` would have been renamed to `'from {yesterday} on'` and then kept literal, so the comparison query would have filtered on a different literal from the current one. It now renames only whole-value tokens, through the file's existing `wholeToken` helper. Same defect class, mechanical, no other claim on the file, same test family: fixed here under the bounded in-place rule and declared in the report. **Caller census** (every non-test caller of `resolveDateMacros` at base `8145424b`; none relies on the embedded form): | Caller | What it passes | Effect | |:---|:---|:---| | `resolveFilterPlaceholders` (core `filter-tokens.ts`), and through it every list, grid, chart, dashboard and detail surface | filter trees | the fix itself | | `shiftFilterByCompareTo` / `lastPeriodBefore` (core `compare-to.ts`) | filter trees; `lastPeriodBefore` builds a whole `{last_UNIT_start}` | aligned (see above); `lastPeriodBefore` unchanged | | `isUsableDateString` (core `dashboard-filters.ts`) | a dashboard date-filter value | an embedded macro no longer counts as a usable date; it never reached the server as one | | `resolveInitialDateMacro` (plugin-calendar `initialDate.ts`) | `calendar.initialDate`, which the spec defines as a wrapped token | same outcome: an embedded string already resolved to a non-date and fell back to today | | plugin-dashboard `utils.ts` | re-export only | none | The card's "page/URL params" hypothesis does not hold: `resolveParam` inside `resolveDateMacros` is the parameterised date-macro grammar (`{N_days_ago}` and friends), which the server also resolves (`parseDateMacroParam`), not page or URL parameters. No dashboard title or URL template calls this resolver. ## Tests - New pin `ui/packages/core/src/utils/__tests__/date-macros.embeddedLiteral-35754.test.ts`: an embedded fixed macro, an embedded parameterised macro, an embedded macro beside a whole one in a filter tree (through `resolveFilterPlaceholders`), a previous-period comparison, and `{{today}}` all stay literal. CONTROLS: a whole `{today}`, `${today}` and `{7_days_ago}` still resolve; `'acme {x} deal'` and `{a}{b}` are untouched. - **Pin flip (deviation):** `date-macros.test.ts` pinned the embedded behaviour in two assertions: "leaves unknown tokens untouched and ignores non-strings" expected `'hello {today} world'` to become `'hello 2026-05-25 world'`, and "accepts both {token} and ${token} forms" expected `'range: ${last_week_start}..${today}'` to become `'range: 2026-05-18..2026-05-25'`. Both now expect the value as written; the whole-value `'${today}'` assertion beside them is unchanged. All runs on head `372f6c1f`. <details><summary>@object-ui/core suite, type-check, consumers</summary> ```text pnpm --dir ui exec vitest run --maxWorkers=2 packages/core/ Test Files 245 passed (245) Tests 4744 passed | 27 skipped (4771) os-verify-lock: VERDICT command-exit 0 pnpm --filter @object-ui/core run type-check os-verify-lock: VERDICT command-exit 0 pnpm --dir ui exec vitest run --maxWorkers=2 (new pin + plugin-dashboard DatasetWidget.compareTo.test.tsx + plugin-calendar ObjectCalendar.initialDate-28102.test.tsx) Test Files 3 passed (3) Tests 57 passed (57) ``` </details> <details><summary>Ablation, one leg per fixed file (base file restored, pin run, HEAD restored and hash-verified)</summary> ```text == ablate compare-to.ts : globalReplaceMarkers=1 wholeOnlyMarkers=0 x does not rename an embedded macro for a previous-period comparison Tests 1 failed | 6 passed (7) restored: hash match == ablate date-macros.ts : globalReplaceMarkers=1 wholeOnlyMarkers=0 x leaves a fixed macro inside a longer string as written x leaves a parameterised macro inside a longer string as written x leaves an embedded macro literal inside a filter tree, beside a whole one that resolves x does not rename an embedded macro for a previous-period comparison x leaves a macro wrapped in a second pair of braces as written Tests 5 failed | 2 passed (7) restored: hash match diff HEAD empty: yes green re-run: Tests 7 passed (7) ``` Both CONTROL cases stay green under both ablations, as controls should. </details> ## Acceptance notes - Not filed: the hotcrm reconciliation harness keeps its own date-macro resolver (`scripts/analytics-reconcile/macros.ts`); read at hotcrm `e7fe77f`, it has no global replace pass, so nothing there disagrees. - The client whole-value grammar stays the token-NAME grammar (`[a-zA-Z0-9_]+`), narrower than the server's recognition grammar. That is a separate question (what an unknown wrapped value does on the client) and is not touched here. ## Verification <details><summary>Derived gates (dispatch-gates --commands, head 372f6c1)</summary> ```text dispatch-gates --ran: 38 derived, 38 run, 0 NOT-MEASURED, 0 UNRUN (a DERIVED zero, every row carries its exit code) all exit 0 except: pnpm --dir ui exec vitest run scripts/ :: exit 1 1 failure, scripts/__tests__/unit-registry-absence-collision.test.ts: 39 app-shell modules "Failed to resolve entry for package @objectstack/client" (that package was unbuilt in this worktree). After building @objectstack/client: that file 15/15 passed, VERDICT command-exit 0. ``` </details> Read first: 1. `ui/packages/core/src/utils/date-macros.ts`, the `walk` string branch inside `resolveDateMacros`: the whole cut. 2. `ui/packages/core/src/utils/compare-to.ts`, `substituteTokens`: the comparison-query twin. 3. `ui/packages/core/src/utils/__tests__/date-macros.test.ts`: the two flipped assertions. Two-minute check: `pnpm --dir ui exec vitest run packages/core/src/utils/__tests__/date-macros.embeddedLiteral-35754.test.ts` from the repo root shows 7 passed. Seat: `repo:objectstack#7 · os-tesla · session_01L9qibxWPiQYXSf1jxGbN3W` --- _Generated by [Claude Code](https://claude.ai/code/session_01L9qibxWPiQYXSf1jxGbN3W)_ --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 1647fb3 commit 78f7160

5 files changed

Lines changed: 106 additions & 21 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'@object-ui/core': patch
3+
---
4+
5+
fix(core): a date macro inside a longer string is a literal on the client too
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
`resolveDateMacros` resolves a filter value only when the whole value is one placeholder (`'{today}'`, `'${today}'`, `'{7_days_ago}'`). A value that merely contains one, such as `'from {today} on'`, is now returned as written. Before, the client replaced the macro inside the string while the server's resolver left it literal, so the same filter matched different rows in the console and on the server. This is the reading the spec declares for filter placeholders: a value that merely contains braces is untouched.
11+
12+
A previous-period comparison (`shiftFilterByCompareTo`) follows the same reading: it renames only whole-value tokens, so the comparison query filters on the same literal as the current one.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* ObjectUI
3+
* Copyright (c) 2024-present ObjectStack Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
/**
10+
* A date macro inside a longer string is a literal, on the client as on the
11+
* server (objectstack#35754).
12+
*
13+
* The spec reads a filter value as a placeholder only when it is ENTIRELY one
14+
* wrapped token (`FILTER_TOKEN_WRAPPED_RE` in `@objectstack/spec/data`): "a
15+
* value that merely CONTAINS braces (`'acme {x} deal'`) is untouched". The
16+
* server's `resolveFilterTokens` applies that reading. The client resolver
17+
* used to run a second, global pass that substituted a macro anywhere in the
18+
* string, so `'from {today} on'` matched one row set in the console and
19+
* another on the server.
20+
*/
21+
22+
import { describe, it, expect } from 'vitest';
23+
import { resolveDateMacros } from '../date-macros';
24+
import { resolveFilterPlaceholders } from '../filter-tokens';
25+
import { shiftFilterByCompareTo } from '../compare-to';
26+
27+
// Monday 2026-05-25, local time.
28+
const now = new Date(2026, 4, 25, 12, 30, 0);
29+
30+
describe('an embedded date macro is a literal (objectstack#35754)', () => {
31+
it('leaves a fixed macro inside a longer string as written', () => {
32+
expect(resolveDateMacros('from {today} on', now)).toBe('from {today} on');
33+
expect(resolveDateMacros('since ${current_month_start}', now)).toBe('since ${current_month_start}');
34+
});
35+
36+
it('leaves a parameterised macro inside a longer string as written', () => {
37+
expect(resolveDateMacros('about {7_days_ago}', now)).toBe('about {7_days_ago}');
38+
});
39+
40+
it('leaves an embedded macro literal inside a filter tree, beside a whole one that resolves', () => {
41+
expect(
42+
resolveFilterPlaceholders({ note: 'due {tomorrow}', due: { $gte: '{today}' } }, {}, now),
43+
).toEqual({ note: 'due {tomorrow}', due: { $gte: '2026-05-25' } });
44+
});
45+
46+
it('does not rename an embedded macro for a previous-period comparison', () => {
47+
const filter = { note: 'from {today} on', at: { $gte: '{today}' } };
48+
expect(shiftFilterByCompareTo(filter, { kind: 'previousPeriod' }, now)).toEqual({
49+
note: 'from {today} on',
50+
at: { $gte: '2026-05-24' },
51+
});
52+
});
53+
54+
it('CONTROL: a whole-value macro still resolves, in both spellings', () => {
55+
expect(resolveDateMacros('{today}', now)).toBe('2026-05-25');
56+
expect(resolveDateMacros('${today}', now)).toBe('2026-05-25');
57+
expect(resolveDateMacros('{7_days_ago}', now)).toBe('2026-05-18');
58+
});
59+
60+
it('leaves a macro wrapped in a second pair of braces as written', () => {
61+
// `{{x}}` is not one wrapped token (spec), so the `{today}` inside is text.
62+
expect(resolveDateMacros('{{today}}', now)).toBe('{{today}}');
63+
});
64+
65+
it('CONTROL: a value with braces that is not one wrapped token is untouched', () => {
66+
expect(resolveDateMacros('acme {x} deal', now)).toBe('acme {x} deal');
67+
expect(resolveDateMacros('{a}{b}', now)).toBe('{a}{b}');
68+
});
69+
});

‎ui/packages/core/src/utils/__tests__/date-macros.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,16 @@ describe('resolveDateMacros', () => {
8686

8787
it('leaves unknown tokens untouched and ignores non-strings', () => {
8888
expect(resolveDateMacros('{not_a_macro}', now)).toBe('{not_a_macro}');
89-
expect(resolveDateMacros('hello {today} world', now)).toBe('hello 2026-05-25 world');
89+
// A macro inside a longer string is a literal, as on the server.
90+
expect(resolveDateMacros('hello {today} world', now)).toBe('hello {today} world');
9091
expect(resolveDateMacros(42 as any, now)).toBe(42);
9192
expect(resolveDateMacros(null as any, now)).toBe(null);
9293
});
9394

9495
it('accepts both {token} and ${token} forms', () => {
9596
expect(resolveDateMacros('${today}', now)).toBe('2026-05-25');
9697
expect(resolveDateMacros('range: ${last_week_start}..${today}', now)).toBe(
97-
'range: 2026-05-18..2026-05-25',
98+
'range: ${last_week_start}..${today}',
9899
);
99100
});
100101

‎ui/packages/core/src/utils/compare-to.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,17 @@ function hasRollingWindow(value: unknown): boolean {
213213
}
214214

215215
/**
216-
* Replace every `{token}` / `${token}` in `value` with what `replace` returns
217-
* for it, in ONE pass, so no replacement is replaced again.
216+
* Replace every string in `value` that is one whole `{token}` / `${token}`
217+
* with what `replace` returns for it, in ONE pass, so no replacement is
218+
* replaced again. A string that merely contains a token is a literal
219+
* (`resolveDateMacros` leaves it as written), so it is not renamed either:
220+
* the comparison query filters on the same literal as the current one.
218221
*/
219222
function substituteTokens<T>(value: T, replace: (token: string) => string | undefined): T {
220223
if (value == null) return value;
221224
if (typeof value === 'string') {
222-
return value.replace(/\$?\{([a-zA-Z0-9_]+)\}/g, (m, tok) => replace(tok) ?? m) as any;
225+
const tok = wholeToken(value);
226+
return (tok === undefined ? value : replace(tok) ?? value) as any;
223227
}
224228
if (Array.isArray(value)) return value.map((v) => substituteTokens(v, replace)) as any;
225229
if (typeof value === 'object') {

‎ui/packages/core/src/utils/date-macros.ts‎

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ import { getDisplayTimeZone } from './date-display.js';
4949
* Walks any plain-object / array structure recursively. Non-string values
5050
* (and unknown tokens) are passed through untouched.
5151
*
52+
* Only a value that is ENTIRELY one placeholder resolves. A value that merely
53+
* contains one (`'from {today} on'`, `'range: {a}..{b}'`) is a literal and is
54+
* returned as written: that is the reading the spec declares
55+
* (`FILTER_TOKEN_WRAPPED_RE` in `@objectstack/spec/data`) and the server's
56+
* resolver applies, so a filter matches the same rows whichever side
57+
* resolves it.
58+
*
5259
* ## Which clock decides "today"
5360
*
5461
* The calendar tokens (`today`, the period bounds, `N_days` / `N_weeks` /
@@ -183,26 +190,18 @@ export function resolveDateMacros<T = any>(filter: T, now: Date = new Date()): T
183190
return null;
184191
};
185192

186-
const tokenRe = /\$?\{([a-zA-Z0-9_]+)\}/g;
187-
188193
const walk = (value: any): any => {
189194
if (value == null) return value;
190195
if (typeof value === 'string') {
196+
// Only a WHOLE-string placeholder is a macro, as on the server
197+
// (`resolveFilterTokens` in `@objectstack/core`): a value that merely
198+
// contains one (`'from {today} on'`) is a literal and is left as
199+
// written, so one filter selects one row set on either side of the wire.
191200
const wholeMatch = value.match(/^\$?\{([a-zA-Z0-9_]+)\}$/);
192-
if (wholeMatch) {
193-
const tok = wholeMatch[1];
194-
if (macros[tok]) return macros[tok]();
195-
const param = resolveParam(tok);
196-
if (param != null) return param;
197-
}
198-
let touched = false;
199-
const replaced = value.replace(tokenRe, (m, tok) => {
200-
if (macros[tok]) { touched = true; return macros[tok](); }
201-
const param = resolveParam(tok);
202-
if (param != null) { touched = true; return param; }
203-
return m;
204-
});
205-
return touched ? replaced : value;
201+
if (!wholeMatch) return value;
202+
const tok = wholeMatch[1];
203+
if (macros[tok]) return macros[tok]();
204+
return resolveParam(tok) ?? value;
206205
}
207206
if (Array.isArray(value)) return value.map(walk);
208207
// Only a PLAIN object (prototype `Object.prototype` or `null`) is walked;

0 commit comments

Comments
 (0)