Skip to content

Commit b92efb3

Browse files
committed
fix: avoid false positive in no-unassigned-vars for declare module
1 parent 00716a3 commit b92efb3

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

lib/rules/no-unassigned-vars.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,19 @@ module.exports = {
3636
VariableDeclarator(node) {
3737
/** @type {import('estree').VariableDeclaration} */
3838
const declaration = node.parent;
39-
const shouldCheck =
40-
!node.init &&
41-
node.id.type === "Identifier" &&
42-
declaration.kind !== "const" &&
43-
!declaration.declare;
44-
if (!shouldCheck) {
39+
const ancestors = context.sourceCode.getAncestors(node);
40+
const insideDeclareModule = ancestors.some(
41+
ancestorNode =>
42+
ancestorNode.type === "TSModuleDeclaration" &&
43+
ancestorNode.declare === true
44+
);
45+
const shouldSkip =
46+
node.init ||
47+
node.id.type !== "Identifier" ||
48+
declaration.kind === "const" ||
49+
declaration.declare ||
50+
insideDeclareModule;
51+
if (shouldSkip) {
4552
return;
4653
}
4754
const [variable] = sourceCode.getDeclaredVariables(node);

tests/lib/rules/no-unassigned-vars.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ ruleTesterTypeScript.run("no-unassigned-vars", rule, {
140140
}
141141
}
142142
`,
143+
unIndent`
144+
declare module 'module' {
145+
import type { T } from 'module'
146+
let x: T
147+
export = x
148+
}
149+
`,
143150
],
144151
invalid: [
145152
{

0 commit comments

Comments
 (0)