From d563eeb2af2938b9ff7f75e0492a5a26112a4772 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 9 Jul 2020 05:14:40 +0300 Subject: [PATCH] fix: wrong path resolution in multiple eslintrc configurations (#51) close #50 Co-authored-by: Alex Vasiuro --- package.json | 5 +++-- src/index.ts | 5 ++++- tests/multipleEslintrcs/module-a/.eslintrc.js | 1 + tests/multipleEslintrcs/module-a/index.ts | 15 +++++++++++++++ .../module-a/subfolder/tsImportee.ts | 1 + .../module-a/subfolder/tsxImportee.tsx | 1 + tests/multipleEslintrcs/module-a/tsImportee.ts | 1 + tests/multipleEslintrcs/module-a/tsconfig.json | 11 +++++++++++ tests/multipleEslintrcs/module-a/tsxImportee.tsx | 1 + tests/multipleEslintrcs/module-b/.eslintrc.js | 1 + tests/multipleEslintrcs/module-b/index.ts | 15 +++++++++++++++ .../module-b/subfolder/tsImportee.ts | 1 + .../module-b/subfolder/tsxImportee.tsx | 1 + tests/multipleEslintrcs/module-b/tsImportee.ts | 1 + tests/multipleEslintrcs/module-b/tsconfig.json | 11 +++++++++++ tests/multipleEslintrcs/module-b/tsxImportee.tsx | 1 + 16 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 tests/multipleEslintrcs/module-a/.eslintrc.js create mode 100644 tests/multipleEslintrcs/module-a/index.ts create mode 100644 tests/multipleEslintrcs/module-a/subfolder/tsImportee.ts create mode 100644 tests/multipleEslintrcs/module-a/subfolder/tsxImportee.tsx create mode 100644 tests/multipleEslintrcs/module-a/tsImportee.ts create mode 100644 tests/multipleEslintrcs/module-a/tsconfig.json create mode 100644 tests/multipleEslintrcs/module-a/tsxImportee.tsx create mode 100644 tests/multipleEslintrcs/module-b/.eslintrc.js create mode 100644 tests/multipleEslintrcs/module-b/index.ts create mode 100644 tests/multipleEslintrcs/module-b/subfolder/tsImportee.ts create mode 100644 tests/multipleEslintrcs/module-b/subfolder/tsxImportee.tsx create mode 100644 tests/multipleEslintrcs/module-b/tsImportee.ts create mode 100644 tests/multipleEslintrcs/module-b/tsconfig.json create mode 100644 tests/multipleEslintrcs/module-b/tsxImportee.tsx diff --git a/package.json b/package.json index d8a1fc2..c28ce8f 100644 --- a/package.json +++ b/package.json @@ -35,10 +35,11 @@ "lint:tsc": "tsc --incremental false --noEmit", "prepublishOnly": "yarn build", "pretest": "r", + "test": "run-p test:*", + "test:multipleEslintrcs": "eslint --ext ts,tsx tests/multipleEslintrcs", + "test:multipleTsconfigs": "eslint --ext ts,tsx tests/multipleTsconfigs", "test:withPaths": "eslint --ext ts,tsx tests/withPaths", "test:withoutPaths": "eslint --ext ts,tsx tests/withoutPaths", - "test:multipleTsconfigs": "eslint --ext ts,tsx tests/multipleTsconfigs", - "test": "run-p test:*", "type-coverage": "type-coverage --cache --detail --ignore-catch --strict" }, "peerDependencies": { diff --git a/src/index.ts b/src/index.ts index b8098e2..01f85de 100644 --- a/src/index.ts +++ b/src/index.ts @@ -111,6 +111,7 @@ function packageFilter(pkg: Record) { return pkg } +let mappersBuildForOptions: TsResolverOptions let mappers: | Array<(source: string, file: string) => string | undefined> | undefined @@ -133,7 +134,7 @@ function getMappedPath(source: string, file: string) { } function initMappers(options: TsResolverOptions) { - if (mappers) { + if (mappers && mappersBuildForOptions === options) { return } @@ -171,6 +172,8 @@ function initMappers(options: TsResolverOptions) { return matchPath(source, undefined, undefined, extensions) } }) + + mappersBuildForOptions = options } function isConfigLoaderSuccessResult( diff --git a/tests/multipleEslintrcs/module-a/.eslintrc.js b/tests/multipleEslintrcs/module-a/.eslintrc.js new file mode 100644 index 0000000..5296f2b --- /dev/null +++ b/tests/multipleEslintrcs/module-a/.eslintrc.js @@ -0,0 +1 @@ +module.exports = require('../../baseEslintConfig')(__dirname); diff --git a/tests/multipleEslintrcs/module-a/index.ts b/tests/multipleEslintrcs/module-a/index.ts new file mode 100644 index 0000000..8d011e9 --- /dev/null +++ b/tests/multipleEslintrcs/module-a/index.ts @@ -0,0 +1,15 @@ +// import relative +import './tsImportee' +import './tsxImportee' +import './subfolder/tsImportee' +import './subfolder/tsxImportee' + +// import using tsconfig.json path mapping +import 'folder/tsImportee' +import 'folder/tsxImportee' +import 'folder/subfolder/tsImportee' +import 'folder/subfolder/tsxImportee' + +// import from node_module +import 'typescript' +import 'dummy.js' diff --git a/tests/multipleEslintrcs/module-a/subfolder/tsImportee.ts b/tests/multipleEslintrcs/module-a/subfolder/tsImportee.ts new file mode 100644 index 0000000..2893e5d --- /dev/null +++ b/tests/multipleEslintrcs/module-a/subfolder/tsImportee.ts @@ -0,0 +1 @@ +export default 'yes' diff --git a/tests/multipleEslintrcs/module-a/subfolder/tsxImportee.tsx b/tests/multipleEslintrcs/module-a/subfolder/tsxImportee.tsx new file mode 100644 index 0000000..e500b41 --- /dev/null +++ b/tests/multipleEslintrcs/module-a/subfolder/tsxImportee.tsx @@ -0,0 +1 @@ +export default 'React Component' diff --git a/tests/multipleEslintrcs/module-a/tsImportee.ts b/tests/multipleEslintrcs/module-a/tsImportee.ts new file mode 100644 index 0000000..2893e5d --- /dev/null +++ b/tests/multipleEslintrcs/module-a/tsImportee.ts @@ -0,0 +1 @@ +export default 'yes' diff --git a/tests/multipleEslintrcs/module-a/tsconfig.json b/tests/multipleEslintrcs/module-a/tsconfig.json new file mode 100644 index 0000000..1ed978a --- /dev/null +++ b/tests/multipleEslintrcs/module-a/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "baseUrl": ".", + "jsx": "react", + "paths": { + "folder/*": ["*"], + "*": ["../../../../node_modules/*"] + } + }, + "files": ["index.ts", "tsImportee.ts", "tsxImportee.tsx"] +} diff --git a/tests/multipleEslintrcs/module-a/tsxImportee.tsx b/tests/multipleEslintrcs/module-a/tsxImportee.tsx new file mode 100644 index 0000000..e500b41 --- /dev/null +++ b/tests/multipleEslintrcs/module-a/tsxImportee.tsx @@ -0,0 +1 @@ +export default 'React Component' diff --git a/tests/multipleEslintrcs/module-b/.eslintrc.js b/tests/multipleEslintrcs/module-b/.eslintrc.js new file mode 100644 index 0000000..5296f2b --- /dev/null +++ b/tests/multipleEslintrcs/module-b/.eslintrc.js @@ -0,0 +1 @@ +module.exports = require('../../baseEslintConfig')(__dirname); diff --git a/tests/multipleEslintrcs/module-b/index.ts b/tests/multipleEslintrcs/module-b/index.ts new file mode 100644 index 0000000..8d011e9 --- /dev/null +++ b/tests/multipleEslintrcs/module-b/index.ts @@ -0,0 +1,15 @@ +// import relative +import './tsImportee' +import './tsxImportee' +import './subfolder/tsImportee' +import './subfolder/tsxImportee' + +// import using tsconfig.json path mapping +import 'folder/tsImportee' +import 'folder/tsxImportee' +import 'folder/subfolder/tsImportee' +import 'folder/subfolder/tsxImportee' + +// import from node_module +import 'typescript' +import 'dummy.js' diff --git a/tests/multipleEslintrcs/module-b/subfolder/tsImportee.ts b/tests/multipleEslintrcs/module-b/subfolder/tsImportee.ts new file mode 100644 index 0000000..2893e5d --- /dev/null +++ b/tests/multipleEslintrcs/module-b/subfolder/tsImportee.ts @@ -0,0 +1 @@ +export default 'yes' diff --git a/tests/multipleEslintrcs/module-b/subfolder/tsxImportee.tsx b/tests/multipleEslintrcs/module-b/subfolder/tsxImportee.tsx new file mode 100644 index 0000000..e500b41 --- /dev/null +++ b/tests/multipleEslintrcs/module-b/subfolder/tsxImportee.tsx @@ -0,0 +1 @@ +export default 'React Component' diff --git a/tests/multipleEslintrcs/module-b/tsImportee.ts b/tests/multipleEslintrcs/module-b/tsImportee.ts new file mode 100644 index 0000000..2893e5d --- /dev/null +++ b/tests/multipleEslintrcs/module-b/tsImportee.ts @@ -0,0 +1 @@ +export default 'yes' diff --git a/tests/multipleEslintrcs/module-b/tsconfig.json b/tests/multipleEslintrcs/module-b/tsconfig.json new file mode 100644 index 0000000..1ed978a --- /dev/null +++ b/tests/multipleEslintrcs/module-b/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "baseUrl": ".", + "jsx": "react", + "paths": { + "folder/*": ["*"], + "*": ["../../../../node_modules/*"] + } + }, + "files": ["index.ts", "tsImportee.ts", "tsxImportee.tsx"] +} diff --git a/tests/multipleEslintrcs/module-b/tsxImportee.tsx b/tests/multipleEslintrcs/module-b/tsxImportee.tsx new file mode 100644 index 0000000..e500b41 --- /dev/null +++ b/tests/multipleEslintrcs/module-b/tsxImportee.tsx @@ -0,0 +1 @@ +export default 'React Component'