diff --git a/.changeset/37502-field-consumers-related-list-child.md b/.changeset/37502-field-consumers-related-list-child.md new file mode 100644 index 00000000000..aebab2af83e --- /dev/null +++ b/.changeset/37502-field-consumers-related-list-child.md @@ -0,0 +1,13 @@ +--- +'@objectstack/lint': patch +--- + +fix(lint): `field-no-consumers` credits a related list's columns to the child object it lists + +Clause-②: no +Metadata-protocol: none + +- An object-form `relatedLists` entry (`{ list, label?, columns? }`) is now read against the child object its `list` names (`child`, or `child.field`), not against the object that declares the list. Its `columns` are the child's field names, so each one counts as a display of the child's field. +- Before, a column was credited to the declaring object's field of the same name. A parent field that nothing displayed stayed silent whenever a child's related-list column had the same name (the blank starter's company lists its contacts with a `phone` column, which kept an undisplayed `company.phone` quiet), and the child's own column did not count for the child. +- What you may see: `os validate` and `os lint` can now report a parent field that only a child's related-list column used to cover. Place the field (a field group, `highlightFields`, a view column) or remove it. A child field that only a related list draws is no longer reported. A column of a child this stack does not declare credits no object. The rule stays a warning. +- `child.field` in `list` now also counts as a read of that relationship field on the child: the list joins on it. diff --git a/packages/cli/test/starter-field-consumers.test.ts b/packages/cli/test/starter-field-consumers.test.ts index 3cc91106197..0f44eb14ca5 100644 --- a/packages/cli/test/starter-field-consumers.test.ts +++ b/packages/cli/test/starter-field-consumers.test.ts @@ -287,19 +287,22 @@ function fieldNoConsumers(stack: AnyRec): { validate: string[]; lint: string[] } * platform's Starter app, and so the blank starter, places every field, * #36945); * - its name in the object's `highlightFields` (the record header); - * - its name in a related list's `columns`, on ANY object. A related list of - * this object, declared on another, shows the field on that object's record - * page. The rule also credits a related list's column names to the object - * that DECLARES the list (measured: the blank starter's `company.phone` - * stays credited by the company's related list of contacts, whose `phone` - * column is the contact's), so the name is dropped from every related list, - * not only from those of this object. + * - its name in the `columns` of a related list of THIS object, declared on + * any object: the list shows the field on the declaring object's record + * page. A column is a field of the child object the entry names (its + * `list`, `child` or `child.field`), and the rule credits it there, so a + * related list of another object keeps its columns: the blank starter's + * company lists its contacts with a `phone` column, which is the + * contact's `phone` and never the company's. */ function withoutPlacement(objects: readonly AnyRec[], objectIndex: number, field: string): AnyRec[] { const drop = (list: unknown) => (Array.isArray(list) ? (list as string[]).filter((f) => f !== field) : list); + const child = String(objects[objectIndex].name); + const listsThisChild = (r: unknown): r is AnyRec => + isRec(r) && typeof r.list === 'string' && r.list.split('.')[0] === child && 'columns' in r; const withoutRelatedListColumn = (obj: AnyRec): AnyRec => Array.isArray(obj.relatedLists) - ? { ...obj, relatedLists: (obj.relatedLists as AnyRec[]).map((r) => ('columns' in r ? { ...r, columns: drop(r.columns) } : r)) } + ? { ...obj, relatedLists: (obj.relatedLists as unknown[]).map((r) => (listsThisChild(r) ? { ...r, columns: drop(r.columns) } : r)) } : obj; return objects.map((obj, i) => { if (i === objectIndex) { diff --git a/packages/lint/src/validate-field-consumers.test.ts b/packages/lint/src/validate-field-consumers.test.ts index 433cc56a7f7..c68595a5a3b 100644 --- a/packages/lint/src/validate-field-consumers.test.ts +++ b/packages/lint/src/validate-field-consumers.test.ts @@ -2,7 +2,7 @@ import { describe, expect, it } from 'vitest'; import { z } from 'zod'; -import { CubeSchema } from '@objectstack/spec/data'; +import { CubeSchema, ObjectSchema } from '@objectstack/spec/data'; import { DatasetSchema } from '@objectstack/spec/ui'; import { PermissionSetSchema } from '@objectstack/spec/security'; import { @@ -1860,6 +1860,69 @@ describe('[#32628] validateFieldConsumers — a field permission\'s key is a car }); }); +describe('[#37502] validateFieldConsumers — a related list\'s columns are fields of the child it lists', () => { + /** + * `company` lists its `contact` rows; both declare `phone`. A view on `probe` + * wakes the rule (a stack of objects alone is not judged) and draws nothing + * of the two. `name` is each object's title, exempt. + */ + const company = (relatedLists?: unknown[]): AnyRec => ({ + name: 'company', + fields: { name: { type: 'text' }, phone: { type: 'phone' } }, + ...(relatedLists ? { relatedLists } : {}), + }); + const contact: AnyRec = { + name: 'contact', + fields: { + name: { type: 'text' }, + phone: { type: 'phone' }, + email: { type: 'email' }, + company: { type: 'lookup', reference: 'company' }, + }, + }; + const stack = (relatedLists?: unknown[]): AnyRec => ({ + objects: [company(relatedLists), contact, { name: 'probe', fields: { name: { type: 'text' } } }], + views: [{ list: { data: { object: 'probe' }, columns: [] } }], + }); + + /** The `object.field` of every finding, sorted. */ + const reported = (s: AnyRec): string[] => + validateFieldConsumers(s).map((f) => `${f.object}.${f.field}`).sort(); + + it('the fixture is a related list the object schema accepts, in both name spellings', () => { + for (const list of ['contact', 'contact.company']) { + expect(ObjectSchema.safeParse(company([{ list, columns: ['name', 'phone'] }])).success, list).toBe(true); + } + }); + + it('baseline: with no related list, both `phone` fields are reported', () => { + expect(reported(stack())).toEqual(['company.phone', 'contact.company', 'contact.email', 'contact.phone']); + }); + + it('⛔ a column credits the CHILD\'s field: the parent\'s same-name `phone` is still reported', () => { + expect(reported(stack([{ list: 'contact', columns: ['name', 'phone'] }]))).toContain('company.phone'); + }); + + it('the child\'s own column counts as a consumer of the child\'s field', () => { + expect(reported(stack([{ list: 'contact', columns: ['name', 'phone'] }]))).toEqual( + ['company.phone', 'contact.company', 'contact.email'], + ); + }); + + it('`child.field` names the same child, and the relationship the list joins on', () => { + expect(reported(stack([{ list: 'contact.company', columns: ['phone', 'email'] }]))).toEqual(['company.phone']); + }); + + it('a bare-string entry carries no columns and credits nothing', () => { + expect(reported(stack(['contact']))).toEqual(['company.phone', 'contact.company', 'contact.email', 'contact.phone']); + }); + + it('⛔ a list of a child this stack does not declare credits no column to the parent', () => { + // Another package's object: its fields are unknown here, so the column lands on no object. + expect(reported(stack([{ list: 'task', columns: ['phone'] }]))).toContain('company.phone'); + }); +}); + describe('[#22161] one-line verdicts — field-no-consumers, the same-name clause bounded', () => { /** `n` objects each declaring `description`, with a view naming only `owner`'s name field. */ const sharedName = (n: number, carried = false): AnyRec => ({ diff --git a/packages/lint/src/validate-field-consumers.ts b/packages/lint/src/validate-field-consumers.ts index 2d8aacd1817..35b0b96570a 100644 --- a/packages/lint/src/validate-field-consumers.ts +++ b/packages/lint/src/validate-field-consumers.ts @@ -25,8 +25,9 @@ * reference is therefore credited to the object whose declaration ENCLOSES it * (`object` / `objectName` / `targetObject` / `data.object` / `config.objectName` * / `list.data.object` / a `dataset` resolved through the dataset's object / a - * map keyed by object name / a flow's trigger object), and only when that - * object actually declares the token. Inside a text blob (a hook handler, an + * map keyed by object name / a flow's trigger object / the child object an + * object-form `relatedLists` entry names in `list` — {@link relatedListChildOf}), + * and only when that object actually declares the token. Inside a text blob (a hook handler, an * action body, a CEL source) the nearest preceding mention of a declared object * is a second candidate — a handler that loads one object and reads its own * genuinely reads both, and under-crediting is the noisy direction. @@ -219,7 +220,12 @@ import { resolveDisplayField, } from '@objectstack/spec/data'; import type { DisplayNameObjectMeta, ServiceObject } from '@objectstack/spec/data'; -import { fieldGroupsPlaceRelationship, isGroupPlaceableRelationship, referenceTargetOf } from '@objectstack/spec/data'; +import { + fieldGroupsPlaceRelationship, + isGroupPlaceableRelationship, + referenceTargetOf, + relatedListEntryName, +} from '@objectstack/spec/data'; import type { DefaultRowScope, FieldPermission, @@ -1073,6 +1079,56 @@ function creditChildEntryField( } } +/** + * The object key whose entries are the record page's related lists + * (`ObjectSchema.relatedLists`). Typed against the spec, so a rename there + * fails this package's typecheck instead of silently turning the crediting + * below off. + */ +const RELATED_LISTS_KEY = 'relatedLists' satisfies keyof ServiceObject; + +/** + * Is `rec` an entry of an object's own `relatedLists`, in its object form + * (`RelatedListEntryObjectSchema`: `{ list, label?, columns? }`)? Only the + * object root's top-level key qualifies; a bare-string entry is no record and + * never reaches here. + */ +function isRelatedListEntry(rec: AnyRec, root: string, segments: readonly string[]): boolean { + return ( + root === 'objects' && + segments.length === 1 && + segments[0] === RELATED_LISTS_KEY && + relatedListEntryName(rec) !== undefined + ); +} + +/** + * [#37502] The object an object-form `relatedLists` entry's subtree is read + * against: the CHILD its `list` names (`child`, or `child.field` for one FK of + * a child with several), never the parent that declares the list. + * + * The entry's `columns` are child FIELD NAMES (`RelatedListEntryObjectSchema`: + * "as field names of the child object"), drawn in the list's tab and in the + * body table a field group places, so each is a display site of the child. + * Read against the declaring parent, a column credited the parent's field of + * the same name and left the child's own column uncredited: the blank + * starter's company lists its contacts with a `phone` column, which kept an + * undisplayed `company.phone` silent. The `list` itself, `child.field`, names + * the child's relationship the list joins on, and is read against the child + * the same way. + * + * A child this stack does not declare (a platform object, another package's + * object) answers `undefined`, so its columns resolve to no object and are + * counted unresolved, like every other field-shaped token that lands nowhere: + * they are never the parent's. + */ +function relatedListChildOf(ledger: ConsumerLedger, rec: AnyRec): string | undefined { + const name = relatedListEntryName(rec); + if (name === undefined) return undefined; + const child = name.includes('.') ? name.slice(0, name.indexOf('.')) : name; + return ledger.isObject(child) ? child : undefined; +} + /** The object context a record establishes for its own subtree, if any. */ function contextOf(ledger: ConsumerLedger, rec: AnyRec, ctx: string | undefined): string | undefined { const named = (v: unknown): string | undefined => (ledger.isObject(v) ? v : undefined); @@ -1131,7 +1187,10 @@ function walk( return; } const rec = node as AnyRec; - const inner = contextOf(ledger, rec, ctx); + // [#37502] A related list's entry is read against the child it lists. + const inner = isRelatedListEntry(rec, root, segments) + ? relatedListChildOf(ledger, rec) + : contextOf(ledger, rec, ctx); // [#20929] An entry of a child collection: its grid draws `childObject`'s // fields, whatever object the enclosing view is bound to. const childEntry: ChildEntryKind | undefined = CHILD_COLLECTION_KEYS.has(leafKey) ? 'collection' : entryKind;