Skip to content

Commit

Permalink
fix: autofix recursive functions in no-var (#16611)
Browse files Browse the repository at this point in the history
Fixes #16610
  • Loading branch information
mdjermanovic committed Dec 8, 2022
1 parent 293573e commit 9b8bb72
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rules/no-var.js
Expand Up @@ -159,7 +159,7 @@ function hasReferenceInTDZ(node) {
return !reference.init && (
start < idStart ||
(defaultValue !== null && start >= defaultStart && end <= defaultEnd) ||
(start >= initStart && end <= initEnd)
(!astUtils.isFunction(node) && start >= initStart && end <= initEnd)
);
});
};
Expand Down
68 changes: 68 additions & 0 deletions tests/lib/rules/no-var.js
Expand Up @@ -319,6 +319,74 @@ ruleTester.run("no-var", rule, {
code: "function foo() { var { let } = {}; }",
output: null,
errors: [{ messageId: "unexpectedVar" }]
},

// https://github.com/eslint/eslint/issues/16610
{
code: "var fx = function (i = 0) { if (i < 5) { return fx(i + 1); } console.log(i); }; fx();",
output: "let fx = function (i = 0) { if (i < 5) { return fx(i + 1); } console.log(i); }; fx();",
parserOptions: { ecmaVersion: 6, sourceType: "module" },
errors: [{ messageId: "unexpectedVar" }]
},
{
code: "var foo = function () { foo() };",
output: "let foo = function () { foo() };",
parserOptions: { ecmaVersion: 6, sourceType: "module" },
errors: [{ messageId: "unexpectedVar" }]
},
{
code: "var foo = () => foo();",
output: "let foo = () => foo();",
parserOptions: { ecmaVersion: 6, sourceType: "module" },
errors: [{ messageId: "unexpectedVar" }]
},
{
code: "var foo = (function () { foo(); })();",
output: null,
parserOptions: { ecmaVersion: 6, sourceType: "module" },
errors: [{ messageId: "unexpectedVar" }]
},
{
code: "var foo = bar(function () { foo(); });",
output: null,
parserOptions: { ecmaVersion: 6, sourceType: "module" },
errors: [{ messageId: "unexpectedVar" }]
},
{
code: "var bar = foo, foo = function () { foo(); };",
output: null,
parserOptions: { ecmaVersion: 6, sourceType: "module" },
errors: [{ messageId: "unexpectedVar" }]
},
{
code: "var bar = foo; var foo = function () { foo(); };",
output: "let bar = foo; var foo = function () { foo(); };",
parserOptions: { ecmaVersion: 6, sourceType: "module" },
errors: [
{ messageId: "unexpectedVar" },
{ messageId: "unexpectedVar" }
]
},
{
code: "var { foo = foo } = function () { foo(); };",
output: null,
parserOptions: { ecmaVersion: 6, sourceType: "module" },
errors: [{ messageId: "unexpectedVar" }]
},
{
code: "var { bar = foo, foo } = function () { foo(); };",
output: null,
parserOptions: { ecmaVersion: 6, sourceType: "module" },
errors: [{ messageId: "unexpectedVar" }]
},
{
code: "var bar = function () { foo(); }; var foo = function() {};",
output: "let bar = function () { foo(); }; var foo = function() {};",
parserOptions: { ecmaVersion: 6, sourceType: "module" },
errors: [
{ messageId: "unexpectedVar" },
{ messageId: "unexpectedVar" }
]
}
]
});

0 comments on commit 9b8bb72

Please sign in to comment.