Skip to content

Parenthesize export assignments if needed #19590

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

Merged
merged 4 commits into from
Oct 31, 2017
Merged

Conversation

weswigham
Copy link
Member

Fixes #19574.

@weswigham weswigham requested review from rbuckton and mhegazy October 30, 2017 22:10
@@ -1982,7 +1982,7 @@ namespace ts {
node.decorators = asNodeArray(decorators);
node.modifiers = asNodeArray(modifiers);
node.isExportEquals = isExportEquals;
node.expression = expression;
node.expression = parenthesizeBinaryOperand(SyntaxKind.EqualsToken, expression, /*isLeftSideOfBinary*/ false, /*leftOperand*/ undefined);
Copy link
Member Author

Choose a reason for hiding this comment

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

As far as I can tell, export default <expr> binds as tightly as export = <expr>, so this seems appropriate.

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, I think export default may need its own parenthesization logic. Per the spec, export default accepts AssigmentExpression but has a lookahead restriction for function, async function, and class.

Basically, that means you need to parenthesize in the following cases:

  • BinaryExpression of CommaToken
  • CommaList (synthetic list of multiple comma expressions)
  • FunctionExpression
  • ClassExpression

Copy link
Member Author

Choose a reason for hiding this comment

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

done.

@@ -1982,7 +1982,7 @@ namespace ts {
node.decorators = asNodeArray(decorators);
node.modifiers = asNodeArray(modifiers);
node.isExportEquals = isExportEquals;
node.expression = expression;
node.expression = parenthesizeBinaryOperand(SyntaxKind.EqualsToken, expression, /*isLeftSideOfBinary*/ false, /*leftOperand*/ undefined);
Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, I think export default may need its own parenthesization logic. Per the spec, export default accepts AssigmentExpression but has a lookahead restriction for function, async function, and class.

Basically, that means you need to parenthesize in the following cases:

  • BinaryExpression of CommaToken
  • CommaList (synthetic list of multiple comma expressions)
  • FunctionExpression
  • ClassExpression

export function parenthesizeDefaultExpression(e: Expression) {
const check = skipPartiallyEmittedExpressions(e);
return (check.kind === SyntaxKind.ClassExpression ||
check.kind === SyntaxKind.FunctionExpression ||

Choose a reason for hiding this comment

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

Is parenthesizing FunctionExpression and ClassExpression enough to stop them from being seen as HoistableDeclaration/ClassDeclaration? 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes. Once you add parens, you are no longer in a declaration.

Copy link
Member Author

Choose a reason for hiding this comment

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

Should be. Anything that doesn't work that way is parsing incorrectly.

@weswigham weswigham merged commit c2aa13d into master Oct 31, 2017
@weswigham weswigham deleted the export-default-parenthesize branch October 31, 2017 01:23
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants