Skip to content

Name clashes throw, optional struct fields, toTypeScript fixes - #212

Merged
krisnye merged 2 commits into
mainfrom
krisnye/clash-warn-struct-optional
Oct 7, 2026
Merged

krisnye merged 2 commits into
mainfrom
krisnye/clash-warn-struct-optional

Conversation

@krisnye

@krisnye krisnye commented Oct 6, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Resolves the two decisions left open in #211, plus toTypeScript / ToType fixes.

  • A component/resource name clash always throws. A resource is stored as a same-named component column on its own entity, so components and resources share one name space. Until now a resource reusing a component's name silently replaced the component's schema (Scope in schema, reserved-name guards, conformance context, and 0.10.20 back-compat #211 turned that into a warning). That changes the column's type and flags, for example making every entity's value non-persistent, and every query on the component also returns the resource's entity. Store.create now throws in both directions, even when the schemas are identical. Database.Plugin.create also rejects a clash at compile time, within one plugin or against its extends / imports. A clash formed through Plugin.combine is caught at runtime. The nameClashes option from the first draft of this PR is removed.
  • Optional struct fields no longer crash. An object schema such as { a: Vec2, b: Vec2 } with default: {} used to crash in the generated struct writer (Cannot read properties of undefined), and an absent primitive field silently wrote NaN. Struct eligibility is unchanged. Instead, getStructLayout gives each object field that isn't required a frozen fill: the field's own default, otherwise zeros of its shape. The writer falls back to it with one ?? per optional field and no allocation. Fields listed in required generate exactly the same code as before, and an omitted field reads back as its fill.
  • Struct layouts are actually memoized. getStructLayout was keyed on a fresh { schema, layout } object (and its throwing path skipped the cache entirely), so it never hit. Every struct column recompiled its read/write functions with new Function, and those memo maps grew without bound. It is now a per-schema WeakMap keyed by layout mode.
  • anyOf agrees between ToType and toTypeScript. ToType resolved anyOf to any, while toTypeScript rendered a union. Both now give the union (anyOf shares the oneOf branch, so no extra conditional depth).
  • Repeated parameter shapes are named after the parameter. A shared object shape used as a named parameter is hoisted as interface Opts, not Shape1.
  • typeName for types JSON Schema can't describe. { typeName: "Response" } renders as Response in toTypeScript. In ToType, a global class name (Response, HTMLElement, ReadableStream) resolves to its instance type, and any other name is any. It's meant for service schemas.
  • nullable is a compile error. Schema.nullable?: never rejects OpenAPI's nullable, which used to be silently ignored. Use oneOf with { type: "null" }.
  • The data-ai rule says the store throws on a clash.
  • Version bump to 0.10.25.

Testing

  • pnpm run lint, pnpm run typecheck (with build caches cleared), pnpm run check:workspace and pnpm run build pass.
  • pnpm run test passes in every package: data 3,611 tests, plus all libraries and samples.
  • New tests:
    • a clash throws in both directions and within one schema, including with identical schemas, through extends and through Plugin.combine; the type tests reject a clash in one plugin and across extends, and keep inference for distinct names (about 1.3% more type instantiations in packages/data);
    • layouts are memoized, and fill appears only on non-required fields and is frozen;
    • the writer zero-fills absent nested fields, uses a field's own default, writes 0 rather than NaN for an absent primitive, and generates no guard for required fields;
    • a store resource with default: {} is created, and a component with absent fields inserts and round-trips through toData / fromData;
    • anyOf type and text agree, a repeated parameter shape is named after its parameter, typeName renders verbatim and resolves to HTMLElement / Response, and nullable fails to compile.

🤖 Generated with Claude Code

krisnye and others added 2 commits October 6, 2026 12:54
…ruct layouts

- Store/Database create take nameClashes to acknowledge a legacy component/resource clash; unlisted clashes warn per store
- struct writers fill absent optional fields (field default or zeros) instead of crashing or writing NaN
- getStructLayout memoizes per schema object, so struct read/write compile once per schema

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nces

- ToType resolves anyOf as a union, matching toTypeScript.
- toTypeScript names a repeated parameter shape after the parameter.
- New Schema.typeName emits a named type reference (Response, HTMLElement);
  ToType resolves global class names to their instance type.
- Schema.nullable?: never rejects OpenAPI nullable at compile time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@krisnye krisnye changed the title nameClashes acknowledgement, optional struct fields, memoized struct layouts nameClashes acknowledgement, optional struct fields, toTypeScript fixes Oct 7, 2026
@krisnye
krisnye merged commit 6630c49 into main Oct 7, 2026
3 checks passed
@krisnye
krisnye deleted the krisnye/clash-warn-struct-optional branch October 7, 2026 01:21
@krisnye krisnye changed the title nameClashes acknowledgement, optional struct fields, toTypeScript fixes Name clashes throw, optional struct fields, toTypeScript fixes Oct 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant