Skip to content

moveToNewFile: handle namespace imports too #38377

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 1 commit into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/services/refactors/moveToNewFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ namespace ts.refactor {
case SyntaxKind.ImportEqualsDeclaration:
case SyntaxKind.ImportSpecifier:
case SyntaxKind.ImportClause:
case SyntaxKind.NamespaceImport:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about adding a comment given that NamespaceImport is in this list but NamedImports, but I'm half-guessing that it would be semi-obvious for most people, and I don't know if there's any reference for the list of syntax kinds.

Copy link
Member

Choose a reason for hiding this comment

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

I actually don't know why this is - is it because it's somehow handled by ImportClause?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@DanielRosenwasser, my guess is that one of NamedImports or ImportClause is for the whole import and the other is for each name in the import, or something similar (since the first is plural). But I don't want to add a comment with a guess... I did see that adding NamedImports doesn't break any tests, but went with the smaller change.

Copy link
Member

@DanielRosenwasser DanielRosenwasser May 7, 2020

Choose a reason for hiding this comment

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

That decision makes sense, thanks!

return true;
case SyntaxKind.VariableDeclaration:
return isVariableDeclarationInImport(decl as VariableDeclaration);
Expand Down
18 changes: 18 additions & 0 deletions tests/cases/fourslash/moveToNewFile_moveNamedImport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// <reference path='fourslash.ts' />

// @Filename: /a.ts
////import { foo as oFoo } from './other';
////[|export const x = oFoo();|]
////export const a = 0;

verify.moveToNewFile({
newFileContents: {
"/a.ts":
`export const a = 0;`,

"/x.ts":
`import { foo as oFoo } from './other';
export const x = oFoo();
`
},
});
18 changes: 18 additions & 0 deletions tests/cases/fourslash/moveToNewFile_moveNamespaceImport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// <reference path='fourslash.ts' />

// @Filename: /a.ts
////import * as o from './other';
////[|export const x = o.foo();|]
////export const a = 0;

verify.moveToNewFile({
newFileContents: {
"/a.ts":
`export const a = 0;`,

"/x.ts":
`import * as o from './other';
export const x = o.foo();
`
},
});