Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<keyof typeof o>` is the standard way to type keys, since `Object.keys` returns `string[]`; ky uses it twice.
Expand Down
3 changes: 3 additions & 0 deletions imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions tests/rules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' } }),
Expand Down
Loading