Skip to content

fix(applications): preserve in-table comments in the visual schema editor#1468

Open
dawsontoth wants to merge 1 commit into
stagefrom
claude/schema-comments-inconsistent-248344
Open

fix(applications): preserve in-table comments in the visual schema editor#1468
dawsontoth wants to merge 1 commit into
stagefrom
claude/schema-comments-inconsistent-248344

Conversation

@dawsontoth

Copy link
Copy Markdown
Contributor

Fixes #1462.

Problem

The visual schema editor keeps untouched tables byte-for-byte, but regenerates a table from its parsed model the moment it's edited. The model had nowhere to store comments that live inside a @table block but aren't attached above a field, so editing a table silently dropped or relocated them. Because unedited files round-trip perfectly, the loss only surfaced on the first edit — the "inconsistent state" from the issue.

Three failure modes, reproduced then fixed (each now a regression test):

Comment location Old behavior on edit
Trailing the last field, before } dropped
Body is only a comment (no fields) comment dropped, and emitted type X @table {\n} — an empty field block, which is a hard graphql-js syntax error that makes Harper reject the whole schema (the "table never loads to the database" symptom)
On the header line: type X @table { # note relocated to a standalone line above the first field

Fix

  • Model (types.ts): added trailingComments: string[] and headerComment?: string to TableModel.
  • Parser (parseSchema.ts): parseFields now returns leftover trivia as trailingComments instead of discarding it; new splitHeaderComment peels a header-line comment off the body so it isn't misattributed to the first field.
  • Serializer (serializeSchema.ts): generateTable re-emits the header comment on the header line and trailing comments before } (shared indentCommentLines helper).
  • Empty-body guard (TableCard.tsx, FieldRow.tsx): the remove button on a table's last field is disabled (a GraphQL type needs ≥1 field), and a zero-field table shows a warning in both the summary line and the field list — so the invalid, schema-breaking state is unreachable through normal editing and surfaced when a file already contains it.

Design note

For the empty-body case I chose a UI guard + visible warning rather than blocking Save or silently dropping a zero-field table — matching this codebase's existing pattern (invalid type names are flagged with a hint, not hard-blocked). Silently dropping a table the user is actively editing would be its own inconsistent-state bug. Happy to add a hard Save-time block if preferred.

Testing

  • New regression tests in parseSchema.test.ts / serializeSchema.test.ts encode the exact three reproductions (edit → serialize) — they fail on the old code.
  • Full suite green: 1276 passing / 11 skipped; tsc, oxlint, and commitlint clean.

🤖 Generated with Claude Code

…itor

The visual schema editor keeps untouched tables byte-for-byte but regenerates
a table from its parsed model the moment it's edited. The model had nowhere to
store comments that live inside a `@table` block but aren't attached above a
field, so editing a table silently dropped or relocated them. Because unedited
files round-trip perfectly, the loss only surfaced on the first edit — an
inconsistent state (studio #1462).

Three failure modes, all now covered by regression tests:

- A comment trailing the last field (before `}`) was accumulated as pending
  trivia and discarded when no field followed. `parseFields` now returns the
  leftover as `trailingComments`; `generateTable` re-emits it before `}`.
- A comments-only body dropped the comment AND emitted `type X @table {\n}` —
  an empty field block, which is a hard graphql-js syntax error that makes
  Harper reject the whole schema (the "table never loads" symptom). To keep
  that state unreachable, the visual editor now blocks removing a table's last
  field and flags a zero-field table with a warning.
- A header-line comment (`type X @table { # note`) was relocated to a standalone
  line above the first field. It's now captured as `headerComment` and re-emitted
  on the header line.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@dawsontoth dawsontoth requested a review from a team as a code owner July 10, 2026 20:20
@github-actions

Copy link
Copy Markdown

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 47.16% 4427 / 9386
🔵 Statements 47.52% 4724 / 9941
🔵 Functions 39.33% 1062 / 2700
🔵 Branches 39.45% 2875 / 7287
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
src/features/instance/applications/components/SchemaEditorView/FieldRow.tsx 43.75% 68.75% 22.85% 39.53% 56, 58, 60, 62, 83-90, 126-267
src/features/instance/applications/components/SchemaEditorView/TableCard.tsx 55.31% 63.88% 36.36% 51.28% 57, 59, 61, 123, 140-225, 247-257
src/features/instance/applications/lib/schema/mutations.ts 96.55% 91.3% 95% 100% 79
src/features/instance/applications/lib/schema/parseSchema.ts 86.91% 78.02% 100% 86.72% 27-28, 35, 42-43, 53, 109, 124, 130-134, 137-138, 143, 151, 156, 208-209, 212-213, 228, 231-232, 240-241, 245, 266-267, 270-271, 280, 311, 315, 330, 374, 387, 391, 395, 410, 449, 471, 475, 483, 488
src/features/instance/applications/lib/schema/serializeSchema.ts 100% 85% 100% 100%
src/features/instance/applications/lib/schema/types.ts 65.38% 56% 92.3% 62.5% 140, 168-177, 185-186
Generated in workflow #1434 for commit 02c05cb by the Vitest Coverage Report Action

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces UI constraints to prevent the removal of the last field in a table, ensuring GraphQL types remain valid with at least one field. Additionally, it enhances the schema parser and serializer to preserve header-line and trailing comments inside table bodies during edits, backed by comprehensive unit tests. There are no review comments to address, so I have no feedback to provide.

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.

Table with comments can mess up the visual schema editor

1 participant