Skip to content

Parenthesize export assignments if needed#19590

Merged
weswigham merged 4 commits into
masterfrom
export-default-parenthesize
Oct 31, 2017
Merged

Parenthesize export assignments if needed#19590
weswigham merged 4 commits into
masterfrom
export-default-parenthesize

Conversation

@weswigham
Copy link
Copy Markdown
Member

Fixes #19574.

@weswigham weswigham requested review from mhegazy and rbuckton October 30, 2017 22:10
Comment thread src/compiler/factory.ts Outdated
node.modifiers = asNodeArray(modifiers);
node.isExportEquals = isExportEquals;
node.expression = expression;
node.expression = parenthesizeBinaryOperand(SyntaxKind.EqualsToken, expression, /*isLeftSideOfBinary*/ false, /*leftOperand*/ undefined);
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

done.

Comment thread src/compiler/factory.ts Outdated
node.modifiers = asNodeArray(modifiers);
node.isExportEquals = isExportEquals;
node.expression = expression;
node.expression = parenthesizeBinaryOperand(SyntaxKind.EqualsToken, expression, /*isLeftSideOfBinary*/ false, /*leftOperand*/ undefined);
Copy link
Copy Markdown
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

Comment thread src/compiler/factory.ts
*/
export function parenthesizeDefaultExpression(e: Expression) {
const check = skipPartiallyEmittedExpressions(e);
return (check.kind === SyntaxKind.ClassExpression ||
Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown
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