Skip to content

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

Merged
orta merged 1 commit into
microsoft:masterfrom
orta:fix_31195
Aug 2, 2019
Merged

Ensure that the comma is removed when all named imports are removed via moveToFile #32663
orta merged 1 commit into
microsoft:masterfrom
orta:fix_31195

Conversation

@orta
Copy link
Copy Markdown
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.

Comment thread src/services/refactors/moveToNewFile.ts Outdated
if (namedBindingsUnused) {
changes.delete(sourceFile, namedBindings);
// Also include the ", ":
changes.replaceRangeWithText(sourceFile, { pos: namedBindings.pos - 1, end: namedBindings.pos }, "");
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
@microsoft microsoft locked as resolved and limited conversation to collaborators Oct 21, 2025
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.

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

3 participants