Skip to content

Commit 2fcb947

Browse files
authored
feat: throw error on malformed tsconfig reference (#423)
close #415
1 parent ffb8ddf commit 2fcb947

File tree

7 files changed

+44
-6
lines changed

7 files changed

+44
-6
lines changed

.changeset/wise-singers-own.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-import-resolver-typescript": minor
3+
---
4+
5+
feat: throw error on malformed `tsconfig` reference

.size-limit.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[
22
{
33
"path": "./lib/index.js",
4-
"limit": "1.4kB"
4+
"limit": "1.5kB"
55
}
66
]

src/constants.ts

+2
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,5 @@ export const DEFAULT_TRY_PATHS = ['', ...DEFAULT_CONFIGS]
7373
export const MATCH_ALL = '**'
7474

7575
export const DEFAULT_IGNORE = [MATCH_ALL, 'node_modules', MATCH_ALL].join('/')
76+
77+
export const TSCONFIG_NOT_FOUND_REGEXP = /^Tsconfig not found\b/

src/index.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ import { isBunBuiltin } from 'is-bun-module'
1212
import { stableHash } from 'stable-hash'
1313
import { ResolverFactory } from 'unrs-resolver'
1414

15-
import { IMPORT_RESOLVER_NAME, JS_EXT_PATTERN } from './constants.js'
15+
import {
16+
IMPORT_RESOLVER_NAME,
17+
JS_EXT_PATTERN,
18+
TSCONFIG_NOT_FOUND_REGEXP,
19+
} from './constants.js'
1620
import {
1721
mangleScopedPackage,
1822
removeQuerystring,
@@ -47,6 +51,9 @@ const unrsResolve = (
4751
}
4852
if (result.error) {
4953
log('oxc resolve error:', result.error)
54+
if (TSCONFIG_NOT_FOUND_REGEXP.test(result.error)) {
55+
throw new Error(result.error)
56+
}
5057
}
5158
return {
5259
found: false,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"references": [
3+
{
4+
"path": "./non-existed"
5+
}
6+
]
7+
}

tests/unit/unit.spec.ts

+21-4
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,25 @@ import path from 'node:path'
22

33
import { exec } from 'tinyexec'
44

5-
import { createTypeScriptImportResolver } from 'eslint-import-resolver-typescript'
5+
import {
6+
createTypeScriptImportResolver,
7+
TSCONFIG_NOT_FOUND_REGEXP,
8+
} from 'eslint-import-resolver-typescript'
69

710
describe('createTypeScriptImportResolver', async () => {
8-
const pnpDir = path.resolve(import.meta.dirname, 'pnp')
11+
const { dirname } = import.meta
12+
13+
const pnpDir = path.resolve(dirname, 'pnp')
914

1015
await exec('yarn', [], {
1116
nodeOptions: {
1217
cwd: pnpDir,
1318
},
1419
})
1520

16-
const resolver = createTypeScriptImportResolver()
17-
1821
it('should work with pnp', async () => {
22+
const resolver = createTypeScriptImportResolver()
23+
1924
const testfile = path.resolve(pnpDir, '__test__.js')
2025

2126
expect(resolver.resolve('pnpapi', testfile)).toMatchInlineSnapshot(`
@@ -32,4 +37,16 @@ describe('createTypeScriptImportResolver', async () => {
3237
}
3338
`)
3439
})
40+
41+
it('should error on malformed tsconfig reference', () => {
42+
const project = path.resolve(dirname, 'malformed-reference')
43+
44+
const resolver = createTypeScriptImportResolver({ project })
45+
46+
const testfile = path.resolve(project, '__test__.js')
47+
48+
expect(() => resolver.resolve('index.js', testfile)).toThrowError(
49+
TSCONFIG_NOT_FOUND_REGEXP,
50+
)
51+
})
3552
})

0 commit comments

Comments
 (0)