|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [ADR-0078] The page-component TYPE gate — the author-time rejection the open |
| 5 | + * `type` union never had (#12950, riding the #12183 ruling of 2026-08-26). |
| 6 | + * |
| 7 | + * ## What was missing |
| 8 | + * |
| 9 | + * `PageComponentSchema.type` is `z.union([PageComponentType, z.string()])`, so |
| 10 | + * an arbitrary string parses. The open arm is deliberate and load-bearing — |
| 11 | + * see `component-type-vocabulary.ts` for the measured inventory of what a |
| 12 | + * union collapse would break — but it also swallowed the spec's OWN |
| 13 | + * namespaces: `global:serch` parsed as happily as `global:search`, every |
| 14 | + * authoring command validated it clean, and the console drew the literal |
| 15 | + * "Component Placeholder" scaffold in front of an end user. The origin card |
| 16 | + * measured that in a real browser: two published pages whose entire content |
| 17 | + * was placeholder, with zero diagnostics anywhere on the authoring path. |
| 18 | + * |
| 19 | + * ## What this rule does |
| 20 | + * |
| 21 | + * One namespace-shaped judgment per authored component node: a `type` inside a |
| 22 | + * namespace the enum itself populates (derived, never restated) must be a type |
| 23 | + * the spec answers for — an enum member, a `ComponentPropsMap` row (which |
| 24 | + * carries the measured string-arm registrations, including the tombstoned |
| 25 | + * `element:filter` / `element:form`), or a `STRING_ARM_REGISTERED_TYPES` |
| 26 | + * ledger entry. Anything else is refused with `severity: 'error'` and the |
| 27 | + * closest declared spellings. |
| 28 | + * |
| 29 | + * Types OUTSIDE the reserved namespaces are untouched — plugin widgets |
| 30 | + * (`mcp:connect-agent`), kebab SDUI blocks (`flex`, `object-chart`, |
| 31 | + * `page-header`), dot shapes (`custom.widget`) all keep the open-arm contract. |
| 32 | + * This rule closes nothing the extension story declares open; it closes the |
| 33 | + * spec's own vocabulary, which nothing ever declared open — it was merely |
| 34 | + * unchecked. |
| 35 | + * |
| 36 | + * ## Why `error` from birth (contrast #5068's warning-first) |
| 37 | + * |
| 38 | + * The props gate launched advisory because the live corpus VIOLATED the |
| 39 | + * declarations it enforced. This rule's live corpus is clean, measured before |
| 40 | + * severity was chosen: across `examples/**` and `packages/**` page sources the |
| 41 | + * only reserved-namespace strings outside the accept set are conversion-fixture |
| 42 | + * stand-ins (`record:detail`, `record:list`, `element:custom` in |
| 43 | + * `conversions/registry.ts` — replayed by the conversion harness, never fed to |
| 44 | + * the authoring commands) and the ledgered `record:line_items`. An error gate |
| 45 | + * with zero live findings breaks no one and refuses the next `global:serch` at |
| 46 | + * the door instead of in front of a user. |
| 47 | + */ |
| 48 | + |
| 49 | +import { |
| 50 | + hasReservedComponentNamespace, |
| 51 | + isKnownComponentType, |
| 52 | + KNOWN_COMPONENT_TYPE_CANDIDATES, |
| 53 | +} from '@objectstack/spec/ui'; |
| 54 | +import { findClosestMatches, formatSuggestion } from '@objectstack/spec/shared'; |
| 55 | +import { walkPageComponents, type AnyRec } from './page-walk.js'; |
| 56 | + |
| 57 | +/** A component `type` inside a spec-reserved namespace that the vocabulary does not declare. */ |
| 58 | +export const COMPONENT_TYPE_UNKNOWN = 'component-type-unknown'; |
| 59 | + |
| 60 | +export interface ComponentTypeFinding { |
| 61 | + severity: 'error'; |
| 62 | + /** Diagnostic rule id. */ |
| 63 | + rule: string; |
| 64 | + /** Human-readable location, e.g. `page "app_launcher" · global:serch`. */ |
| 65 | + where: string; |
| 66 | + /** Config path, e.g. `pages[0].regions[1].components[0].type`. */ |
| 67 | + path: string; |
| 68 | + /** What is wrong. */ |
| 69 | + message: string; |
| 70 | + /** How to fix it. */ |
| 71 | + hint: string; |
| 72 | +} |
| 73 | + |
| 74 | +function isRec(v: unknown): v is AnyRec { |
| 75 | + return !!v && typeof v === 'object' && !Array.isArray(v); |
| 76 | +} |
| 77 | + |
| 78 | +function strName(v: unknown): string | undefined { |
| 79 | + return typeof v === 'string' && v.length > 0 ? v : undefined; |
| 80 | +} |
| 81 | + |
| 82 | +/** Coerce a collection (array or name-keyed map) to an array of records. */ |
| 83 | +function asArray(v: unknown): AnyRec[] { |
| 84 | + if (Array.isArray(v)) return v as AnyRec[]; |
| 85 | + if (v && typeof v === 'object') { |
| 86 | + return Object.entries(v as AnyRec).map(([name, def]) => ({ name, ...(def as AnyRec) })); |
| 87 | + } |
| 88 | + return []; |
| 89 | +} |
| 90 | + |
| 91 | +export function validateComponentTypes(stack: AnyRec): ComponentTypeFinding[] { |
| 92 | + const findings: ComponentTypeFinding[] = []; |
| 93 | + if (!isRec(stack)) return findings; |
| 94 | + |
| 95 | + const pages = asArray(stack.pages); |
| 96 | + for (let pi = 0; pi < pages.length; pi++) { |
| 97 | + const page = pages[pi]; |
| 98 | + if (!isRec(page)) continue; |
| 99 | + const pageName = strName(page.name) ?? `#${pi}`; |
| 100 | + |
| 101 | + for (const { component, path } of walkPageComponents(page, `pages[${pi}]`)) { |
| 102 | + const type = strName(component.type); |
| 103 | + if (!type) continue; |
| 104 | + if (!hasReservedComponentNamespace(type)) continue; // the open arm's half — deliberately untouched |
| 105 | + if (isKnownComponentType(type)) continue; |
| 106 | + |
| 107 | + const suggestions = findClosestMatches(type, KNOWN_COMPONENT_TYPE_CANDIDATES); |
| 108 | + const suggestion = formatSuggestion(suggestions); |
| 109 | + findings.push({ |
| 110 | + severity: 'error', |
| 111 | + rule: COMPONENT_TYPE_UNKNOWN, |
| 112 | + where: `page "${pageName}" · ${type}`, |
| 113 | + path: `${path}.type`, |
| 114 | + message: |
| 115 | + `\`${type}\` is not a component type the platform vocabulary declares. Its namespace ` + |
| 116 | + `(\`${type.slice(0, type.indexOf(':'))}:\`) belongs to the standard component vocabulary, so nothing ` + |
| 117 | + 'will ever render this node — the page would validate, publish, and then draw a placeholder ' + |
| 118 | + 'scaffold in front of the end user.' + |
| 119 | + (suggestion ? ` ${suggestion}` : ''), |
| 120 | + hint: suggestions.length |
| 121 | + ? `Rename \`${type}\` → \`${suggestions[0]}\`.` |
| 122 | + : `Use a declared component type from the standard vocabulary, or — for a custom component ` + |
| 123 | + `registered by your own plugin — give it its own namespace (e.g. \`my-plugin:${type.slice(type.indexOf(':') + 1)}\`) ` + |
| 124 | + 'so it cannot be mistaken for platform vocabulary.', |
| 125 | + }); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return findings; |
| 130 | +} |
0 commit comments