Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 docs/src/rules/no-unassigned-vars.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ In TypeScript:

declare let value: number | undefined;
console.log(value);

declare module "my-module" {
let value: string;
export = value;
}
```

:::
Expand Down
19 changes: 13 additions & 6 deletions lib/rules/no-unassigned-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,19 @@ module.exports = {
VariableDeclarator(node) {
/** @type {import('estree').VariableDeclaration} */
const declaration = node.parent;
const shouldCheck =
!node.init &&
node.id.type === "Identifier" &&
declaration.kind !== "const" &&
!declaration.declare;
if (!shouldCheck) {
const ancestors = context.sourceCode.getAncestors(node);
const insideDeclareModule = ancestors.some(
ancestorNode =>
ancestorNode.type === "TSModuleDeclaration" &&
ancestorNode.declare === true,
);
const shouldSkip =
node.init ||
node.id.type !== "Identifier" ||
declaration.kind === "const" ||
declaration.declare ||
insideDeclareModule;
if (shouldSkip) {
return;
}
const [variable] = sourceCode.getDeclaredVariables(node);
Expand Down
7 changes: 7 additions & 0 deletions tests/lib/rules/no-unassigned-vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ ruleTesterTypeScript.run("no-unassigned-vars", rule, {
}
}
`,
unIndent`
declare module 'module' {
import type { T } from 'module';
let x: T;
export = x;
}
`,
],
invalid: [
{
Expand Down
Loading