Skip to content

Commit

Permalink
fix: removing last named import/export should not remove or change de…
Browse files Browse the repository at this point in the history
…claration (#1599)
  • Loading branch information
dsherret authored Dec 30, 2024
1 parent f4ed887 commit 3d41ded
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 10 deletions.
8 changes: 3 additions & 5 deletions packages/ts-morph/src/compiler/ast/module/ExportSpecifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,12 @@ export class ExportSpecifier extends ExportSpecifierBase<ts.ExportSpecifier> {
*/
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();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export class ImportSpecifier extends ImportSpecifierBase<ts.ImportSpecifier> {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down

0 comments on commit 3d41ded

Please sign in to comment.