Skip to content
This repository was archived by the owner on Nov 4, 2026. It is now read-only.

Commit 9e7e1a5

Browse files
refactor(react,components): one visibleWhen scope builder for SchemaRenderer and page:tabs (objectui#12992) (#13001)
Fixes #12992 Clause-②: yes ## What changed - `@object-ui/react`: the module-private `configEvaluationScope` in `SchemaRenderer.tsx` is now the exported `buildNodeEvaluationScope(predicateScope, boundRecord, pageVariables)`, with a JSDoc that names its four roots. It stays in `SchemaRenderer.tsx`; the package entry already re-exports that module (`export * from './SchemaRenderer.js'`), so `index.ts` needed no line. The body of the function is unchanged; only the name, the `export`, and the JSDoc moved. `SchemaRenderer`'s memo and `evaluateConfigBagInScope` call it as before. - `@object-ui/components` (`containers.tsx`, `PageTabsRenderer`): - the derived-badge gate evaluator (objectui#12987) now calls `buildNodeEvaluationScope(predicateScope, recordData, pageVariables)` instead of spelling the scope inline; - the item-level `visibleWhen` evaluator (`isItemVisible`) now composes the builder too: the row spread flat underneath, the builder's scope, then `record` / `data` set to the row on top. Each key gets the same value as before (see "Root measurement"). After this, `containers.tsx` spells `current_user` nowhere outside comments. - Changeset: `@object-ui/react: minor` (new export), `@object-ui/components: patch`. ### Name `buildNodeEvaluationScope`, not `buildVisibleWhenScope`. The same scope evaluates every expression `SchemaRenderer` evaluates on a node (`properties`, `content`, the `visibleWhen` chain), and `evaluateConfigBagInScope` uses it for action-container members. A `visibleWhen`-only name would understate what a root added there reaches. ## Root measurement (both spellings, at base `4dfdebdf5f`) | root | `SchemaRenderer` (`configEvaluationScope`) | `page:tabs` badge gates (inline) | |---|---|---| | ambient spread | `...predicateScope` | `...predicateScope` | | `current_user` | `predicateScope?.user` | `(predicateScope as any)?.user` | | `record` | only for a non-array object row | only for a non-array object row (same test) | | `page` | `pageVariables` | `pageVariables` | The two match on every root, so nothing changed and nothing needed a fork. Their inputs come from the same hooks (`usePredicateScope`, `useRecordContext().data`, `usePageVariables`). One input difference already existed and is unchanged here: `SchemaRenderer` adds a node's API-bound `data` to `predicateScope` once the binding answers, and the badge passes the ambient scope (see Acceptance notes). For the item-level scope, the old literal was `{ ...row, ...predicateScope, current_user, record: row, data: row, page }`. The new one is `{ ...row, ...buildNodeEvaluationScope(...), record: row, data: row }`. Every key ends with the same value: `current_user` and `page` are written after the ambient spread in both, and `record` / `data` are set to the row last in both, so an ambient `record` / `data` is still covered, and an absent row still gives `record: undefined`, as before. ## Note on the card's line address The triage comment's `containers.tsx` line range points at the item-level `isItemVisible` scope. The card body describes the badge mirror from objectui#12987. The badge mirror is the one with the same roots as `SchemaRenderer`. The item-level scope has extra roots (flat row, `data`, unconditional `record`); those are kept unchanged and still written out at that site. ## Tests All run from the repo root through the shared verify lock. Each lock line read `VERDICT command-exit 0` unless stated otherwise. - New pin `packages/react/src/__tests__/buildNodeEvaluationScope-12992.test.ts`: the builder is exported from the package entry and returns the four roots for a fixture. `record` is absent for `undefined` / `null` / string / number / array. An ambient `record` is kept when no row is bound, and a bound row overrides it. `current_user` and `page` are written over the ambient spread. - New source pin `packages/components/src/__tests__/page-tabs-scope-builder-12992.test.ts` (reads `containers.tsx` through `scripts/js-comment-mask.mjs`): the file imports `buildNodeEvaluationScope` from `@object-ui/react`, calls it, and spells no `current_user`. - New pins plus the objectui#12987 pins (`page-tabs-count-visible-when-12987.test.tsx`, unchanged): 3 files, 15 tests passed. - Every test file under `packages/react` and `packages/components` that names `visibleWhen`, `page:tabs`, `configEvaluationScope`, `evaluateConfigBagInScope` or `useConfigBagEvaluator` (56 files, new pins included): 56 files, 643 tests passed, at `654022a9c3`. Later commits change only a test-file comment line and add the changeset. - `packages/react/src/utils/configBag.pin.test.ts` (its allowlist names the `boundRecord` row test in `SchemaRenderer.tsx`, which stays in that file) plus the source pin: 2 files, 5 tests passed, at `0706ba39cd`. - Type-check: closure build `pnpm --filter '@object-ui/components^...' build` (8 projects, exit 0), then `pnpm --filter @object-ui/react type-check && pnpm --filter @object-ui/components type-check`, exit 0. With `--listFiles`, each package's `tsconfig.test.json` program includes its new test file (1 hit each). - Ablation, one run. `ablation-replace` put the inline badge mirror back into `containers.tsx` (anchor 1 to 0, blob `7743264bea58` to `d2e3e857a674`). The source pin went red (`no longer spells current_user itself`; 1 failed, 9 passed across the source pin and the objectui#12987 pins), and the objectui#12987 pins stayed green. Restore: blob after restore `7743264bea58`, equal to the HEAD blob, and `git diff HEAD` was empty. ## Gates (exit codes, at `0706ba39cd`) - `check:phantom-deps`, `check:self-import`, `check:unreferenced-sources`, `check:doc-example-readers`, `check:handler-key-reads`, `check:side-effects-array`, `check:element-data-source-declaration`, `check:spec-symbols`, `check:action-forward-parity`, `check:designer-field-key-parity`, `check:icon-record-names`, `check:i18n-keys`, `check:i18n-drift`, `check:i18n-designer-parity`, `check:control-bytes`, `check:new-line-citations`, `node scripts/check-hand-rolled-comment-mask.mjs`, `node scripts/check-changeset-fixed.mjs`, `node scripts/check-changeset-presence.mjs`: exit 0. - `check:spec-floors`: exit 1. It is red on main too, and this change does not affect it. - Lint, narrowed to the 4 changed `.ts`/`.tsx` files (`eslint --no-inline-config --format json`): 4 files, 0 errors. The warnings are existing `no-explicit-any` hits in `containers.tsx`; this change removes two `as any`. The root config enables no type-aware linting, so linting only these files cannot change the result for any file this change does not touch. - NOT MEASURED, left to CI: full `packages/react/` and `packages/components/` directory runs (the dispatch order forbids whole-directory runs on the shared box). Also `check:readme-exports`: it exits 1 because 29 packages are unbuilt in this worktree ("the population COLLAPSED"), so it did not reach a verdict. It was not in the dispatched battery. ## Acceptance notes - Docs: `packages/react/README.md` does not list the new export. The README was outside this card's file surface. Who picks this up: no one yet. - Unchanged input difference: the badge gates use the ambient predicate scope without a gated node's own API-bound `data`, while `SchemaRenderer` adds that `data` once the binding answers. This is a guess from reading the code; no public entry point was measured. It exists only if a gated node on a related list's path also has an API `dataSource` binding and its `visibleWhen` reads `data.*`. Who picks this up: no one yet. Session: `https://claude.ai/code/session_01XVp19E1MYUTFM3dnkwz8gB` --- _Generated by [Claude Code](https://claude.ai/code/session_01XVp19E1MYUTFM3dnkwz8gB)_ --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 1d3da6c commit 9e7e1a5

5 files changed

Lines changed: 172 additions & 40 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@object-ui/react': minor
3+
'@object-ui/components': patch
4+
---
5+
6+
`@object-ui/react` now exports `buildNodeEvaluationScope(predicateScope, boundRecord, pageVariables)`, the one builder of the scope a page node's `visibleWhen` and other values are evaluated against (objectui#12992). It returns four roots: the ambient predicate scope, `current_user` aliased to `user`, the bound row as `record` (only when there is one), and page variables as `page`. `SchemaRenderer` and `evaluateConfigBagInScope` call it as before. `page:tabs` in `@object-ui/components` now calls it for its derived-badge gates and its item-level `visibleWhen` instead of spelling the scope again, so a root added to the builder reaches the tab strip too. No root changes for any caller.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
* objectui#12992 — `page:tabs` builds no `visibleWhen` scope of its own. Its
11+
* gates are decided over the scope `@object-ui/react`'s
12+
* `buildNodeEvaluationScope` builds, the same function `SchemaRenderer` calls,
13+
* so a root added there is not silently missing here.
14+
*
15+
* Read from source with the comment spans masked, so the prose that explains
16+
* the scope does not count as a spelling of it.
17+
*/
18+
19+
import { describe, it, expect } from 'vitest';
20+
import { readFileSync } from 'node:fs';
21+
import path from 'node:path';
22+
import { fileURLToPath } from 'node:url';
23+
// @ts-expect-error — plain-JS shared helper, intentionally untyped (`allowJs: false`)
24+
import { maskComments } from '../../../../scripts/js-comment-mask.mjs';
25+
26+
const here = path.dirname(fileURLToPath(import.meta.url));
27+
const CONTAINERS = path.resolve(here, '../renderers/layout/containers.tsx');
28+
const code = maskComments(readFileSync(CONTAINERS, 'utf8'));
29+
30+
describe('page:tabs visibleWhen scope (objectui#12992)', () => {
31+
it('imports buildNodeEvaluationScope from @object-ui/react', () => {
32+
expect(code).toMatch(
33+
/import\s*\{[^}]*\bbuildNodeEvaluationScope\b[^}]*\}\s*from\s*'@object-ui\/react'/,
34+
);
35+
});
36+
37+
it('calls it for the scope it evaluates', () => {
38+
expect(code).toMatch(/\bbuildNodeEvaluationScope\s*\(\s*predicateScope\b/);
39+
});
40+
41+
it('no longer spells `current_user` itself', () => {
42+
expect(code).not.toMatch(/\bcurrent_user\b/);
43+
});
44+
});

