pzavolinsky / ts-unused-exports

ts-unused-exports finds unused exported symbols in your Typescript project
MIT License
738 stars 46 forks source link

False positive on re-export #216

Closed dheerajvs closed 1 year ago

dheerajvs commented 2 years ago

I get false positives when a symbol is re-exported multiple times.

Repo to reproduce: https://github.com/dheerajvs/ts-unused-exports-test

Screenshot 2022-05-10 at 1 17 08 PM
$ npm test

> ts-unused-exports-test@1.0.0 test
> ts-unused-exports tsconfig.json

1 module with unused exports
ts-unused-exports-test/src/a/index.ts: *:/ts-unused-exports-test/src/a/a/a
mrseanryan commented 1 year ago

Thank you for reporting, and for the nice clear example!

I've made a PR that adds this to the i-tests.

But for a fix, need some time to think about ...

Of course if anyone wants to have a go, you are welcome :)

mrseanryan commented 1 year ago

Update: 9.0.4 has improved support for re-exports, especially involving export *.

The particular case above is covered with tests.

note: for cases where a file is 'exporting too much' then that is still flagged as an 'unused export'.

For example:

Scenario: export * from ./a1/a2/a.ts - import skipping the mid-level - import from innermost a.ts
  Given file "a1/a2/a.ts" is
    """
    export const A = 1;
    export const A_innermost_unused = 1;
    """
  And file "a1/a2/index.ts" is
    """
    export * from "./a";
    """
  And file "a1/a.ts" is export * from './a';
  And file "a1/index.ts" is
    """
    // skips the mid-level - imports directly from a1/a2/a.ts NOT the index, so not from an export *
    export * from "./a1/a2/a";
    export const A_unused = 1;
    """
  And file "c.ts" is import { A } from './a1';

  When analyzing "tsconfig.json"
  Then the result is { "a1/index.ts": ["A_unused"],"a1/a2/index_ts":["A","A_innermost_unused"] }

A is considered an unused export, when exported from a1/a2/index_ts.

This seems reasonable, since a1/a2/index.ts is not really used and its export or even the whole file, could be removed.

mrseanryan commented 1 year ago

Closing this - as this case is covered with 9.0.4.

If anyone finds more particular examples, it will be great if you can open more issues, with such examples, then we can continue to improve support ...

thank you for your help!