Skip to content

feat: throw error on malformed tsconfig reference #423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wise-singers-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-import-resolver-typescript": minor
---

feat: throw error on malformed `tsconfig` reference
2 changes: 1 addition & 1 deletion .size-limit.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"path": "./lib/index.js",
"limit": "1.4kB"
"limit": "1.5kB"
}
]
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/malformed-reference/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"references": [
{
"path": "./non-existed"
}
]
}
25 changes: 21 additions & 4 deletions tests/unit/unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@ 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: {
cwd: pnpDir,
},
})

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(`
Expand All @@ -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,
)
})
})