Skip to content

Commit

Permalink
feat!: replace tsconfig-paths with get-tsconfig (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin authored Mar 12, 2024
1 parent a7157c3 commit 49e3cd2
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 77 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-baboons-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-import-x": minor
---

feat!: replace tsconfig-paths with get-tsconfig
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
75 changes: 35 additions & 40 deletions src/ExportMap.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs'
import { dirname } from 'path'
import { resolve as pathResolve } from 'path'

import doctrine from 'doctrine'

Expand All @@ -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')

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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,
}
}

Expand Down
15 changes: 8 additions & 7 deletions test/core/getExports.spec.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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,
Expand All @@ -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', () => {
Expand Down
29 changes: 1 addition & 28 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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==
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 49e3cd2

Please sign in to comment.