pzavolinsky / ts-unused-exports

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

Fix an ESM index.js/.mjs/.cjs import edge case #260

Closed jstasiak closed 1 year ago

jstasiak commented 1 year ago

In [1] a removeFileExtensionToAllowForJs() utility function has been added to help with normalizing import paths into extensionless ones in order to handle ESM imports (which have to have extensions, either .js, .mjs or .cjs).

One thing was missed though as index files are handled in a special way.

In the analyzer.ts:

const processImports = (file: File, exportMap: ExportMap): void => {
  Object.keys(file.imports).forEach((key) => {
    let ex = exportMap[removeFileExtensionToAllowForJs(key)]?.exports;

exportMap had keys with extensions and "index" removed (so that "xxx/index.ts" became "xxx" there) but paths in file.imports came from getFromText() which only handled extensionless index imports so xxx/index.js" stayed "xxx/index.js". The .js extension was then removed by removeFileExtensionToAllowForJs() but the damage was done already and "xxx" != "xxx/index".

This change brings the removeFileExtensionToAllowForJs()-like ESM-compatible behavior to getFromText().

[1] 6e506ae05ef3 ("Add .js extension support with es6 example (#255)")

mrseanryan commented 1 year ago

Thank you @jstasiak for this PR !

I made a new PR with your commit, and added small fix for the tests.

Released as 9.0.1

jstasiak commented 1 year ago

Thank you very much for taking care of this so quickly @mrseanryan, much appreciated!