webpro-nl / knip

✂️ Find unused files, dependencies and exports in your JavaScript and TypeScript projects. Knip it before you ship it!
https://knip.dev
ISC License
7.06k stars 177 forks source link

💡 Add option to ignore export names using a pattern #763

Open eugene-mohc opened 3 months ago

eugene-mohc commented 3 months ago

Suggest an idea for this project

It would be useful to be able to ignore certain exports by pattern or regex matching. For example I like to always export the props type of a React component in its file. It may not be used anywhere else yet, but is conveniently there for the future and reduces git diffs.

This can somewhat be achieved using these settings:

{
  "ignoreExportsUsedInFile": {
    "type": true
  }
}

However this prevents all other type exports from being reported as well, which is not ideal. The config could accept an array of patterns to ignore instead:

{
  "ignoreExportsUsedInFile": {
    "type": [".*Props"]
  }
}

This would ignore only type exports which end with "Props", for example:

              // Matches, OK      // Not used, reported
export type { ComponentNameProps, SomeOtherType }

export { ComponentName }
webpro commented 3 months ago

Valid RFC. Would likely becomes regexes for ignoreExports or ignoreExportTypes (like ignoreBinaries etc already do).

For now, you could add a simple preprocessor function to strip out anything unwanted.