From a1d87b7811b5d02a648c4df074630b51eee70bc4 Mon Sep 17 00:00:00 2001 From: line0 Date: Wed, 6 Jul 2022 17:19:48 +0200 Subject: [PATCH] fix: don't ignore the tsconfig file name in project paths or path patterns `getTsConfig` treats every path as a directory and looks for a `tsconfig.json` in the closest parent directory this breaks any configuration that uses file names other than tsconfig.json, as is convention for [solution-style](https://devblogs.microsoft.com/typescript/announcing-typescript-3-9/#solution-style-tsconfig) tsconfigs, which is exacerbated by the lack of support for the `references` field (see #94) --- package.json | 1 + src/index.ts | 7 +++++-- tests/nonDefaultTsconfigFileName/.eslintrc.cjs | 5 +++++ tests/nonDefaultTsconfigFileName/index.ts | 2 ++ tests/nonDefaultTsconfigFileName/tsImportee.ts | 1 + tests/nonDefaultTsconfigFileName/tsconfig.custom.json | 9 +++++++++ 6 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 tests/nonDefaultTsconfigFileName/.eslintrc.cjs create mode 100644 tests/nonDefaultTsconfigFileName/index.ts create mode 100644 tests/nonDefaultTsconfigFileName/tsImportee.ts create mode 100644 tests/nonDefaultTsconfigFileName/tsconfig.custom.json diff --git a/package.json b/package.json index 9463cf6..497c172 100644 --- a/package.json +++ b/package.json @@ -87,6 +87,7 @@ "test:withPathsAndNestedBaseUrl": "eslint --ext ts,tsx tests/withPathsAndNestedBaseUrl", "test:withQuerystring": "eslint --ext ts,tsx tests/withQuerystring", "test:withoutPaths": "eslint --ext ts,tsx tests/withoutPaths", + "test:nonDefaultTsconfigFileName": "eslint --ext ts,tsx tests/nonDefaultTsconfigFileName", "typecov": "type-coverage" }, "peerDependencies": { diff --git a/src/index.ts b/src/index.ts index eeb0f27..52a2865 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,7 +9,7 @@ import { Resolver, ResolverFactory, } from 'enhanced-resolve' -import { createPathsMatcher, getTsconfig } from 'get-tsconfig' +import { createPathsMatcher, getTsconfig, parseTsconfig } from 'get-tsconfig' import isCore from 'is-core-module' import isGlob from 'is-glob' import { createSyncFn } from 'synckit' @@ -350,7 +350,10 @@ function initMappers(options: TsResolverOptions) { ] mappers = projectPaths.map(projectPath => { - const tsconfigResult = getTsconfig(projectPath) + const tsconfigResult = + fs.existsSync(projectPath) && fs.statSync(projectPath).isFile() + ? { path: projectPath, config: parseTsconfig(projectPath) } + : getTsconfig(projectPath) return tsconfigResult && createPathsMatcher(tsconfigResult) }) diff --git a/tests/nonDefaultTsconfigFileName/.eslintrc.cjs b/tests/nonDefaultTsconfigFileName/.eslintrc.cjs new file mode 100644 index 0000000..b20dbb7 --- /dev/null +++ b/tests/nonDefaultTsconfigFileName/.eslintrc.cjs @@ -0,0 +1,5 @@ +const path = require('path') + +module.exports = require('../baseEslintConfig.cjs')( + path.join(__dirname, 'tsconfig.custom.json'), +) diff --git a/tests/nonDefaultTsconfigFileName/index.ts b/tests/nonDefaultTsconfigFileName/index.ts new file mode 100644 index 0000000..ac0c043 --- /dev/null +++ b/tests/nonDefaultTsconfigFileName/index.ts @@ -0,0 +1,2 @@ +// import using tsconfig.json path mapping +import 'folder/tsImportee' diff --git a/tests/nonDefaultTsconfigFileName/tsImportee.ts b/tests/nonDefaultTsconfigFileName/tsImportee.ts new file mode 100644 index 0000000..2893e5d --- /dev/null +++ b/tests/nonDefaultTsconfigFileName/tsImportee.ts @@ -0,0 +1 @@ +export default 'yes' diff --git a/tests/nonDefaultTsconfigFileName/tsconfig.custom.json b/tests/nonDefaultTsconfigFileName/tsconfig.custom.json new file mode 100644 index 0000000..97be7a8 --- /dev/null +++ b/tests/nonDefaultTsconfigFileName/tsconfig.custom.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "baseUrl": ".", + "paths": { + "folder/*": ["*"], + } + }, + "files": ["index.ts", "tsImportee.ts"] +}