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

Removing the last named export creates a star export #1551

Open
kosciolek opened this issue Jun 28, 2024 · 4 comments
Open

Removing the last named export creates a star export #1551

kosciolek opened this issue Jun 28, 2024 · 4 comments
Labels

Comments

@kosciolek
Copy link

Describe the bug

Version: 23.0.0

Calling namedExport.remove() on the last named export in a declaration inserts an export * from 'module'; , instead of removing the declaration.

To Reproduce

import { Project } from "ts-morph";

(async () => {
  const project = new Project();

  project.addSourceFileAtPath("test.ts");

  for (const file of project.getSourceFiles()) {
    for (const declaration of file.getExportDeclarations()) {
      for (const namedExport of declaration.getNamedExports()) {
        namedExport.remove();
      }
    }
  }

  await project.save();
})();

Input:

export { a } from "./b";

Output:

export * from "./b";

Expected behavior

An empty file (the export declaration should be simply removed).

@kosciolek
Copy link
Author

I can contribute a bugfix, if someone confirms this really is a bug and not intended

@dsherret dsherret added the bug label Jun 28, 2024
@dsherret
Copy link
Owner

It should go to:

export { } from "./b";

Someone may go to add a named export after removing the last one.

@kosciolek
Copy link
Author

kosciolek commented Jun 28, 2024

@dsherret I believe there should be 2 methods then, since this behavior is easy to miss
remove and removeAndRemoveDeclarationIfEmpty, or removeWithDeclaration

WDYT?

@dsherret
Copy link
Owner

dsherret commented Jun 28, 2024

There's already an exportDeclaration.remove(). I don't think we need so many methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants