From c337f046fb8b11eccb0366765bf66e3feebc9b34 Mon Sep 17 00:00:00 2001 From: Orta Therox Date: Thu, 1 Aug 2019 14:08:58 -0400 Subject: [PATCH] Ensure that the comma is removed when all named imports are removed via moveToFile - fixes #31195 --- src/harness/fourslash.ts | 2 +- src/services/refactors/moveToNewFile.ts | 6 ++++- .../moveToNewFile_cleanUpLastNamedImport.ts | 27 +++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 tests/cases/fourslash/moveToNewFile_cleanUpLastNamedImport.ts diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index f12506be2fb27..11271a9e61a3b 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -2430,7 +2430,7 @@ namespace FourSlash { const oldText = this.tryGetFileContent(change.fileName); ts.Debug.assert(!!change.isNewFile === (oldText === undefined)); const newContent = change.isNewFile ? ts.first(change.textChanges).newText : ts.textChanges.applyChanges(oldText!, change.textChanges); - assert.equal(newContent, expectedNewContent); + assert.equal(newContent, expectedNewContent, `String mis-matched in file ${change.fileName}`); } for (const newFileName in newFileContent) { ts.Debug.assert(changes.some(c => c.fileName === newFileName), "No change in file", () => newFileName); diff --git a/src/services/refactors/moveToNewFile.ts b/src/services/refactors/moveToNewFile.ts index ad60a6da5fc5e..8da05ee55e1fa 100644 --- a/src/services/refactors/moveToNewFile.ts +++ b/src/services/refactors/moveToNewFile.ts @@ -350,7 +350,11 @@ namespace ts.refactor { } if (namedBindings) { if (namedBindingsUnused) { - changes.delete(sourceFile, namedBindings); + changes.replaceNode( + sourceFile, + importDecl.importClause, + updateImportClause(importDecl.importClause, name, /*namedBindings*/ undefined) + ); } else if (namedBindings.kind === SyntaxKind.NamedImports) { for (const element of namedBindings.elements) { diff --git a/tests/cases/fourslash/moveToNewFile_cleanUpLastNamedImport.ts b/tests/cases/fourslash/moveToNewFile_cleanUpLastNamedImport.ts new file mode 100644 index 0000000000000..eb45fc77f404d --- /dev/null +++ b/tests/cases/fourslash/moveToNewFile_cleanUpLastNamedImport.ts @@ -0,0 +1,27 @@ +/// + +// @Filename: /has-exports.ts +//// +//// export interface Exported { } +//// const defaultExport = "" +//// export default defaultExport + +// @Filename: /31195.ts +//// +////import defaultExport, { Exported } from "./has-exports" +////console.log(defaultExport) +////[|export const bar = (logger: Exported) => 0;|] + +verify.moveToNewFile({ + newFileContents: { + "/31195.ts": ` +import defaultExport from "./has-exports" +console.log(defaultExport) +`, + + "/bar.ts": + `import { Exported } from "./has-exports"; +export const bar = (logger: Exported) => 0; +`, + } +});