From 82d090b2411853f94909a3c9a0ed3b8bbe1bef11 Mon Sep 17 00:00:00 2001 From: rbong Date: Mon, 1 Aug 2022 11:12:05 -0400 Subject: [PATCH] feat: add support for `jsconfig.json` (#161) Co-authored-by: JounQin --- .changeset/neat-glasses-carry.md | 5 +++++ package.json | 1 + src/index.ts | 11 ++++++++++- tests/withJsconfig/.eslintrc.cjs | 5 +++++ tests/withJsconfig/importee.js | 1 + tests/withJsconfig/index.js | 2 ++ tests/withJsconfig/jsconfig.json | 8 ++++++++ 7 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 .changeset/neat-glasses-carry.md create mode 100644 tests/withJsconfig/.eslintrc.cjs create mode 100644 tests/withJsconfig/importee.js create mode 100644 tests/withJsconfig/index.js create mode 100644 tests/withJsconfig/jsconfig.json diff --git a/.changeset/neat-glasses-carry.md b/.changeset/neat-glasses-carry.md new file mode 100644 index 0000000..b37ebea --- /dev/null +++ b/.changeset/neat-glasses-carry.md @@ -0,0 +1,5 @@ +--- +"eslint-import-resolver-typescript": minor +--- + +feat: add support for `jsconfig.json` diff --git a/package.json b/package.json index 2b0823e..0d4ca8c 100644 --- a/package.json +++ b/package.json @@ -82,6 +82,7 @@ "test": "run-p test:*", "test:multipleEslintrcs": "eslint --ext ts,tsx tests/multipleEslintrcs", "test:multipleTsconfigs": "eslint --ext ts,tsx tests/multipleTsconfigs", + "test:withJsconfig": "eslint --ext js tests/withJsconfig", "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", diff --git a/src/index.ts b/src/index.ts index 4404230..6ca26a9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,6 +10,7 @@ import { ResolverFactory, } from 'enhanced-resolve' import { createPathsMatcher, getTsconfig } from 'get-tsconfig' +import type { TsConfigResult } from 'get-tsconfig' import isCore from 'is-core-module' import isGlob from 'is-glob' import { createSyncFn } from 'synckit' @@ -333,7 +334,15 @@ function initMappers(options: InternalResolverOptions) { ] mappers = projectPaths.map(projectPath => { - const tsconfigResult = getTsconfig(projectPath) + let tsconfigResult: TsConfigResult | null + + if (isFile(projectPath)) { + const { dir, base } = path.parse(projectPath) + tsconfigResult = getTsconfig(dir, base) + } else { + tsconfigResult = getTsconfig(projectPath) + } + return tsconfigResult && createPathsMatcher(tsconfigResult) }) diff --git a/tests/withJsconfig/.eslintrc.cjs b/tests/withJsconfig/.eslintrc.cjs new file mode 100644 index 0000000..f20d5d2 --- /dev/null +++ b/tests/withJsconfig/.eslintrc.cjs @@ -0,0 +1,5 @@ +const path = require('path') + +const configPath = path.join(__dirname, 'jsconfig.json') + +module.exports = require('../baseEslintConfig.cjs')(configPath) diff --git a/tests/withJsconfig/importee.js b/tests/withJsconfig/importee.js new file mode 100644 index 0000000..cb43be0 --- /dev/null +++ b/tests/withJsconfig/importee.js @@ -0,0 +1 @@ +export default 'importee' diff --git a/tests/withJsconfig/index.js b/tests/withJsconfig/index.js new file mode 100644 index 0000000..7e44c56 --- /dev/null +++ b/tests/withJsconfig/index.js @@ -0,0 +1,2 @@ +// import using jsconfig.json path mapping +import '#/importee' diff --git a/tests/withJsconfig/jsconfig.json b/tests/withJsconfig/jsconfig.json new file mode 100644 index 0000000..a06fc56 --- /dev/null +++ b/tests/withJsconfig/jsconfig.json @@ -0,0 +1,8 @@ +{ + "compilerOptions": { + "baseUrl": "./", + "paths": { + "#/*": ["*"] + } + } +}