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

convertToEs6Module: Avoid replacing entire function #22507

Merged
3 commits merged into from
Apr 16, 2018
Merged

Conversation

ghost
Copy link

@ghost ghost commented Mar 13, 2018

Fixes #22492
Will rebase once #22361 is in.

@ghost ghost changed the title Convert exports dot x equals convertToEs6Module: Avoid replacing entire function Mar 13, 2018
@ghost ghost mentioned this pull request Mar 20, 2018
@ghost ghost force-pushed the convertExportsDotXEquals branch 4 times, most recently from 3fde583 to 9e99f5d Compare April 6, 2018 22:36
@ghost ghost requested a review from amcasey April 6, 2018 23:22
@ghost ghost force-pushed the convertExportsDotXEquals branch from 9e99f5d to 5c13eaf Compare April 6, 2018 23:22
Copy link
Member

@amcasey amcasey left a comment

Choose a reason for hiding this comment

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

Minor concerns.

////}

// TODO: GH#22492 Should be a able access `exports.z` inside `exports.f`

verify.codeFix({
description: "Convert to ES6 module",
// TODO: GH#22492
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this the bug that this change fixes?

Copy link
Author

Choose a reason for hiding this comment

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

👍

if (lparen) {
// `() => {}` --> `function f() {}`
this.insertNodesAt(sourceFile, lparen.getStart(sourceFile), [createToken(SyntaxKind.FunctionKeyword), createIdentifier(name)], { joiner: " " });
this.deleteNode(sourceFile, arrow);
Copy link
Member

Choose a reason for hiding this comment

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

Will this destroy any trivia? Does it matter?

Copy link
Author

Choose a reason for hiding this comment

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

This is a bug already, see #23373

const arrow = findChildOfKind(node, SyntaxKind.EqualsGreaterThanToken, sourceFile)!;
const lparen = findChildOfKind(node, SyntaxKind.OpenParenToken, sourceFile);
if (lparen) {
// `() => {}` --> `function f() {}`
Copy link
Member

Choose a reason for hiding this comment

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

Does this work for () => true?

Copy link
Member

Choose a reason for hiding this comment

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

(Or is there some reason that's impossible here?)

Copy link
Author

Choose a reason for hiding this comment

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

See if (node.body.kind !== SyntaxKind.Block) below

}
else {
// `x => {}` -> `function f(x) {}`
this.insertNodesAt(sourceFile, first(node.parameters).getStart(sourceFile), [createToken(SyntaxKind.FunctionKeyword), createIdentifier(name + "(")], { joiner: " " });
Copy link
Member

Choose a reason for hiding this comment

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

The lparen is in the identifier because we don't want a space between them? If we're going to do that, why not just insert raw text?

Copy link
Author

Choose a reason for hiding this comment

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

Good idea, when I first wrote this we didn't have insertText.

else {
// `x => {}` -> `function f(x) {}`
this.insertNodesAt(sourceFile, first(node.parameters).getStart(sourceFile), [createToken(SyntaxKind.FunctionKeyword), createIdentifier(name + "(")], { joiner: " " });
// Replaceing full range of arrow to get rid of the leading space -- replace ` =>` with `)`
Copy link
Member

Choose a reason for hiding this comment

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

Typo "replaceing"

Copy link
Author

Choose a reason for hiding this comment

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

👍

// `x => {}` -> `function f(x) {}`
this.insertNodesAt(sourceFile, first(node.parameters).getStart(sourceFile), [createToken(SyntaxKind.FunctionKeyword), createIdentifier(name + "(")], { joiner: " " });
// Replaceing full range of arrow to get rid of the leading space -- replace ` =>` with `)`
this.replaceRange(sourceFile, arrow, createToken(SyntaxKind.CloseParenToken));
Copy link
Member

Choose a reason for hiding this comment

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

This will definitely clobber trivia.

Copy link
Author

Choose a reason for hiding this comment

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

@@ -303,7 +300,27 @@ namespace ts.codefix {
return makeExportDeclaration([createExportSpecifier(/*propertyName*/ undefined, "default")], moduleSpecifier);
}

function convertExportsDotXEquals(name: string | undefined, exported: Expression): Statement {
function convertExportsDotXEquals({ left, right, parent }: BinaryExpression & { left: PropertyAccessExpression }, sourceFile: SourceFile, changes: textChanges.ChangeTracker): void {
Copy link
Member

Choose a reason for hiding this comment

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

Could "ExportsDotXEquals" be rephrased as "ExportsPropertyAssignment"?

Copy link
Author

Choose a reason for hiding this comment

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

👍

const name = left.name.text;
if ((isFunctionExpression(right) || isArrowFunction(right) || isClassExpression(right)) && (!right.name || right.name.text === name)) {
// `exports.f = function() {}` -> `export function f() {}` -- Replace `exports.f = ` with `export `, and insert the name after `function`.
changes.replaceRange(sourceFile, { pos: left.getStart(sourceFile), end: right.getStart(sourceFile) }, createToken(SyntaxKind.ExportKeyword), { suffix: " " });
Copy link
Member

Choose a reason for hiding this comment

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

Will this clobber trivia on the LHS of the assignment?

Copy link
Author

Choose a reason for hiding this comment

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

if (!right.name) changes.insertName(sourceFile, right, name);

const semi = findChildOfKind(parent, SyntaxKind.SemicolonToken, sourceFile);
if (semi) changes.deleteNode(sourceFile, semi, { useNonAdjustedEndPosition: true });
Copy link
Member

Choose a reason for hiding this comment

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

Will this clobber trailing trivia on the semicolon?

Copy link
Author

Choose a reason for hiding this comment

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

@ghost ghost merged commit aac9ef5 into master Apr 16, 2018
@ghost ghost deleted the convertExportsDotXEquals branch April 16, 2018 20:16
@microsoft microsoft locked and limited conversation to collaborators Jul 26, 2018
This pull request was closed.
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.

None yet

2 participants