Skip to content

Commit ff4382b

Browse files
feat: apply fix for no-var in TSModuleBlock (#20638)
1 parent c672a2a commit ff4382b

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

lib/rules/no-var.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,10 @@ module.exports = {
319319
function canFix(node) {
320320
const variables = sourceCode.getDeclaredVariables(node);
321321
const scopeNode = getScopeNode(node);
322+
const parentStatementList = new Set([
323+
...astUtils.STATEMENT_LIST_PARENTS,
324+
"TSModuleBlock",
325+
]);
322326

323327
if (
324328
node.parent.type === "SwitchCase" ||
@@ -347,7 +351,7 @@ module.exports = {
347351
node.parent.type === "ForStatement" &&
348352
node.parent.init === node
349353
) &&
350-
!astUtils.STATEMENT_LIST_PARENTS.has(node.parent.type)
354+
!parentStatementList.has(node.parent.type)
351355
) {
352356
// If the declaration is not in a block, e.g. `if (foo) var bar = 1;`, then it can't be fixed.
353357
return false;

tests/lib/rules/no-var.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,12 +517,19 @@ const ruleTesterTypeScript = new RuleTester({
517517
ruleTesterTypeScript.run("no-var", rule, {
518518
valid: ["declare global { var bar: 'car' }"],
519519
invalid: [
520+
{
521+
code: "declare var x: number",
522+
output: "declare let x: number",
523+
errors: [{ messageId: "unexpectedVar" }],
524+
},
520525
{
521526
code: "declare namespace ns { var bar: 'car' }",
527+
output: "declare namespace ns { let bar: 'car' }",
522528
errors: [{ messageId: "unexpectedVar" }],
523529
},
524530
{
525531
code: "declare module 'module' { var bar: 'car' }",
532+
output: "declare module 'module' { let bar: 'car' }",
526533
errors: [{ messageId: "unexpectedVar" }],
527534
},
528535
],

0 commit comments

Comments
 (0)