fix(formatter): keep parens in chained export default type cast#23678
Closed
ammubhave wants to merge 1 commit into
Closed
fix(formatter): keep parens in chained export default type cast#23678ammubhave wants to merge 1 commit into
export default type cast#23678ammubhave wants to merge 1 commit into
Conversation
`export default (fn as T)` was losing its outer parentheses when the
cast was chained, e.g. `export default (function foo() {} as unknown as
Foo)`. The parens are load-bearing: without them the leading `function`/
`class` token makes `export default` parse the right-hand side as a
declaration, detaching the `as` cast.
The `export default` case in `ts_as_or_satisfies_needs_parens` only
inspected the direct inner expression, so a chained cast (whose inner is
another `TSAsExpression`) failed the `FunctionExpression`/
`ClassExpression` check. Walk to the leftmost descendant instead,
mirroring the existing `CallExpression` handler.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Merging this PR will not alter performance
Comparing Footnotes
|
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.
Fixes #23501
Problem
oxfmtdrops the load-bearing outer parentheses inexport default (fn as T)when the cast is chained:Without the parens the leading
function/classtoken makesexport defaultparse the right-hand side as a declaration, so the trailingas …cast is no longer attached to anything. Downstreamnew defaultExport(...)then fails with TS7009.Cause
The
ExportDefaultDeclarationarm ofts_as_or_satisfies_needs_parensonly inspected the direct inner expression:A single
asworked, but for a chained cast the outerasnode's inner is anotherTSAsExpression, so theFunctionExpression/ClassExpressioncheck failed and the parens were dropped.Fix
Walk to the leftmost descendant via
ExpressionLeftSide::leftmostbefore checking forFunctionExpression/ClassExpression— mirroring the existingCallExpressionhandler, which already does this for the sameexport defaultscenario. This matches the ECMAScript grammar:export defaultforbids a right-hand side whose leading token isfunction/async function/class, so parens are needed exactly when the leftmost leaf is a function/class expression.Tests
tests/fixtures/ts/parenthesis/export-default-as.ts.oxc_formatterfixture suite passes (290 tests).export-defaultcontexts get no parens;function/classas leftmost (even chained or via member access) keep them.🤖 Generated with Claude Code