From f52bf2d2cc5177031cb49d2a90814958d4b00f7a Mon Sep 17 00:00:00 2001 From: JounQin Date: Sun, 3 Jul 2022 13:58:12 +0800 Subject: [PATCH 1/7] refactor: support custom extensions on resolving --- src/index.ts | 52 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/src/index.ts b/src/index.ts index 6e9c746..ab085cf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -95,7 +95,14 @@ export function resolve( found: boolean path?: string | null } { - const opts: ResolveOptions & TsResolverOptions = { + const opts: Required< + Pick< + ResolveOptions, + 'conditionNames' | 'extensions' | 'mainFields' | 'useSyncFileSystemCalls' + > + > & + ResolveOptions & + TsResolverOptions = { ...options, extensions: options?.extensions ?? defaultExtensions, mainFields: options?.mainFields ?? defaultMainFields, @@ -122,7 +129,7 @@ export function resolve( initMappers(opts) - const mappedPath = getMappedPath(source, file, true) + const mappedPath = getMappedPath(source, file, opts.extensions, true) if (mappedPath) { log('matched ts path:', mappedPath) } @@ -250,12 +257,15 @@ const isFile = (path?: string | undefined): path is string => { /** * @param {string} source the module to resolve; i.e './some-module' * @param {string} file the importing file's full path; i.e. '/usr/local/bin/file.js' + * @param {string[]} extensions the extensions to try + * @param {boolean} retry should retry on failed to resolve * @returns The mapped path of the module or undefined */ // eslint-disable-next-line sonarjs/cognitive-complexity function getMappedPath( source: string, file: string, + extensions = defaultExtensions, retry?: boolean, ): string | undefined { let paths: string[] | undefined = [] @@ -269,9 +279,7 @@ function getMappedPath( paths = mappers! .map(mapper => mapper?.(source).map(item => - path.extname(item) - ? item - : ['ts', 'tsx', '.d.ts', 'js'].map(ext => `${item}.${ext}`), + ['', ...extensions].map(ext => `${item}${ext}`), ), ) .flat(2) @@ -283,28 +291,28 @@ function getMappedPath( const jsExt = path.extname(source) const tsExt = jsExt.replace('js', 'ts') const basename = source.replace(JS_EXT_PATTERN, '') - return ( + + const resolved = getMappedPath(basename + tsExt, file) || - getMappedPath(source + '/index.ts', file) || - getMappedPath(source + '/index.tsx', file) || - getMappedPath(source + '/index.d.ts', file) || getMappedPath( basename + '.d' + (tsExt === '.tsx' ? '.ts' : tsExt), file, - ) || - getMappedPath(source + '/index.js', file) - ) + ) + + if (resolved) { + return resolved + } + } + + for (const ext of extensions) { + const resolved = + getMappedPath(source + ext, file) || + getMappedPath(source + `/index${ext}`, file) + + if (resolved) { + return resolved + } } - return ( - getMappedPath(source + '.ts', file) || - getMappedPath(source + '.tsx', file) || - getMappedPath(source + '.js', file) || - getMappedPath(source + '.d.ts', file) || - getMappedPath(source + '/index.ts', file) || - getMappedPath(source + '/index.tsx', file) || - getMappedPath(source + '/index.d.ts', file) || - getMappedPath(source + '/index.js', file) - ) } if (paths.length > 1) { From 5b0185fe6e6a35a42963185781aff826064b5004 Mon Sep 17 00:00:00 2001 From: JounQin Date: Sun, 3 Jul 2022 14:02:07 +0800 Subject: [PATCH 2/7] chore: do not try extensions which already has js extension --- src/index.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index ab085cf..1e5778d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -287,7 +287,8 @@ function getMappedPath( } if (retry && paths.length === 0) { - if (JS_EXT_PATTERN.test(source)) { + const isJs = JS_EXT_PATTERN.test(source) + if (isJs) { const jsExt = path.extname(source) const tsExt = jsExt.replace('js', 'ts') const basename = source.replace(JS_EXT_PATTERN, '') @@ -306,7 +307,7 @@ function getMappedPath( for (const ext of extensions) { const resolved = - getMappedPath(source + ext, file) || + (isJs ? null : getMappedPath(source + ext, file)) || getMappedPath(source + `/index${ext}`, file) if (resolved) { From 2369af01b3d341781becb45191c5d342da8ebc89 Mon Sep 17 00:00:00 2001 From: JounQin Date: Sun, 3 Jul 2022 14:03:29 +0800 Subject: [PATCH 3/7] Create late-papayas-smile.md --- .changeset/late-papayas-smile.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/late-papayas-smile.md diff --git a/.changeset/late-papayas-smile.md b/.changeset/late-papayas-smile.md new file mode 100644 index 0000000..8bcffd2 --- /dev/null +++ b/.changeset/late-papayas-smile.md @@ -0,0 +1,5 @@ +--- +"eslint-import-resolver-typescript": minor +--- + +refactor: support custom extensions on resolving From 24034c97105a28c70ae050724356e4e4853f3d0f Mon Sep 17 00:00:00 2001 From: JounQin Date: Sun, 3 Jul 2022 14:13:51 +0800 Subject: [PATCH 4/7] feat: try extensionless file by default --- src/index.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index 1e5778d..7ce79f2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -78,6 +78,9 @@ export interface TsResolverOptions const fileSystem = fs as FileSystem +const JS_EXT_PATTERN = /\.(?:[cm]js|jsx?)$/ +const RELATIVE_PATH_PATTERN = /^\.{1,2}(?:\/.*)?$/ + let mappersBuildForOptions: TsResolverOptions let mappers: Array<((specifier: string) => string[]) | null> | undefined let resolver: Resolver @@ -147,7 +150,7 @@ export function resolve( // naive attempt at @types/* resolution, // if path is neither absolute nor relative if ( - (/\.jsx?$/.test(foundNodePath!) || + (JS_EXT_PATTERN.test(foundNodePath!) || (opts.alwaysTryTypes && !foundNodePath)) && !/^@types[/\\]/.test(source) && !path.isAbsolute(source) && @@ -238,9 +241,6 @@ function removeQuerystring(id: string) { return id } -const JS_EXT_PATTERN = /\.(?:[cm]js|jsx?)$/ -const RELATIVE_PATH_PATTERN = /^\.{1,2}(?:\/.*)?$/ - /** Remove .js or .jsx extension from module id. */ function removeJsExtension(id: string) { return id.replace(JS_EXT_PATTERN, '') @@ -268,6 +268,8 @@ function getMappedPath( extensions = defaultExtensions, retry?: boolean, ): string | undefined { + extensions = ['', ...extensions] + let paths: string[] | undefined = [] if (RELATIVE_PATH_PATTERN.test(source)) { @@ -278,9 +280,7 @@ function getMappedPath( } else { paths = mappers! .map(mapper => - mapper?.(source).map(item => - ['', ...extensions].map(ext => `${item}${ext}`), - ), + mapper?.(source).map(item => extensions.map(ext => `${item}${ext}`)), ) .flat(2) .filter(isFile) From a9ad8b44cd999bc88dc2fbbd6f6a0249c8102ff3 Mon Sep 17 00:00:00 2001 From: JounQin Date: Sun, 3 Jul 2022 14:14:41 +0800 Subject: [PATCH 5/7] Create yellow-mice-build.md --- .changeset/yellow-mice-build.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/yellow-mice-build.md diff --git a/.changeset/yellow-mice-build.md b/.changeset/yellow-mice-build.md new file mode 100644 index 0000000..8844f8a --- /dev/null +++ b/.changeset/yellow-mice-build.md @@ -0,0 +1,5 @@ +--- +"eslint-import-resolver-typescript": minor +--- + +feat: try extensionless file by default From 48458218d9f9839ce3cb9ed87ac95527f02809e5 Mon Sep 17 00:00:00 2001 From: JounQin Date: Sun, 3 Jul 2022 14:18:41 +0800 Subject: [PATCH 6/7] chore: improve tsResolve comments --- src/index.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 7ce79f2..4d89a6f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -189,17 +189,17 @@ function resolveExtension(id: string) { return } - if (id.endsWith('.mjs')) { + if (id.endsWith('.cjs')) { return { path: idWithoutJsExt, - extensions: ['.mts', '.d.mts'], + extensions: ['.cts', '.d.cts'], } } - if (id.endsWith('.cjs')) { + if (id.endsWith('.mjs')) { return { path: idWithoutJsExt, - extensions: ['.cts', '.d.cts'], + extensions: ['.mts', '.d.mts'], } } @@ -210,7 +210,7 @@ function resolveExtension(id: string) { /** * Like `sync` from `resolve` package, but considers that the module id - * could have a .js or .jsx extension. + * could have a .cjs, .mjs, .js or .jsx extension. */ function tsResolve( source: string, From e71532465402a67d18f5d5abb4c8404a6d537f26 Mon Sep 17 00:00:00 2001 From: JounQin Date: Sun, 3 Jul 2022 14:20:19 +0800 Subject: [PATCH 7/7] chore: improve removeJsExtension comments --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 4d89a6f..f5694fd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -241,7 +241,7 @@ function removeQuerystring(id: string) { return id } -/** Remove .js or .jsx extension from module id. */ +/** Remove .cjs, .mjs, .js or .jsx extension from module id. */ function removeJsExtension(id: string) { return id.replace(JS_EXT_PATTERN, '') }