From ae876257d97ba460a549a802d6f44117d1db144c Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Sun, 24 Jan 2021 19:10:30 +0530 Subject: [PATCH 1/2] Fix: extend fixer range of prefer const to whole declaration --- lib/rules/prefer-const.js | 22 ++++++++++++++++++---- tests/lib/rules/prefer-const.js | 7 +++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/lib/rules/prefer-const.js b/lib/rules/prefer-const.js index 439a4db3c96..3cccd2d21a5 100644 --- a/lib/rules/prefer-const.js +++ b/lib/rules/prefer-const.js @@ -5,6 +5,11 @@ "use strict"; +//------------------------------------------------------------------------------ +// Requirements +//------------------------------------------------------------------------------ + +const FixTracker = require("./utils/fix-tracker"); const astUtils = require("./utils/ast-utils"); //------------------------------------------------------------------------------ @@ -451,10 +456,19 @@ module.exports = { messageId: "useConst", data: node, fix: shouldFix - ? fixer => fixer.replaceText( - sourceCode.getFirstToken(varDeclParent, t => t.value === varDeclParent.kind), - "const" - ) + ? fixer => { + const letKeywordToken = sourceCode.getFirstToken(varDeclParent, t => t.value === varDeclParent.kind); + + + /** + * Extend the replacement range to the whole declaration, + * in order to prevent other fixes in the same pass + * https://github.com/eslint/eslint/issues/13899 + */ + return new FixTracker(fixer, sourceCode) + .retainRange(varDeclParent.range) + .replaceTextRange(letKeywordToken.range, "const"); + } : null }); }); diff --git a/tests/lib/rules/prefer-const.js b/tests/lib/rules/prefer-const.js index ad25f5f4830..449e7e0651f 100644 --- a/tests/lib/rules/prefer-const.js +++ b/tests/lib/rules/prefer-const.js @@ -546,6 +546,13 @@ ruleTester.run("prefer-const", rule, { { message: "'bar' is never reassigned. Use 'const' instead.", type: "Identifier" }, { message: "'bar' is never reassigned. Use 'const' instead.", type: "Identifier" } ] + }, + + // https://github.com/eslint/eslint/issues/13899 + { + code: "/*eslint no-undef-init:error*/ let foo = undefined;", + output: "/*eslint no-undef-init:error*/ const foo = undefined;", + errors: 2 } ] }); From a22375beae7eca0eba1c7f89c139e1f763eae391 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Mon, 25 Jan 2021 11:12:47 +0530 Subject: [PATCH 2/2] Chore: update prefer-const test case --- lib/rules/prefer-const.js | 1 - tests/lib/rules/prefer-const.js | 4 +++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/rules/prefer-const.js b/lib/rules/prefer-const.js index 3cccd2d21a5..f6e79e71c3e 100644 --- a/lib/rules/prefer-const.js +++ b/lib/rules/prefer-const.js @@ -459,7 +459,6 @@ module.exports = { ? fixer => { const letKeywordToken = sourceCode.getFirstToken(varDeclParent, t => t.value === varDeclParent.kind); - /** * Extend the replacement range to the whole declaration, * in order to prevent other fixes in the same pass diff --git a/tests/lib/rules/prefer-const.js b/tests/lib/rules/prefer-const.js index 449e7e0651f..c591d236583 100644 --- a/tests/lib/rules/prefer-const.js +++ b/tests/lib/rules/prefer-const.js @@ -500,9 +500,11 @@ ruleTester.run("prefer-const", rule, { { message: "'b' is never reassigned. Use 'const' instead.", type: "Identifier" } ] }, + + // The inner `let` will be auto-fixed in the second pass { code: "let someFunc = () => { let a = 1, b = 2; foo(a, b) }", - output: "const someFunc = () => { const a = 1, b = 2; foo(a, b) }", + output: "const someFunc = () => { let a = 1, b = 2; foo(a, b) }", errors: [ { message: "'someFunc' is never reassigned. Use 'const' instead.", type: "Identifier" }, { message: "'a' is never reassigned. Use 'const' instead.", type: "Identifier" },