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

Ensure that the comma is removed when all named imports are removed via moveToFile #32663

Merged
merged 1 commit into from
Aug 2, 2019

Conversation

orta
Copy link
Contributor

@orta orta commented Aug 1, 2019

Fixes #31195

This one feels like I'm taking a shortcut, but it was definitely doing the right thing before. When I looked in ast-explorer what import defaultD, from "./has-exports"; looks, like the comma comes from a NamedImports with no elements inside a ImportClause- removing the comma completely removes the NamedImports node.

This AST change is actually happening in the current refactor as-is, however, deleting just that node doesn't include the comma (maybe the fix is instead to make the span for that node different?) but this will remove the comma either way. I don't believe there are ever cases where you need that comma.

@@ -351,6 +351,8 @@ namespace ts.refactor {
if (namedBindings) {
if (namedBindingsUnused) {
changes.delete(sourceFile, namedBindings);
// Also include the ", ":
changes.replaceRangeWithText(sourceFile, { pos: namedBindings.pos - 1, end: namedBindings.pos }, "");
Copy link
Member

Choose a reason for hiding this comment

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

I would probably opt for

changes.replaceNode(
  sourceFile,
  importDecl.importClause,
  updateImportClause(importDecl.importClause, name, /*namedBindings*/ undefined));

(to replace both lines in this if)

Copy link
Member

@andrewbranch andrewbranch Aug 1, 2019

Choose a reason for hiding this comment

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

I think the issue is that the comma token is a child of the ImportClause, and TextChanges only does the exact thing you tell it to do without being smart about the context it’s working in. As you discovered, deleting the named bindings node just removes the text from pos to end, which doesn’t include the comma. So really, it's the ImportClause that needs to change—it needs to drop its named bindings node and its comma token. Replacing it with a new one will re-print its contents, which lets the emitter be smart about whether a comma is needed without requiring manual intervention.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Perfect, yep - moving up the AST makes a lot of sense.

That change makes a lot of sense. Thanks 👯‍♂

@orta orta merged commit 4a26271 into microsoft:master Aug 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"Move to a new file" quick fix won't properly separate default and named imports and leaves dangling comma
3 participants