diff --git a/package.json b/package.json index f2d7695..979d5c1 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "test:multipleTsconfigs": "eslint --ext ts,tsx tests/multipleTsconfigs", "test:withJsExtension": "node tests/withJsExtension/test.js && eslint --ext ts,tsx tests/withJsExtension", "test:withPaths": "eslint --ext ts,tsx tests/withPaths", + "test:withPathsAndNestedBaseUrl": "eslint --ext ts,tsx tests/withPathsAndNestedBaseUrl", "test:withQuerystring": "eslint --ext ts,tsx tests/withQuerystring", "test:withoutPaths": "eslint --ext ts,tsx tests/withoutPaths", "type-coverage": "type-coverage --cache --detail --ignore-catch --strict --update" diff --git a/src/index.ts b/src/index.ts index fe63fdb..054a1fb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -64,7 +64,7 @@ export function resolve( } initMappers(options) - const mappedPath = getMappedPath(source, file) + const mappedPath = getMappedPath(source) if (mappedPath) { log('matched ts path:', mappedPath) } @@ -153,19 +153,15 @@ function removeJsExtension(id: string) { } let mappersBuildForOptions: TsResolverOptions -let mappers: - | Array<(source: string, file: string) => string | undefined> - | undefined +let mappers: Array<(source: string) => string | undefined> | undefined /** * @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' * @returns The mapped path of the module or undefined */ -function getMappedPath(source: string, file: string) { - const paths = mappers! - .map(mapper => mapper(source, file)) - .filter(path => !!path) +function getMappedPath(source: string) { + const paths = mappers!.map(mapper => mapper(source)).filter(path => !!path) if (paths.length > 1) { log('found multiple matching ts paths:', paths) @@ -230,12 +226,7 @@ function initMappers(options: TsResolverOptions) { configLoaderResult.paths, ) - return (source: string, file: string) => { - // exclude files that are not part of the config base url - if (!file.includes(configLoaderResult.absoluteBaseUrl)) { - return - } - + return (source: string) => { // look for files based on setup tsconfig "paths" return matchPath( source, diff --git a/tests/withPathsAndNestedBaseUrl/.eslintrc.js b/tests/withPathsAndNestedBaseUrl/.eslintrc.js new file mode 100644 index 0000000..cc90fb8 --- /dev/null +++ b/tests/withPathsAndNestedBaseUrl/.eslintrc.js @@ -0,0 +1 @@ +module.exports = require('../baseEslintConfig')(__dirname) diff --git a/tests/withPathsAndNestedBaseUrl/other/bar.ts b/tests/withPathsAndNestedBaseUrl/other/bar.ts new file mode 100644 index 0000000..827b976 --- /dev/null +++ b/tests/withPathsAndNestedBaseUrl/other/bar.ts @@ -0,0 +1,2 @@ +// import using tsconfig.json `baseUrl` +import 'foo' diff --git a/tests/withPathsAndNestedBaseUrl/root/foo.ts b/tests/withPathsAndNestedBaseUrl/root/foo.ts new file mode 100644 index 0000000..2589e1d --- /dev/null +++ b/tests/withPathsAndNestedBaseUrl/root/foo.ts @@ -0,0 +1,2 @@ +// import using tsconfig.json path mapping +import 'other/bar' diff --git a/tests/withPathsAndNestedBaseUrl/tsconfig.json b/tests/withPathsAndNestedBaseUrl/tsconfig.json new file mode 100644 index 0000000..7c01a48 --- /dev/null +++ b/tests/withPathsAndNestedBaseUrl/tsconfig.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "baseUrl": "root/", + "paths": { + "other/*": ["../other/*"] + } + }, + "files": ["root/foo.ts", "other/bar.ts"] +}