pzavolinsky / ts-unused-exports

ts-unused-exports finds unused exported symbols in your Typescript project

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

glob-support in excludePathsFromReport

boredland opened this issue · comments

Hi there!

I have some generated code in my project. Some of its exports are being used, some aren't. As I see it, this library has three ways to deal with this: "ignoreFiles" and "excludePathsFromReport" and not passing the files to it (either via tsconfig.files/include/exclude or by specifying the files passed to it).

My problem is:

  • I want to include the generated files in my build - excluding them in tsconfig isn't really an option
  • I want to include them in the check this library performs, because this generated code utilizes regular code

The remaining option subsequently is "excludePathsFromReport". In places in our codebase that have generated files in a single specific path, this works out quite nice. But sadly in other parts we have generated code alongside its source:

/some/path/origin.ts
/some/paths/origin.generated.ts

So if I didn't oversee something there currently isn't an option to make usage of origin.ts in origin.generated.ts count while excluding origin.generated.ts from the final report. Therefore I'd like to add glob pattern support for excludePathsFromReport (using globby or similar). Wdyt?

This could lead to patterns like this for me being allowed:

excludePathsFromReport="**/*.generated.*"

this it how I currently built this around this library:

/* eslint-disable no-console */
import analyzeTsConfig from 'ts-unused-exports';
import globby from 'globby';
import 'colors';

const excludePaths = globby.sync(['**/*.{generated,stories}.*']);

const entries = Object.entries(analyzeTsConfig('./tsconfig.json'));

const includedEntries = entries.filter((entry) => {
  const [entryPath] = entry;
  return !excludePaths.some((excludePath) => entryPath.endsWith(excludePath));
});

if (includedEntries.length > 0) {
  console.error(`${includedEntries.length} module with unused exports`.red);
  includedEntries.map((entry) => {
    const [path, occurrences] = entry;
    occurrences.map((occurrence) => {
      console.log(
        `${path}[${occurrence.location?.line ?? 0},${occurrence.location?.character ?? 0}]: ` +
          `${occurrence.exportName}`.yellow,
      );
    });
  });
  process.exit(1);
}

@boredland we could add this feature.

BUT I think we'd like to avoid a breaking change - so if the value contains ; then it would be the legacy 'split on ; and check if contains x' behavior

So:

1 - --excludePathsFromReport=math;utils -> [legacy behavior] path should not contain 'math' or 'utils'
2 - --excludePathsFromReport=.*math -> [new behavior] path should not end in 'math'

BUT in the short term, I think you can add the file name like this: (both file and folder names should work)

  • --excludePathsFromReport=origin.generated.ts;file2.generated.ts

related unit test:

https://github.com/pzavolinsky/ts-unused-exports/blob/c1c14faa4af9056ec7c013fb3f594e0198c80abb/features/exclude-paths-from-report.feature

BUT in the short term, I think you can add the file name like this: (both file and folder names should work)

the problem is, that a we have a lot of those files and new ones get added eventually. I think having this little custom script works fine for now.

hi, didn't want to create a new similar issue.
I have a case where I want to filter out similarly as @boredland some generated files but also some raw gql.ts files that are used for generating files. Instead of just ignoring it I'd like to filter them out from the results.

Tried adding --ignoreFiles argument but that didn't work.

ts-unused-exports ./tsconfig.json --ignoreFiles='^gql\\.ts$'

After some debugging noticed that the regex patterns only receive the filename without extension. This seemed unexpected and a bit frustrating as most tools usually have the filename with extension.

So the question, is that expected behaviour? Would you consider passing the extension as well?
The regex support is awesome, although a bit ugly when you have in npm scripts (not the issue with the library at all tho') as you need to escape characters. Except for the minor gripe, I have the library does the job pretty nice, thanks!

Update:
Noticed that the filter function didn't receive a file that was in the final report. I have file src/types/index.ts which consists of wildcard reexports so I absolute import it in code using import {MyType} from '@types'. But noticed that other index.ts files were found for the filtering.

Contents of the src/types/index.ts:

export * from "./graphql-generated";
export * from "./fragments";
export * from "./global";