fix: surface error instead of panicking on unclosed exported namespace/module - #803
fix: surface error instead of panicking on unclosed exported namespace/module#803ammubhave wants to merge 1 commit into
Conversation
…e/module
swc's parser recovers from a missing close brace for an unclosed
`export namespace`/`export module` without emitting any diagnostic, so it
isn't caught by `ensure_no_specific_syntax_errors` and reaches code
generation, where `gen_membered_body` panicked via
`.expect("Expected to find a close brace token.")`.
Push a generation diagnostic (which makes `generate` return an error)
instead of panicking when the close brace token is missing.
Closes dprint#802
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
Thanks, but I’d rather this get fixed properly in swc. — AI comment: Thanks for the detailed root-cause work here — I verified it independently and it held up, including the "0 diagnostics" observation. Digging into why swc reports nothing, the cause turns out to be upstream and more general than namespaces. So swc does generate the error — it just throws it away. I've opened a fix upstream: swc-project/swc#12137. It makes recoverable errors roll back with the parser checkpoint instead of being suppressed, so Going to close this one in favour of fixing it at the source. Your diagnosis is what made the upstream fix possible — thanks for taking the time to dig into the parser properly rather than just papering over the panic. |
Summary
Fixes #802.
Formatting a file with an unclosed exported
namespace/modulepanicked withExpected to find a close brace token.instead of reporting a syntax error:Root cause
swc's parser recovers from the missing
}for theexport namespace/export modulecase and produces aTsModuleDeclwithout emitting any diagnostic (verified: 0 diagnostics). The parse-time gateensure_no_specific_syntax_errorstherefore has nothing to filter, so code generation proceeds andgen_membered_bodypanics on.expect("Expected to find a close brace token.").Every other unclosed form (bare
namespace,declare namespace/module/global, baremodule, dottednamespace Foo.Bar) does emit an swc diagnostic and was already handled.Fix
When no close-brace token is found in
gen_membered_body, push a generation diagnostic (which makesgeneratereturn anErr) instead of.expect()-panicking. This reuses the existingcontext.diagnosticserror channel and, because it relies on the view's direct-token-ownership, also handles nested cases where the single}belongs to the inner block.The resulting error is, e.g.:
Tests
Added unit tests in
src/format_text.rscovering the panic→error variants (simple,export module, empty body, nested, unclosed-outer-with-complete-inner, dotted name) — including exact-message assertions that pin the reported line — plus regression cases confirming well-formed namespaces still format.🤖 Generated with Claude Code