‎packages/components/src/renderers/layout/containers.tsx‎

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { ComponentRegistry, ExpressionEvaluator, declaredNameField, evalRowPredi
2323
import type { ComponentInput } from '@object-ui/core';
2424
import { actionRendersAt, resolveDeclaredActionIds } from '@object-ui/types';
2525
import type { DeclaredActionsRefusal } from '@object-ui/types';
26-
import { useRecordContext, useAction, useCapabilityGate, usePredicateScope, usePageVariables, useInlineEdit, useActionTextLocalizer, useMetadataItem, reportUnresolvableVisibilityPredicate, resolveInlineAriaProps } from '@object-ui/react';
26+
import { useRecordContext, useAction, useCapabilityGate, usePredicateScope, usePageVariables, useInlineEdit, useActionTextLocalizer, useMetadataItem, reportUnresolvableVisibilityPredicate, resolveInlineAriaProps, buildNodeEvaluationScope } from '@object-ui/react';
2727
import { renderChildren, renderNodeSlot, cn } from '../../lib/utils';
2828
import { LazyIcon } from '../../lib/lazy-icon';
2929
import { RelatedCountStore, useRelatedCountVersion } from '../../hooks/related-count-store';
@@ -662,13 +662,16 @@ const PageTabsRenderer: React.FC<any> = ({ schema, className, ...props }) => {
662662
const recordData: any = ctx?.data;
663663
const isItemVisible = (it: PageTabsItem): boolean => {
664664
if (it?.visibleWhen === undefined || it?.visibleWhen === null) return true;
665+
// The node scope (`buildNodeEvaluationScope`: the ambient scope,
666+
// `current_user`, `record`, `page`) plus this site's own breadth: the row
667+
// spread flat underneath, and `record` / `data` set to the row
668+
// unconditionally on top. Composed rather than spelled out again, so a
669+
// root added to the node scope reaches this gate too (objectui#12992).
665670
const evaluator = new ExpressionEvaluator({
666671
...(recordData && typeof recordData === 'object' ? recordData : {}),
667-
...predicateScope,
668-
current_user: (predicateScope as any)?.user,
672+
...buildNodeEvaluationScope(predicateScope, recordData, pageVariables),
669673
record: recordData,
670674
data: recordData,
671-
page: pageVariables,
672675
});
673676
// evaluateCondition is fail-open (unparseable predicate → visible) — the
674677
// same semantics SchemaRenderer applies to component-level `visibleWhen`,
@@ -724,12 +727,11 @@ const PageTabsRenderer: React.FC<any> = ({ schema, className, ...props }) => {
724727

725728
// objectui#12987 — a related list the tab body does not draw is not counted.
726729
// The body draws each block through `SchemaRenderer`, which decides a node's
727-
// `visibleWhen` with `ExpressionEvaluator.evaluateCondition` against the
728-
// ambient predicate scope, `current_user` aliased to `user`, the page's
729-
// bound row as `record` (only when there is one) and page variables as
730-
// `page`. The gates below are decided by that same evaluator over that same
731-
// scope, read from the same three hooks — NOT the item-level scope of
732-
// `isItemVisible` above, which also spreads the row flat and binds `data`.
730+
// `visibleWhen` with `ExpressionEvaluator.evaluateCondition` over the scope
731+
// `buildNodeEvaluationScope` builds. The gates below are decided by that same
732+
// evaluator over a scope built by that same function (objectui#12992), from
733+
// the same three hooks — NOT the item-level scope of `isItemVisible` above,
734+
// which also spreads the row flat and binds `data`.
733735
//
734736
// A predicate that cannot be evaluated yet (a root still loading, a
735737
// malformed source) is treated as the body treats it: `evaluateCondition`
@@ -752,14 +754,9 @@ const PageTabsRenderer: React.FC<any> = ({ schema, className, ...props }) => {
752754
const isGateOpen = (gate: any): boolean => {
753755
const known = gateVerdicts.get(gate);
754756
if (known !== undefined) return known;
755-
blockEvaluator ??= new ExpressionEvaluator({
756-
...predicateScope,
757-
current_user: (predicateScope as any)?.user,
758-
...(recordData && typeof recordData === 'object' && !Array.isArray(recordData)
759-
? { record: recordData }
760-
: null),
761-
page: pageVariables,
762-
});
757+
blockEvaluator ??= new ExpressionEvaluator(
758+
buildNodeEvaluationScope(predicateScope, recordData, pageVariables),
759+
);
763760
const verdict = blockEvaluator.evaluateCondition(gate.visibleWhen, {
764761
onFault: (reason) =>
765762
reportUnresolvableVisibilityPredicate(

‎packages/react/src/SchemaRenderer.tsx‎

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -656,27 +656,44 @@ const isDataObjectValue = (key: string, value: unknown): boolean =>
656656
!PREDICATE_CHAIN_KEYS.has(key) && isConfigBag(value) && !isExpressionEnvelope(value);
657657

658658
/**
659-
* The scope the evaluation memo in {@link SchemaRenderer} evaluates a node's
660-
* values against: the ambient host scope, `current_user` aliased to `user`, the
661-
* page's bound row as `record` (only when there is one), and page variables as
662-
* `page`. Why each root is (and `data` is not) bound is stated where the memo
663-
* builds its evaluator from this.
664-
*
665-
* One function, so the memo and {@link evaluateConfigBagInScope} cannot build
666-
* two different scopes (objectui#10290).
659+
* The scope a page node's values are evaluated against: its `visibleWhen`
660+
* gate and every other expression the evaluation memo in
661+
* {@link SchemaRenderer} evaluates on the node. Four roots, in this order:
662+
*
663+
* 1. **the ambient predicate scope**: every root `predicateScope` carries (the
664+
* host scope `usePredicateScope()` publishes, plus a node's API-bound
665+
* `data` once it has answered), spread first;
666+
* 2. **`current_user`**: aliased to the ambient `user`, so `user.email` and
667+
* `current_user.email` read the same value;
668+
* 3. **`record`**: the page's bound row, ONLY when there is one (a non-array
669+
* object). No row binds nothing, so a `record` the ambient scope supplies
670+
* is not shadowed by an empty one; a row, written after the spread, wins;
671+
* 4. **`page`**: the page variables (`PageSchema.variables`) as `page.<var>`.
672+
*
673+
* Why each root is (and `data` is not) bound is stated where the memo builds
674+
* its evaluator from this.
675+
*
676+
* The ONE builder of this scope. The memo and {@link evaluateConfigBagInScope}
677+
* call it, so they cannot build two different scopes (objectui#10290), and it
678+
* is exported for the `page:tabs` derived badge in `@object-ui/components`,
679+
* which decides the `visibleWhen` gates in front of a related list the way the
680+
* tab body decides them (objectui#12987, objectui#12992). A root added here
681+
* reaches every one of them.
667682
*/
668-
const configEvaluationScope = (
683+
export function buildNodeEvaluationScope(
669684
predicateScope: Record<string, unknown>,
670685
boundRecord: unknown,
671686
pageVariables: unknown,
672-
): Record<string, unknown> => ({
673-
...predicateScope,
674-
current_user: predicateScope?.user,
675-
...(boundRecord && typeof boundRecord === 'object' && !Array.isArray(boundRecord)
676-
? { record: boundRecord }
677-
: null),
678-
page: pageVariables,
679-
});
687+
): Record<string, unknown> {
688+
return {
689+
...predicateScope,
690+
current_user: predicateScope?.user,
691+
...(boundRecord && typeof boundRecord === 'object' && !Array.isArray(boundRecord)
692+
? { record: boundRecord }
693+
: null),
694+
page: pageVariables,
695+
};
696+
}
680697

681698
/**
682699
* Evaluate ONE config value by its key. This is the per-key rule of the
@@ -740,7 +757,7 @@ export function evaluateConfigBagInScope(
740757
): unknown {
741758
if (!isConfigBag(bag)) return bag;
742759
const evaluator = new ExpressionEvaluator(
743-
configEvaluationScope(predicateScope, boundRecord, pageVariables),
760+
buildNodeEvaluationScope(predicateScope, boundRecord, pageVariables),
744761
);
745762
return evaluateConfigBagWith(evaluator, bag as Record<string, unknown>);
746763
}
@@ -1268,11 +1285,13 @@ export const SchemaRenderer: ForwardRefExoticComponent<
12681285
// no row of its own" into "this surface's row is empty" — only the latter
12691286
// is entitled to shadow.
12701287
//
1271-
// Built by `configEvaluationScope`, which `useConfigBagEvaluator` shares, so
1272-
// an action container's members evaluate against this same scope
1273-
// (objectui#10290).
1288+
// Built by `buildNodeEvaluationScope`, which `useConfigBagEvaluator`
1289+
// shares, so an action container's members evaluate against this same
1290+
// scope (objectui#10290), and which the `page:tabs` badge in
1291+
// `@object-ui/components` calls for its `visibleWhen` gates
1292+
// (objectui#12992).
12741293
const evaluator = new ExpressionEvaluator(
1275-
configEvaluationScope(predicateScope, boundRecord, pageVariables),
1294+
buildNodeEvaluationScope(predicateScope, boundRecord, pageVariables),
12761295
);
12771296
// Shallow copy: the interpreter's working copy, typed as renderer state
12781297
// rather than as the authoring face (objectui#11349). See
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
* objectui#12992 — the scope a node's `visibleWhen` is evaluated against has
11+
* ONE builder, exported at the package entry so the `page:tabs` badge in
12+
* `@object-ui/components` calls it instead of spelling the scope again.
13+
*
14+
* This pins the four roots it returns for a fixture: the ambient predicate
15+
* scope, `current_user` aliased to `user`, `record` only when a row is bound,
16+
* and `page` variables.
17+
*/
18+
19+
import { describe, it, expect } from 'vitest';
20+
import { buildNodeEvaluationScope } from '../index';
21+
22+
const USER = { id: 'u1', email: 'ada@example.com' };
23+
const ROW = { id: 'ENV-1', plan: 'pro' };
24+
const PAGE = { selected: 'a' };
25+
26+
describe('buildNodeEvaluationScope (objectui#12992)', () => {
27+
it('is exported from the package entry', () => {
28+
expect(typeof buildNodeEvaluationScope).toBe('function');
29+
});
30+
31+
it('returns the ambient scope, `user` / `current_user`, `record` and `page`', () => {
32+
const scope = buildNodeEvaluationScope({ user: USER, features: { beta: true } }, ROW, PAGE);
33+
expect(scope).toEqual({
34+
user: USER,
35+
features: { beta: true },
36+
current_user: USER,
37+
record: ROW,
38+
page: PAGE,
39+
});
40+
expect(scope.current_user).toBe(scope.user);
41+
expect(scope.record).toBe(ROW);
42+
});
43+
44+
it('binds no `record` when no row is bound', () => {
45+
for (const noRow of [undefined, null, 'ENV-1', 7, [ROW]]) {
46+
const scope = buildNodeEvaluationScope({ user: USER }, noRow, PAGE);
47+
expect(Object.prototype.hasOwnProperty.call(scope, 'record'), String(noRow)).toBe(false);
48+
}
49+
});
50+
51+
it('keeps an ambient `record` when no row is bound, and a bound row wins over it', () => {
52+
const ambient = { id: 'HOST' };
53+
expect(buildNodeEvaluationScope({ record: ambient }, undefined, PAGE).record).toBe(ambient);
54+
expect(buildNodeEvaluationScope({ record: ambient }, ROW, PAGE).record).toBe(ROW);
55+
});
56+
57+
it('writes `current_user` and `page` over the ambient spread', () => {
58+
const scope = buildNodeEvaluationScope(
59+
{ user: USER, current_user: { id: 'stale' }, page: { stale: true } },
60+
undefined,
61+
PAGE,
62+
);
63+
expect(scope.current_user).toBe(USER);
64+
expect(scope.page).toBe(PAGE);
65+
});
66+
});

0 commit comments

Comments
 (0)