Skip to content

Commit 3d41ded

Browse files
authored
fix: removing last named import/export should not remove or change declaration (#1599)
1 parent f4ed887 commit 3d41ded

File tree

4 files changed

+8
-10
lines changed

4 files changed

+8
-10
lines changed

packages/ts-morph/src/compiler/ast/module/ExportSpecifier.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,12 @@ export class ExportSpecifier extends ExportSpecifierBase<ts.ExportSpecifier> {
192192
*/
193193
remove() {
194194
const exportDeclaration = this.getExportDeclaration();
195-
const exports = exportDeclaration.getNamedExports();
195+
const namedExports = exportDeclaration.getNamedExports();
196196

197-
if (exports.length > 1)
197+
if (namedExports.length > 1 || exportDeclaration.getNamespaceExport() == null)
198198
removeCommaSeparatedChild(this);
199-
else if (exportDeclaration.hasModuleSpecifier())
200-
exportDeclaration.toNamespaceExport();
201199
else
202-
exportDeclaration.remove();
200+
exportDeclaration.toNamespaceExport();
203201
}
204202

205203
/**

packages/ts-morph/src/compiler/ast/module/ImportSpecifier.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export class ImportSpecifier extends ImportSpecifierBase<ts.ImportSpecifier> {
168168
const importDeclaration = this.getImportDeclaration();
169169
const namedImports = importDeclaration.getNamedImports();
170170

171-
if (namedImports.length > 1)
171+
if (namedImports.length > 1 || importDeclaration.getNamespaceImport() == null && importDeclaration.getDefaultImport() == null)
172172
removeCommaSeparatedChild(this);
173173
else
174174
importDeclaration.removeNamedImports();

packages/ts-morph/src/tests/compiler/ast/module/exportSpecifierTests.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -339,11 +339,11 @@ describe("ExportSpecifier", () => {
339339
}
340340

341341
it("should change to a namespace import when there's only one to remove and a module specifier exists", () => {
342-
doTest(`export {name} from "./test";`, "name", `export * from "./test";`);
342+
doTest(`export {name} from "./test";`, "name", `export {} from "./test";`);
343343
});
344344

345-
it("should remove the export declaration when there's only one to remove and no module specifier exists", () => {
346-
doTest(`export {name};`, "name", ``);
345+
it("should not remove the export declaration when there's only one to remove and no module specifier exists", () => {
346+
doTest(`export {name};`, "name", `export {};`);
347347
});
348348

349349
it("should remove the named import when it's the first", () => {

packages/ts-morph/src/tests/compiler/ast/module/importSpecifierTests.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ describe("ImportSpecifier", () => {
221221
});
222222

223223
it("should remove the named imports when only one exist", () => {
224-
doTest(`import {Name1} from "module-name";`, "Name1", `import "module-name";`);
224+
doTest(`import {Name1} from "module-name";`, "Name1", `import {} from "module-name";`);
225225
});
226226

227227
it("should remove the named imports when only one exists and a default import exist", () => {

0 commit comments

Comments
 (0)