diff --git a/.changeset/wise-singers-own.md b/.changeset/wise-singers-own.md new file mode 100644 index 00000000..a16ba8e6 --- /dev/null +++ b/.changeset/wise-singers-own.md @@ -0,0 +1,5 @@ +--- +"eslint-import-resolver-typescript": minor +--- + +feat: throw error on malformed `tsconfig` reference diff --git a/.size-limit.json b/.size-limit.json index 32a6a585..f8a08422 100644 --- a/.size-limit.json +++ b/.size-limit.json @@ -1,6 +1,6 @@ [ { "path": "./lib/index.js", - "limit": "1.4kB" + "limit": "1.5kB" } ] diff --git a/src/constants.ts b/src/constants.ts index 25a218f8..302ff33b 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -73,3 +73,5 @@ export const DEFAULT_TRY_PATHS = ['', ...DEFAULT_CONFIGS] export const MATCH_ALL = '**' export const DEFAULT_IGNORE = [MATCH_ALL, 'node_modules', MATCH_ALL].join('/') + +export const TSCONFIG_NOT_FOUND_REGEXP = /^Tsconfig not found\b/ diff --git a/src/index.ts b/src/index.ts index 56ff51f4..d6387c15 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,7 +12,11 @@ import { isBunBuiltin } from 'is-bun-module' import { stableHash } from 'stable-hash' import { ResolverFactory } from 'unrs-resolver' -import { IMPORT_RESOLVER_NAME, JS_EXT_PATTERN } from './constants.js' +import { + IMPORT_RESOLVER_NAME, + JS_EXT_PATTERN, + TSCONFIG_NOT_FOUND_REGEXP, +} from './constants.js' import { mangleScopedPackage, removeQuerystring, @@ -47,6 +51,9 @@ const unrsResolve = ( } if (result.error) { log('oxc resolve error:', result.error) + if (TSCONFIG_NOT_FOUND_REGEXP.test(result.error)) { + throw new Error(result.error) + } } return { found: false, diff --git a/tests/unit/malformed-references/tsconfig.json b/tests/unit/malformed-reference/index.ts similarity index 100% rename from tests/unit/malformed-references/tsconfig.json rename to tests/unit/malformed-reference/index.ts diff --git a/tests/unit/malformed-reference/tsconfig.json b/tests/unit/malformed-reference/tsconfig.json new file mode 100644 index 00000000..95d45b55 --- /dev/null +++ b/tests/unit/malformed-reference/tsconfig.json @@ -0,0 +1,7 @@ +{ + "references": [ + { + "path": "./non-existed" + } + ] +} diff --git a/tests/unit/unit.spec.ts b/tests/unit/unit.spec.ts index d64a9917..a6a244cf 100644 --- a/tests/unit/unit.spec.ts +++ b/tests/unit/unit.spec.ts @@ -2,10 +2,15 @@ import path from 'node:path' import { exec } from 'tinyexec' -import { createTypeScriptImportResolver } from 'eslint-import-resolver-typescript' +import { + createTypeScriptImportResolver, + TSCONFIG_NOT_FOUND_REGEXP, +} from 'eslint-import-resolver-typescript' describe('createTypeScriptImportResolver', async () => { - const pnpDir = path.resolve(import.meta.dirname, 'pnp') + const { dirname } = import.meta + + const pnpDir = path.resolve(dirname, 'pnp') await exec('yarn', [], { nodeOptions: { @@ -13,9 +18,9 @@ describe('createTypeScriptImportResolver', async () => { }, }) - const resolver = createTypeScriptImportResolver() - it('should work with pnp', async () => { + const resolver = createTypeScriptImportResolver() + const testfile = path.resolve(pnpDir, '__test__.js') expect(resolver.resolve('pnpapi', testfile)).toMatchInlineSnapshot(` @@ -32,4 +37,16 @@ describe('createTypeScriptImportResolver', async () => { } `) }) + + it('should error on malformed tsconfig reference', () => { + const project = path.resolve(dirname, 'malformed-reference') + + const resolver = createTypeScriptImportResolver({ project }) + + const testfile = path.resolve(project, '__test__.js') + + expect(() => resolver.resolve('index.js', testfile)).toThrowError( + TSCONFIG_NOT_FOUND_REGEXP, + ) + }) })