Skip to content

fix(canonical): keep a comment in an empty key beside its opener - #30

Merged
adammharris merged 1 commit into
mainfrom
agent/backlog-a-comment-in-an-empty-key-container-is-dropped
Nov 7, 2026
Merged

adammharris merged 1 commit into
mainfrom
agent/backlog-a-comment-in-an-empty-key-container-is-dropped

Conversation

@adammharris

Copy link
Copy Markdown
Member

What changed

  • rust/fig-core/src/canonical/printer.rs: the KeyValue arm writes its key through a new Printer::key. An empty container holding a trailing comment prints as a block, with the comment beside its opener:

    {
      [ // why
      ]: 1
    }
    
  • bindings/typescript/js/canonical.js: the same change as writeKey. The task did not name this file, but the JavaScript twin's printer lost the comment the same way, and a test showed it before the fix.

  • rust/fig-fuzz/src/lib.rs: {[//\n]:1} joins canonical_seeds.

  • The task is marked done.

Why

An empty container prints inline and leaves its trailing comment to whoever writes its line. An entry's line writes the value's comment, so when the empty container was the key, nothing wrote its comment. It was lost silently and canonical_round_trip failed. The parser refuses a comment between a key and its :, so the comment can only go beside the opener, where capture_open_trailing reads it back.

Closes docs/tasks/a-comment-in-an-empty-key-container-is-dropped.md.

Behavioural change

Canonical output changes for these inputs only: the key is written over two lines and the comment is kept. This is recorded in the commit's Behavioural-change: trailer.

How it was verified

  • Tests written first and seen failing: a_comment_in_an_empty_container_used_as_a_key_stays_beside_its_opener and a_commented_empty_key_at_the_nesting_limit_prints in parser.rs, and a canonical twin test in twins.test.ts. All pass now. The first covers the task's three inputs plus {[// a\n]: {[// b\n]: 2}}, {&x [// a\n]: 1} and a key nested inside a key.
  • ship ci: all 11 jobs passed, with node_modules installed so the TypeScript suite ran for real (118 passed, 0 failed, 0 skipped).
  • The task's repro through the built CLI now keeps the comment.

Not in the gate, and not clean on main either, in code this PR does not change: cargo clippy -D warnings stops on 18 errors in fig-kit and flags capture_open_trailing in parser.rs, and cargo fmt --check reports diffs in other files.

An empty container prints inline and leaves its trailing comment to
whoever writes its line. An entry's line writes the value's trailing
comment, so when the empty container was the entry's key nothing wrote
its own: `{[//\n]:1}` lost the comment and canonical_round_trip failed.
The parser refuses a comment between a key and its `:`, so it cannot go
after the key's close.

The KeyValue arm now writes its key through `Printer::key`, which prints
an empty container holding a trailing comment as a block, the comment
beside the opener, where `capture_open_trailing` reads it back. The
JavaScript twin's printer lost the comment the same way and gets the same
`writeKey`, so the two still write one spelling. `{[//\n]:1}` joins
`canonical_seeds`, so every PR checks it.

Closes docs/tasks/a-comment-in-an-empty-key-container-is-dropped.md.

Behavioural-change: a line comment after the opening bracket of an empty container used as a mapping key in canonical text now prints beside that bracket, with the key written over two lines, in both the compiled format and `@diaryx/fig`'s canonical language. It used to be dropped without a word. `fig fmt` on a `.fig` file keeps such a comment.
@adammharris
adammharris merged commit d97b314 into main Nov 7, 2026
10 checks passed
@adammharris
adammharris deleted the agent/backlog-a-comment-in-an-empty-key-container-is-dropped branch November 7, 2026 16:05
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