From 37987717fc48425f90fe220a56601c7b4164623c Mon Sep 17 00:00:00 2001 From: JounQin Date: Tue, 12 Mar 2024 18:52:32 +0800 Subject: [PATCH] feat!: replace tsconfig-paths with get-tsconfig --- package.json | 4 +- src/ExportMap.js | 75 +++++++++++++++++------------------- test/core/getExports.spec.js | 15 ++++---- yarn.lock | 29 +------------- 4 files changed, 46 insertions(+), 77 deletions(-) diff --git a/package.json b/package.json index ffd2b89a3..4a24fcd37 100644 --- a/package.json +++ b/package.json @@ -47,10 +47,10 @@ "debug": "^3.2.7", "doctrine": "^2.1.0", "eslint-import-resolver-node": "^0.3.9", + "get-tsconfig": "^4.7.3", "is-glob": "^4.0.3", "minimatch": "^3.1.2", - "semver": "^6.3.1", - "tsconfig-paths": "^3.15.0" + "semver": "^6.3.1" }, "devDependencies": { "@1stg/prettier-config": "^4.0.1", diff --git a/src/ExportMap.js b/src/ExportMap.js index 13dac9a00..d76df2f5f 100644 --- a/src/ExportMap.js +++ b/src/ExportMap.js @@ -1,5 +1,5 @@ import fs from 'fs' -import { dirname } from 'path' +import { resolve as pathResolve } from 'path' import doctrine from 'doctrine' @@ -15,9 +15,7 @@ import isIgnored, { hasValidExtension } from './utils/ignore' import { hashObject } from './utils/hash' import * as unambiguous from './utils/unambiguous' -import { tsConfigLoader } from 'tsconfig-paths/lib/tsconfig-loader' - -let ts +import { getTsconfig } from 'get-tsconfig' const log = debug('eslint-plugin-import-x:ExportMap') @@ -650,50 +648,39 @@ ExportMap.parse = function (path, content, context) { const source = makeSourceCode(content, ast) - function readTsConfig(context) { - const tsconfigInfo = tsConfigLoader({ - cwd: - (context.parserOptions && context.parserOptions.tsconfigRootDir) || - process.cwd(), - getEnv: key => process.env[key], - }) - try { - if (tsconfigInfo.tsConfigPath !== undefined) { - // Projects not using TypeScript won't have `typescript` installed. - if (!ts) { - ts = require('typescript') // eslint-disable-line import-x/no-extraneous-dependencies - } - - const configFile = ts.readConfigFile( - tsconfigInfo.tsConfigPath, - ts.sys.readFile, - ) - return ts.parseJsonConfigFileContent( - configFile.config, - ts.sys, - dirname(tsconfigInfo.tsConfigPath), - ) - } - } catch (e) { - // Catch any errors - } - - return null - } - function isEsModuleInterop() { + const parserOptions = context.parserOptions || {} + let tsconfigRootDir = parserOptions.tsconfigRootDir + const project = parserOptions.project const cacheKey = hashObject({ - tsconfigRootDir: - context.parserOptions && context.parserOptions.tsconfigRootDir, + tsconfigRootDir, + project, }).digest('hex') let tsConfig = tsconfigCache.get(cacheKey) if (typeof tsConfig === 'undefined') { - tsConfig = readTsConfig(context) + tsconfigRootDir = tsconfigRootDir || process.cwd() + let tsconfigResult + if (project) { + const projects = Array.isArray(project) ? project : [project] + for (const project of projects) { + tsconfigResult = getTsconfig( + project === true + ? context.filename + : pathResolve(tsconfigRootDir, project), + ) + if (tsconfigResult) { + break + } + } + } else { + tsconfigResult = getTsconfig(tsconfigRootDir) + } + tsConfig = (tsconfigResult && tsconfigResult.config) || null tsconfigCache.set(cacheKey, tsConfig) } - return tsConfig && tsConfig.options - ? tsConfig.options.esModuleInterop + return tsConfig && tsConfig.compilerOptions + ? tsConfig.compilerOptions.esModuleInterop : false } @@ -955,6 +942,14 @@ function childContext(path, context) { parserOptions, parserPath, path, + filename: + typeof context.getPhysicalFilename === 'function' + ? context.getPhysicalFilename() + : context.physicalFilename != null + ? context.physicalFilename + : typeof context.getFilename === 'function' + ? context.getFilename() + : context.filename, } } diff --git a/test/core/getExports.spec.js b/test/core/getExports.spec.js index d5b9861e1..f64e47930 100644 --- a/test/core/getExports.spec.js +++ b/test/core/getExports.spec.js @@ -1,7 +1,8 @@ import semver from 'semver' import eslintPkg from 'eslint/package.json' import typescriptPkg from 'typescript/package.json' -import * as tsConfigLoader from 'tsconfig-paths/lib/tsconfig-loader' +import getTsconfig from 'get-tsconfig' + import ExportMap from '../../src/ExportMap' import * as fs from 'fs' @@ -426,11 +427,11 @@ describe('ExportMap', () => { let imports beforeAll(() => { jest.setTimeout(20e3) // takes a long time :shrug: - jest.spyOn(tsConfigLoader, 'tsConfigLoader').mockClear() + jest.spyOn(getTsconfig, 'getTsconfig').mockClear() imports = ExportMap.get('./typescript.ts', context) }) afterAll(() => { - tsConfigLoader.tsConfigLoader.mockRestore() + getTsconfig.getTsconfig.mockRestore() }) it('returns something for a TypeScript file', () => { @@ -468,11 +469,11 @@ describe('ExportMap', () => { tsconfigRootDir: null, }, } - expect(tsConfigLoader.tsConfigLoader).toHaveBeenCalledTimes(0) + expect(getTsconfig.getTsconfig).toHaveBeenCalledTimes(0) ExportMap.parse('./baz.ts', 'export const baz = 5', customContext) - expect(tsConfigLoader.tsConfigLoader).toHaveBeenCalledTimes(1) + expect(getTsconfig.getTsconfig).toHaveBeenCalledTimes(1) ExportMap.parse('./baz.ts', 'export const baz = 5', customContext) - expect(tsConfigLoader.tsConfigLoader).toHaveBeenCalledTimes(1) + expect(getTsconfig.getTsconfig).toHaveBeenCalledTimes(1) const differentContext = { ...context, @@ -482,7 +483,7 @@ describe('ExportMap', () => { } ExportMap.parse('./baz.ts', 'export const baz = 5', differentContext) - expect(tsConfigLoader.tsConfigLoader).toHaveBeenCalledTimes(2) + expect(getTsconfig.getTsconfig).toHaveBeenCalledTimes(2) }) it('should cache after parsing for an ambiguous module', () => { diff --git a/yarn.lock b/yarn.lock index 1448daae7..9d259199f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1857,11 +1857,6 @@ resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.15.tgz#596a1747233694d50f6ad8a7869fcb6f56cf5841" integrity sha512-5+fP8P8MFNC+AyZCDxrB2pkZFPGzqQWUzpSeuuVLvm8VMcorNYavBqoFcxK8bQz4Qsbn4oUEEem4wDLfcysGHA== -"@types/json5@^0.0.29": - version "0.0.29" - resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee" - integrity sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ== - "@types/minimist@^1.2.0": version "1.2.5" resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.5.tgz#ec10755e871497bcd83efe927e43ec46e8c0747e" @@ -3595,7 +3590,7 @@ get-symbol-description@^1.0.2: es-errors "^1.3.0" get-intrinsic "^1.2.4" -get-tsconfig@^4.5.0: +get-tsconfig@^4.5.0, get-tsconfig@^4.7.3: version "4.7.3" resolved "https://registry.yarnpkg.com/get-tsconfig/-/get-tsconfig-4.7.3.tgz#0498163d98f7b58484dd4906999c0c9d5f103f83" integrity sha512-ZvkrzoUA0PQZM6fy6+/Hce561s+faD1rsNwhnO5FelNjyy7EMGJ3Rz1AQ8GYDWjhRs/7dBLOEJvhK8MiEJOAFg== @@ -4488,13 +4483,6 @@ json-stable-stringify-without-jsonify@^1.0.1: resolved "https://registry.yarnpkg.com/json-stable-stringify-without-jsonify/-/json-stable-stringify-without-jsonify-1.0.1.tgz#9db7b59496ad3f3cfef30a75142d2d930ad72651" integrity sha512-Bdboy+l7tA3OGW6FjyFHWkP5LuByj1Tk33Ljyq0axyzdk9//JSi2u3fP1QSmd1KNwq6VOKYGlAu87CisVir6Pw== -json5@^1.0.2: - version "1.0.2" - resolved "https://registry.yarnpkg.com/json5/-/json5-1.0.2.tgz#63d98d60f21b313b77c4d6da18bfa69d80e1d593" - integrity sha512-g1MWMLBiz8FKi1e4w0UyVL3w+iJceWAFBAaBnnGKOpNa5f8TLktkbre1+s6oICydWAm+HRUGTmI+//xv2hvXYA== - dependencies: - minimist "^1.2.0" - json5@^2.2.3: version "2.2.3" resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.3.tgz#78cd6f1a19bdc12b73db5ad0c61efd66c1e29283" @@ -4789,11 +4777,6 @@ minimist-options@^4.0.2: is-plain-obj "^1.1.0" kind-of "^6.0.3" -minimist@^1.2.0, minimist@^1.2.6: - version "1.2.8" - resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.8.tgz#c1a464e7693302e082a075cee0c057741ac4772c" - integrity sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA== - mixme@^0.5.1: version "0.5.10" resolved "https://registry.yarnpkg.com/mixme/-/mixme-0.5.10.tgz#d653b2984b75d9018828f1ea333e51717ead5f51" @@ -5914,16 +5897,6 @@ ts-api-utils@^1.0.1: resolved "https://registry.yarnpkg.com/ts-api-utils/-/ts-api-utils-1.3.0.tgz#4b490e27129f1e8e686b45cc4ab63714dc60eea1" integrity sha512-UQMIo7pb8WRomKR1/+MFVLTroIvDVtMX3K6OUir8ynLyzB8Jeriont2bTAtmNPa1ekAgN7YPDyf6V+ygrdU+eQ== -tsconfig-paths@^3.15.0: - version "3.15.0" - resolved "https://registry.yarnpkg.com/tsconfig-paths/-/tsconfig-paths-3.15.0.tgz#5299ec605e55b1abb23ec939ef15edaf483070d4" - integrity sha512-2Ac2RgzDe/cn48GvOe3M+o82pEFewD3UPbyoUHHdKasHwJKjds4fLXWf/Ux5kATBKN20oaFGu+jbElp1pos0mg== - dependencies: - "@types/json5" "^0.0.29" - json5 "^1.0.2" - minimist "^1.2.6" - strip-bom "^3.0.0" - tslib@^1.8.1: version "1.14.1" resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"