eslint-import-resolver-typescript icon indicating copy to clipboard operation
eslint-import-resolver-typescript copied to clipboard

Support declaring internal module regex or respect import/internal-regex setting

Open harunurhan opened this issue 2 years ago • 6 comments

Resolver performs way too slow on a medium to large TS project where every local/internal import is absolute from the project root, since https://github.com/import-js/eslint-import-resolver-typescript/blob/71b23a206514842fef70a99220e5ffb1d6da2a0e/src/index.ts#L276 optimization branch is not used.

It would be useful to allow users to specify internalRegex similar to https://github.com/import-js/eslint-plugin-import/tree/a89eadf3247af844da6b0f9e7bca7690777bf665#importinternal-regex.

Worth mentioning that using above import/internal-regex did speed up the linting times, but unexpectedly I have seen more improvements when implemented a custom resolver like below using the same regexp, so perhaps there is a bug (or misunderstanding) there and no new option is needed, just addressing that one.

export function resolve(
    importPath: string,
    importerFileAbsolutePath: string,
    tsResolverOptions: TsResolverOptions
): ResolveResult {
    if (internalModuleRegexp.test(importPath)) {
        const absoluteImportPath = path.resolve(`./${importPath}`);
        return {
            found: true,
            path: absoluteImportPath,
        };
    }

    return eslintImportResolverTypescript.resolve(importPath, importerFileAbsolutePath, tsResolverOptions);
}

harunurhan avatar May 24 '23 12:05 harunurhan

I don't quite follow you meanings, why internalModuleRegexp matched means the related absoluteImportPath as resolved.

JounQin avatar May 24 '23 13:05 JounQin

This is a simplified implementation that won't work for every usecase perhaps, sorry for confusion, but what I mean:

Imagine you have a large repo like this

- foo
  - nested
    -  file1.ts
- bar
  - nested
    - file2.ts 
- package.json
- .eslintrc.json
- tsconfig.json

And file1.ts is using an absolute (to the tsconfig) import path.

import { something } from "bar/nested/file2"

For this import we are using enchanced-resolve, however we could just resolve it faster since we have foo and bar are our top level folders and we use absolute import paths.

harunurhan avatar May 24 '23 13:05 harunurhan

however we could just resolve it faster since we have foo and bar are our top level folders and we use absolute import paths

This seems not always true?

For a common mono repo, the structure would be:

- packages
  - foo
    - nested
      - file1.ts
  - bar
    - nested
      - file2.ts
- package.json

How can we resolve it faster for bar/nested/file2 in this case?

JounQin avatar May 24 '23 15:05 JounQin

if we base tsconfig path while resolving not the package.json, this would work right?, though complicated when there are multiple projects linting via same config. Also we could ask for users to provide path to root folder 🤷‍♂️

harunurhan avatar May 24 '23 16:05 harunurhan

I still can't fully get your point, but a PR for showing case would be appreciated. 👍

JounQin avatar May 24 '23 16:05 JounQin

Perhaps it's also hard to implement in a generic way, that's why my explanation doesn't make a lot of sense but I'll give it a try when I have time.

harunurhan avatar May 24 '23 19:05 harunurhan