Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: removing last named import/export should not remove or change declaration #1599

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading