fix: surface error instead of panicking on unclosed exported namespace/module#803
Open
ammubhave wants to merge 1 commit into
Open
fix: surface error instead of panicking on unclosed exported namespace/module#803ammubhave wants to merge 1 commit into
ammubhave 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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