From ba91bb59aec4483faeaf0ec21712aa7206476238 Mon Sep 17 00:00:00 2001 From: Daniel King <89734689+CodeDeficient@users.noreply.github.com> Date: Mon, 26 Oct 2026 08:11:37 -0400 Subject: [PATCH] Skip the export check for shorthand ambient modules A project shim such as `declare module 'lucide-react';` types every import from that module as any, so TypeScript accepts any name. The checker resolves the import to the shim, getExportsOfModule returns nothing, and every imported name was reported as a critical hallucinated_import: 825 false findings on a Next.js app with shims for lucide-react and @radix-ui/react-slot, enough to fail --strict. Fixes #100 Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 1 + imports.ts | 3 +++ tests/rules.test.ts | 15 +++++++++++++++ 3 files changed, 19 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a26e9e..f17362f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ All notable changes will be documented in this file. - `swallowed_error` (medium): a `catch` block whose only statements are `console` calls. The error is logged and the code carries on as if the operation succeeded. A comment in the block explaining why that is safe, a rethrow, or any other statement (such as `setError(e)`) means it is not reported, and so does a fallback after the `try`, as for `empty_catch`. ### Changed +- `hallucinated_import` skips modules declared with a shorthand ambient declaration (`declare module 'lucide-react';`). TypeScript types every import from such a module as `any`, so any name is valid. A Next.js app with shims for `lucide-react` and `@radix-ui/react-slot` got 825 false critical findings, and `--strict` failed its build. - `undeclared_dependency` is not reported in `examples/` folders, whose readers install what each example needs (express's examples import `redis`). An example that imports a package that does not exist on npm is still reported as `nonexistent_package`. - `non_null_assertion` points at the `!` itself. Chained assertions such as `getElementById(id)!.textContent!` were both reported at the start of the expression. - A cast of `[]`, `{}` or `Object.keys(...)` to an array or object type is reported as `type_assertion` (medium), not `unsafe_array_assertion`/`unsafe_object_assertion` (high). `Object.keys(o) as Array` is the standard way to type keys, since `Object.keys` returns `string[]`; ky uses it twice. diff --git a/imports.ts b/imports.ts index 8038880..6aca992 100644 --- a/imports.ts +++ b/imports.ts @@ -555,6 +555,9 @@ export async function checkImports( continue; } + // `declare module 'x';` with no body types every import from 'x' as any, so any name is valid. + if (moduleSymbol.declarations?.some(declaration => ts.isModuleDeclaration(declaration) && declaration.body === undefined)) continue; + const exported = new Set(checker.getExportsOfModule(moduleSymbol).map(symbol => symbol.name)); // Modules typed as `export = something` without members expose nothing we can check. // An ES module with no exports (`export {}`) is still checked. diff --git a/tests/rules.test.ts b/tests/rules.test.ts index 27d7b3d..abda583 100644 --- a/tests/rules.test.ts +++ b/tests/rules.test.ts @@ -607,6 +607,21 @@ test('checkImports: names missing from typed modules are reported, including mul } }); +test('checkImports: a shorthand ambient module (declare module \'x\';) accepts any name', async () => { + // shadcn and Next.js projects add shims like this; TypeScript types every import from them as any. + const dir = makeProject({ + ...fakeReact, + 'tsconfig.json': JSON.stringify({ compilerOptions: { strict: true }, include: ['src', 'types'] }), + 'types/react.d.ts': "declare module 'react';\n", + 'src/a.tsx': "import { useState, AnythingAtAll } from 'react';\nimport React from 'react';\n" + }); + try { + assert.deepEqual(await checkImports([path.join(dir, 'src/a.tsx')], dir, registry), []); + } finally { + fs.rmSync(dir, { recursive: true, force: true }); + } +}); + test('checkImports: a default import from a module with no default export is reported', async () => { const dir = makeProject({ 'package.json': JSON.stringify({ name: 'app', dependencies: { esm: '1.0.0', cjs: '1.0.0', cjsnamed: '1.0.0', withdefault: '1.0.0' } }),