Skip to content

Commit

Permalink
Ensure that the comma is removed when all named imports are removed v…
Browse files Browse the repository at this point in the history
…ia moveToFile - fixes #31195
  • Loading branch information
orta committed Aug 1, 2019
1 parent b377e99 commit c337f04
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/harness/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion src/services/refactors/moveToNewFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
27 changes: 27 additions & 0 deletions tests/cases/fourslash/moveToNewFile_cleanUpLastNamedImport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/// <reference path='fourslash.ts' />

// @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;
`,
}
});

0 comments on commit c337f04

Please sign in to comment.