Skip to content

Commit c337f04

Browse files
committed
Ensure that the comma is removed when all named imports are removed via moveToFile - fixes microsoft#31195
1 parent b377e99 commit c337f04

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/harness/fourslash.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2430,7 +2430,7 @@ namespace FourSlash {
24302430
const oldText = this.tryGetFileContent(change.fileName);
24312431
ts.Debug.assert(!!change.isNewFile === (oldText === undefined));
24322432
const newContent = change.isNewFile ? ts.first(change.textChanges).newText : ts.textChanges.applyChanges(oldText!, change.textChanges);
2433-
assert.equal(newContent, expectedNewContent);
2433+
assert.equal(newContent, expectedNewContent, `String mis-matched in file ${change.fileName}`);
24342434
}
24352435
for (const newFileName in newFileContent) {
24362436
ts.Debug.assert(changes.some(c => c.fileName === newFileName), "No change in file", () => newFileName);

src/services/refactors/moveToNewFile.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,11 @@ namespace ts.refactor {
350350
}
351351
if (namedBindings) {
352352
if (namedBindingsUnused) {
353-
changes.delete(sourceFile, namedBindings);
353+
changes.replaceNode(
354+
sourceFile,
355+
importDecl.importClause,
356+
updateImportClause(importDecl.importClause, name, /*namedBindings*/ undefined)
357+
);
354358
}
355359
else if (namedBindings.kind === SyntaxKind.NamedImports) {
356360
for (const element of namedBindings.elements) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /has-exports.ts
4+
////
5+
//// export interface Exported { }
6+
//// const defaultExport = ""
7+
//// export default defaultExport
8+
9+
// @Filename: /31195.ts
10+
////
11+
////import defaultExport, { Exported } from "./has-exports"
12+
////console.log(defaultExport)
13+
////[|export const bar = (logger: Exported) => 0;|]
14+
15+
verify.moveToNewFile({
16+
newFileContents: {
17+
"/31195.ts": `
18+
import defaultExport from "./has-exports"
19+
console.log(defaultExport)
20+
`,
21+
22+
"/bar.ts":
23+
`import { Exported } from "./has-exports";
24+
export const bar = (logger: Exported) => 0;
25+
`,
26+
}
27+
});

0 commit comments

Comments
 (0)