From 3d41ded41f8b3bcf7e0277bd8554eeb9c07559e5 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Mon, 30 Dec 2024 11:52:13 -0500 Subject: [PATCH] fix: removing last named import/export should not remove or change declaration (#1599) --- .../ts-morph/src/compiler/ast/module/ExportSpecifier.ts | 8 +++----- .../ts-morph/src/compiler/ast/module/ImportSpecifier.ts | 2 +- .../src/tests/compiler/ast/module/exportSpecifierTests.ts | 6 +++--- .../src/tests/compiler/ast/module/importSpecifierTests.ts | 2 +- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/packages/ts-morph/src/compiler/ast/module/ExportSpecifier.ts b/packages/ts-morph/src/compiler/ast/module/ExportSpecifier.ts index 86dab091e..872a69996 100644 --- a/packages/ts-morph/src/compiler/ast/module/ExportSpecifier.ts +++ b/packages/ts-morph/src/compiler/ast/module/ExportSpecifier.ts @@ -192,14 +192,12 @@ export class ExportSpecifier extends ExportSpecifierBase { */ remove() { const exportDeclaration = this.getExportDeclaration(); - const exports = exportDeclaration.getNamedExports(); + const namedExports = exportDeclaration.getNamedExports(); - if (exports.length > 1) + if (namedExports.length > 1 || exportDeclaration.getNamespaceExport() == null) removeCommaSeparatedChild(this); - else if (exportDeclaration.hasModuleSpecifier()) - exportDeclaration.toNamespaceExport(); else - exportDeclaration.remove(); + exportDeclaration.toNamespaceExport(); } /** diff --git a/packages/ts-morph/src/compiler/ast/module/ImportSpecifier.ts b/packages/ts-morph/src/compiler/ast/module/ImportSpecifier.ts index 2d71fa07a..95f8dd95b 100644 --- a/packages/ts-morph/src/compiler/ast/module/ImportSpecifier.ts +++ b/packages/ts-morph/src/compiler/ast/module/ImportSpecifier.ts @@ -168,7 +168,7 @@ export class ImportSpecifier extends ImportSpecifierBase { const importDeclaration = this.getImportDeclaration(); const namedImports = importDeclaration.getNamedImports(); - if (namedImports.length > 1) + if (namedImports.length > 1 || importDeclaration.getNamespaceImport() == null && importDeclaration.getDefaultImport() == null) removeCommaSeparatedChild(this); else importDeclaration.removeNamedImports(); diff --git a/packages/ts-morph/src/tests/compiler/ast/module/exportSpecifierTests.ts b/packages/ts-morph/src/tests/compiler/ast/module/exportSpecifierTests.ts index b8997ad5b..a08d1945f 100644 --- a/packages/ts-morph/src/tests/compiler/ast/module/exportSpecifierTests.ts +++ b/packages/ts-morph/src/tests/compiler/ast/module/exportSpecifierTests.ts @@ -339,11 +339,11 @@ describe("ExportSpecifier", () => { } it("should change to a namespace import when there's only one to remove and a module specifier exists", () => { - doTest(`export {name} from "./test";`, "name", `export * from "./test";`); + doTest(`export {name} from "./test";`, "name", `export {} from "./test";`); }); - it("should remove the export declaration when there's only one to remove and no module specifier exists", () => { - doTest(`export {name};`, "name", ``); + it("should not remove the export declaration when there's only one to remove and no module specifier exists", () => { + doTest(`export {name};`, "name", `export {};`); }); it("should remove the named import when it's the first", () => { diff --git a/packages/ts-morph/src/tests/compiler/ast/module/importSpecifierTests.ts b/packages/ts-morph/src/tests/compiler/ast/module/importSpecifierTests.ts index 3404c5acd..8c5deb564 100644 --- a/packages/ts-morph/src/tests/compiler/ast/module/importSpecifierTests.ts +++ b/packages/ts-morph/src/tests/compiler/ast/module/importSpecifierTests.ts @@ -221,7 +221,7 @@ describe("ImportSpecifier", () => { }); it("should remove the named imports when only one exist", () => { - doTest(`import {Name1} from "module-name";`, "Name1", `import "module-name";`); + doTest(`import {Name1} from "module-name";`, "Name1", `import {} from "module-name";`); }); it("should remove the named imports when only one exists and a default import exist", () => {