Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(js_formatter): Only parenthesize default-export function cast expressions #1023

Merged
merged 1 commit into from
Dec 3, 2023

Conversation

faultyserver
Copy link
Contributor

Summary

Fixes #914. This is the last case of that issue (the middle one).

Default-exported cast expressions for functions require parentheses to disambiguate between a cast expression for the function and a default function declaration export. As an example:

export default function foo() {} as typeof console.log;
// is actually
export default function foo() {};
as;
typeof console.log;
// because the declaration statement ends, and then `as` is considered a new statement

In this case parentheses force the statement to be interpreted as a default export of a cast, rather than a declaration:

export default (function foo() {} as typeof console.log);

But (seemingly) all other kinds of default export expressions are able to be written without parentheses, as Prettier prints them out that way. So this PR implements a new check for when the parent of the cast is an export default and only adds parentheses for function expressions then, omitting them in every other case.

All other types of parent expressions are unaffected.

Test Plan

Added some needs_parentheses tests in as_expressions.rs to ensure these cases are hit correctly.

Copy link

netlify bot commented Dec 2, 2023

Deploy Preview for rad-torte-839a59 canceled.

Name Link
🔨 Latest commit d299df9
🔍 Latest deploy log https://app.netlify.com/sites/rad-torte-839a59/deploys/656bc02ec8064e00081648a1

@github-actions github-actions bot added A-Formatter Area: formatter L-JavaScript Language: JavaScript and super languages labels Dec 2, 2023
@faultyserver faultyserver changed the title fix(js_formatter): Parenthesized default-export cast expressions fix(js_formatter): Parenthesize default-export function cast expressions Dec 2, 2023
@faultyserver faultyserver changed the title fix(js_formatter): Parenthesize default-export function cast expressions fix(js_formatter): Only parenthesize default-export function cast expressions Dec 2, 2023
@ematipico ematipico self-requested a review December 3, 2023 10:57
Copy link
Member

@Conaclos Conaclos left a comment

Choose a reason for hiding this comment

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

Looks good to me!

@Conaclos Conaclos merged commit c890be7 into main Dec 3, 2023
17 checks passed
@Conaclos Conaclos deleted the faulty/export-function-parens branch December 3, 2023 12:23
yossydev pushed a commit to yossydev/biome that referenced this pull request Dec 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Formatter Area: formatter L-JavaScript Language: JavaScript and super languages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

📝 formatting compat issues
2 participants