Skip to content

Commit 49e3cd2

Browse files
authored
feat!: replace tsconfig-paths with get-tsconfig (#36)
1 parent a7157c3 commit 49e3cd2

File tree

5 files changed

+51
-77
lines changed

5 files changed

+51
-77
lines changed

.changeset/cyan-baboons-cough.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-import-x": minor
3+
---
4+
5+
feat!: replace tsconfig-paths with get-tsconfig

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@
4747
"debug": "^3.2.7",
4848
"doctrine": "^2.1.0",
4949
"eslint-import-resolver-node": "^0.3.9",
50+
"get-tsconfig": "^4.7.3",
5051
"is-glob": "^4.0.3",
5152
"minimatch": "^3.1.2",
52-
"semver": "^6.3.1",
53-
"tsconfig-paths": "^3.15.0"
53+
"semver": "^6.3.1"
5454
},
5555
"devDependencies": {
5656
"@1stg/prettier-config": "^4.0.1",

src/ExportMap.js

+35-40
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import fs from 'fs'
2-
import { dirname } from 'path'
2+
import { resolve as pathResolve } from 'path'
33

44
import doctrine from 'doctrine'
55

@@ -15,9 +15,7 @@ import isIgnored, { hasValidExtension } from './utils/ignore'
1515
import { hashObject } from './utils/hash'
1616
import * as unambiguous from './utils/unambiguous'
1717

18-
import { tsConfigLoader } from 'tsconfig-paths/lib/tsconfig-loader'
19-
20-
let ts
18+
import { getTsconfig } from 'get-tsconfig'
2119

2220
const log = debug('eslint-plugin-import-x:ExportMap')
2321

@@ -650,50 +648,39 @@ ExportMap.parse = function (path, content, context) {
650648

651649
const source = makeSourceCode(content, ast)
652650

653-
function readTsConfig(context) {
654-
const tsconfigInfo = tsConfigLoader({
655-
cwd:
656-
(context.parserOptions && context.parserOptions.tsconfigRootDir) ||
657-
process.cwd(),
658-
getEnv: key => process.env[key],
659-
})
660-
try {
661-
if (tsconfigInfo.tsConfigPath !== undefined) {
662-
// Projects not using TypeScript won't have `typescript` installed.
663-
if (!ts) {
664-
ts = require('typescript') // eslint-disable-line import-x/no-extraneous-dependencies
665-
}
666-
667-
const configFile = ts.readConfigFile(
668-
tsconfigInfo.tsConfigPath,
669-
ts.sys.readFile,
670-
)
671-
return ts.parseJsonConfigFileContent(
672-
configFile.config,
673-
ts.sys,
674-
dirname(tsconfigInfo.tsConfigPath),
675-
)
676-
}
677-
} catch (e) {
678-
// Catch any errors
679-
}
680-
681-
return null
682-
}
683-
684651
function isEsModuleInterop() {
652+
const parserOptions = context.parserOptions || {}
653+
let tsconfigRootDir = parserOptions.tsconfigRootDir
654+
const project = parserOptions.project
685655
const cacheKey = hashObject({
686-
tsconfigRootDir:
687-
context.parserOptions && context.parserOptions.tsconfigRootDir,
656+
tsconfigRootDir,
657+
project,
688658
}).digest('hex')
689659
let tsConfig = tsconfigCache.get(cacheKey)
690660
if (typeof tsConfig === 'undefined') {
691-
tsConfig = readTsConfig(context)
661+
tsconfigRootDir = tsconfigRootDir || process.cwd()
662+
let tsconfigResult
663+
if (project) {
664+
const projects = Array.isArray(project) ? project : [project]
665+
for (const project of projects) {
666+
tsconfigResult = getTsconfig(
667+
project === true
668+
? context.filename
669+
: pathResolve(tsconfigRootDir, project),
670+
)
671+
if (tsconfigResult) {
672+
break
673+
}
674+
}
675+
} else {
676+
tsconfigResult = getTsconfig(tsconfigRootDir)
677+
}
678+
tsConfig = (tsconfigResult && tsconfigResult.config) || null
692679
tsconfigCache.set(cacheKey, tsConfig)
693680
}
694681

695-
return tsConfig && tsConfig.options
696-
? tsConfig.options.esModuleInterop
682+
return tsConfig && tsConfig.compilerOptions
683+
? tsConfig.compilerOptions.esModuleInterop
697684
: false
698685
}
699686

@@ -955,6 +942,14 @@ function childContext(path, context) {
955942
parserOptions,
956943
parserPath,
957944
path,
945+
filename:
946+
typeof context.getPhysicalFilename === 'function'
947+
? context.getPhysicalFilename()
948+
: context.physicalFilename != null
949+
? context.physicalFilename
950+
: typeof context.getFilename === 'function'
951+
? context.getFilename()
952+
: context.filename,
958953
}
959954
}
960955

test/core/getExports.spec.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import semver from 'semver'
22
import eslintPkg from 'eslint/package.json'
33
import typescriptPkg from 'typescript/package.json'
4-
import * as tsConfigLoader from 'tsconfig-paths/lib/tsconfig-loader'
4+
import getTsconfig from 'get-tsconfig'
5+
56
import ExportMap from '../../src/ExportMap'
67

78
import * as fs from 'fs'
@@ -426,11 +427,11 @@ describe('ExportMap', () => {
426427
let imports
427428
beforeAll(() => {
428429
jest.setTimeout(20e3) // takes a long time :shrug:
429-
jest.spyOn(tsConfigLoader, 'tsConfigLoader').mockClear()
430+
jest.spyOn(getTsconfig, 'getTsconfig').mockClear()
430431
imports = ExportMap.get('./typescript.ts', context)
431432
})
432433
afterAll(() => {
433-
tsConfigLoader.tsConfigLoader.mockRestore()
434+
getTsconfig.getTsconfig.mockRestore()
434435
})
435436

436437
it('returns something for a TypeScript file', () => {
@@ -468,11 +469,11 @@ describe('ExportMap', () => {
468469
tsconfigRootDir: null,
469470
},
470471
}
471-
expect(tsConfigLoader.tsConfigLoader).toHaveBeenCalledTimes(0)
472+
expect(getTsconfig.getTsconfig).toHaveBeenCalledTimes(0)
472473
ExportMap.parse('./baz.ts', 'export const baz = 5', customContext)
473-
expect(tsConfigLoader.tsConfigLoader).toHaveBeenCalledTimes(1)
474+
expect(getTsconfig.getTsconfig).toHaveBeenCalledTimes(1)
474475
ExportMap.parse('./baz.ts', 'export const baz = 5', customContext)
475-
expect(tsConfigLoader.tsConfigLoader).toHaveBeenCalledTimes(1)
476+
expect(getTsconfig.getTsconfig).toHaveBeenCalledTimes(1)
476477

477478
const differentContext = {
478479
...context,
@@ -482,7 +483,7 @@ describe('ExportMap', () => {
482483
}
483484

484485
ExportMap.parse('./baz.ts', 'export const baz = 5', differentContext)
485-
expect(tsConfigLoader.tsConfigLoader).toHaveBeenCalledTimes(2)
486+
expect(getTsconfig.getTsconfig).toHaveBeenCalledTimes(2)
486487
})
487488

488489
it('should cache after parsing for an ambiguous module', () => {

yarn.lock

+1-28
Original file line numberDiff line numberDiff line change
@@ -1857,11 +1857,6 @@
18571857
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.15.tgz#596a1747233694d50f6ad8a7869fcb6f56cf5841"
18581858
integrity sha512-5+fP8P8MFNC+AyZCDxrB2pkZFPGzqQWUzpSeuuVLvm8VMcorNYavBqoFcxK8bQz4Qsbn4oUEEem4wDLfcysGHA==
18591859

1860-
"@types/json5@^0.0.29":
1861-
version "0.0.29"
1862-
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
1863-
integrity sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==
1864-
18651860
"@types/minimist@^1.2.0":
18661861
version "1.2.5"
18671862
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.5.tgz#ec10755e871497bcd83efe927e43ec46e8c0747e"
@@ -3595,7 +3590,7 @@ get-symbol-description@^1.0.2:
35953590
es-errors "^1.3.0"
35963591
get-intrinsic "^1.2.4"
35973592

3598-
get-tsconfig@^4.5.0:
3593+
get-tsconfig@^4.5.0, get-tsconfig@^4.7.3:
35993594
version "4.7.3"
36003595
resolved "https://registry.yarnpkg.com/get-tsconfig/-/get-tsconfig-4.7.3.tgz#0498163d98f7b58484dd4906999c0c9d5f103f83"
36013596
integrity sha512-ZvkrzoUA0PQZM6fy6+/Hce561s+faD1rsNwhnO5FelNjyy7EMGJ3Rz1AQ8GYDWjhRs/7dBLOEJvhK8MiEJOAFg==
@@ -4488,13 +4483,6 @@ json-stable-stringify-without-jsonify@^1.0.1:
44884483
resolved "https://registry.yarnpkg.com/json-stable-stringify-without-jsonify/-/json-stable-stringify-without-jsonify-1.0.1.tgz#9db7b59496ad3f3cfef30a75142d2d930ad72651"
44894484
integrity sha512-Bdboy+l7tA3OGW6FjyFHWkP5LuByj1Tk33Ljyq0axyzdk9//JSi2u3fP1QSmd1KNwq6VOKYGlAu87CisVir6Pw==
44904485

4491-
json5@^1.0.2:
4492-
version "1.0.2"
4493-
resolved "https://registry.yarnpkg.com/json5/-/json5-1.0.2.tgz#63d98d60f21b313b77c4d6da18bfa69d80e1d593"
4494-
integrity sha512-g1MWMLBiz8FKi1e4w0UyVL3w+iJceWAFBAaBnnGKOpNa5f8TLktkbre1+s6oICydWAm+HRUGTmI+//xv2hvXYA==
4495-
dependencies:
4496-
minimist "^1.2.0"
4497-
44984486
json5@^2.2.3:
44994487
version "2.2.3"
45004488
resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.3.tgz#78cd6f1a19bdc12b73db5ad0c61efd66c1e29283"
@@ -4789,11 +4777,6 @@ minimist-options@^4.0.2:
47894777
is-plain-obj "^1.1.0"
47904778
kind-of "^6.0.3"
47914779

4792-
minimist@^1.2.0, minimist@^1.2.6:
4793-
version "1.2.8"
4794-
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.8.tgz#c1a464e7693302e082a075cee0c057741ac4772c"
4795-
integrity sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==
4796-
47974780
mixme@^0.5.1:
47984781
version "0.5.10"
47994782
resolved "https://registry.yarnpkg.com/mixme/-/mixme-0.5.10.tgz#d653b2984b75d9018828f1ea333e51717ead5f51"
@@ -5914,16 +5897,6 @@ ts-api-utils@^1.0.1:
59145897
resolved "https://registry.yarnpkg.com/ts-api-utils/-/ts-api-utils-1.3.0.tgz#4b490e27129f1e8e686b45cc4ab63714dc60eea1"
59155898
integrity sha512-UQMIo7pb8WRomKR1/+MFVLTroIvDVtMX3K6OUir8ynLyzB8Jeriont2bTAtmNPa1ekAgN7YPDyf6V+ygrdU+eQ==
59165899

5917-
tsconfig-paths@^3.15.0:
5918-
version "3.15.0"
5919-
resolved "https://registry.yarnpkg.com/tsconfig-paths/-/tsconfig-paths-3.15.0.tgz#5299ec605e55b1abb23ec939ef15edaf483070d4"
5920-
integrity sha512-2Ac2RgzDe/cn48GvOe3M+o82pEFewD3UPbyoUHHdKasHwJKjds4fLXWf/Ux5kATBKN20oaFGu+jbElp1pos0mg==
5921-
dependencies:
5922-
"@types/json5" "^0.0.29"
5923-
json5 "^1.0.2"
5924-
minimist "^1.2.6"
5925-
strip-bom "^3.0.0"
5926-
59275900
tslib@^1.8.1:
59285901
version "1.14.1"
59295902
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"

0 commit comments

Comments
 (0)