Skip to content

Commit dbba058

Browse files
authored
feat: allow global type declaration in no-var (#19714)
1 parent 152ed51 commit dbba058

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

‎docs/src/rules/no-var.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,42 @@ const CONFIG = {};
5454

5555
:::
5656

57+
This rule additionally supports TypeScript type syntax. There are multiple ways to declare global variables in TypeScript. Only using `var` works for all cases. See this [TypeScript playground](https://www.typescriptlang.org/play/?#code/PQgEB4CcFMDNpgOwMbVAGwJYCMC8AiAEwHsBbfUYAPgFgAoew6ZdAQxlAHN1jtX1QAb3qhRGaABdQAGUkAuUAGcJkTIk4ixAN3agAauwXLV6+ptFqJCWK1SgA6mpIB3IebGjHiIyrUa6HgC+9MEMdGDcvPygtqiKivSyEvQGkPReZuHAoM5OxK6ckvS5iC4AdEnFec5lqVWl+WUZYWAlLkpFdG2NSaC4oADkA-XlqX2Dw13VTWrjQ5kRPHzoACoAFpiKXJ2Ry+ubFTtL-PuKtez0uycbZ830i1GrNx3JdFdPB73982-HH2djb6Td6nGaIOaTe7ZRTQdCwbavGFww6I2Gwc5pOhI9F3LIdOEvejYlEQolojGkrHkryU+jQAAeAAdiJApIJAkA) for reference.
58+
59+
Examples of **incorrect** TypeScript code for this rule:
60+
61+
:::incorrect
62+
63+
```ts
64+
/*eslint no-var: "error"*/
65+
66+
declare var x: number
67+
68+
declare namespace ns {
69+
var x: number
70+
}
71+
72+
declare module 'module' {
73+
var x: number
74+
}
75+
```
76+
77+
:::
78+
79+
Examples of **correct** TypeScript code for this rule:
80+
81+
:::correct
82+
83+
```ts
84+
/*eslint no-var: "error"*/
85+
86+
declare global {
87+
declare var x: number
88+
}
89+
```
90+
91+
:::
92+
5793
## When Not To Use It
5894

5995
In addition to non-ES6 environments, existing JavaScript projects that are beginning to introduce ES6 into their

‎lib/rules/no-var.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ function hasNameDisallowedForLetDeclarations(variable) {
199199
module.exports = {
200200
meta: {
201201
type: "suggestion",
202+
dialects: ["typescript", "javascript"],
203+
language: "javascript",
202204

203205
docs: {
204206
description: "Require `let` or `const` instead of `var`",
@@ -346,9 +348,19 @@ module.exports = {
346348

347349
return {
348350
"VariableDeclaration:exit"(node) {
349-
if (node.kind === "var") {
350-
report(node);
351+
if (node.kind !== "var") {
352+
return;
351353
}
354+
355+
if (
356+
node.parent.type === "TSModuleBlock" &&
357+
node.parent.parent.type === "TSModuleDeclaration" &&
358+
node.parent.parent.global
359+
) {
360+
return;
361+
}
362+
363+
report(node);
352364
},
353365
};
354366
},

‎tests/lib/rules/no-var.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,3 +431,23 @@ ruleTester.run("no-var", rule, {
431431
},
432432
],
433433
});
434+
435+
const ruleTesterTypeScript = new RuleTester({
436+
languageOptions: {
437+
parser: require("@typescript-eslint/parser"),
438+
},
439+
});
440+
441+
ruleTesterTypeScript.run("no-var", rule, {
442+
valid: ["declare global { var bar: 'car' }"],
443+
invalid: [
444+
{
445+
code: "declare namespace ns { var bar: 'car' }",
446+
errors: [{ messageId: "unexpectedVar" }],
447+
},
448+
{
449+
code: "declare module 'module' { var bar: 'car' }",
450+
errors: [{ messageId: "unexpectedVar" }],
451+
},
452+
],
453+
});

0 commit comments

Comments
 (0)