From dde08595d97a094ef777c5da760527672c6aae3f Mon Sep 17 00:00:00 2001 From: JounQin Date: Sat, 29 Mar 2025 23:54:33 +0800 Subject: [PATCH 1/3] feat: throw error on malformed tsconfig reference close #415 --- src/constants.ts | 2 ++ src/index.ts | 9 ++++++- .../index.ts} | 0 tests/unit/malformed-reference/tsconfig.json | 7 ++++++ tests/unit/unit.spec.ts | 25 ++++++++++++++++--- 5 files changed, 38 insertions(+), 5 deletions(-) rename tests/unit/{malformed-references/tsconfig.json => malformed-reference/index.ts} (100%) create mode 100644 tests/unit/malformed-reference/tsconfig.json diff --git a/src/constants.ts b/src/constants.ts index 25a218f..302ff33 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 56ff51f..d6387c1 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 0000000..95d45b5 --- /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 d64a991..a6a244c 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, + ) + }) }) From 259c061945d6635d8bc41fb2505fff09cf3ed93d Mon Sep 17 00:00:00 2001 From: JounQin Date: Sat, 29 Mar 2025 23:55:58 +0800 Subject: [PATCH 2/3] Create wise-singers-own.md --- .changeset/wise-singers-own.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/wise-singers-own.md diff --git a/.changeset/wise-singers-own.md b/.changeset/wise-singers-own.md new file mode 100644 index 0000000..a16ba8e --- /dev/null +++ b/.changeset/wise-singers-own.md @@ -0,0 +1,5 @@ +--- +"eslint-import-resolver-typescript": minor +--- + +feat: throw error on malformed `tsconfig` reference From 03c97f3a84ca7dcb8302a1f7ecbc76f3d6b1a091 Mon Sep 17 00:00:00 2001 From: JounQin Date: Sun, 30 Mar 2025 00:47:00 +0800 Subject: [PATCH 3/3] chore: increase size limit --- .size-limit.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.size-limit.json b/.size-limit.json index 32a6a58..f8a0842 100644 --- a/.size-limit.json +++ b/.size-limit.json @@ -1,6 +1,6 @@ [ { "path": "./lib/index.js", - "limit": "1.4kB" + "limit": "1.5kB" } ]