From 7dc1ea9a1b9a21daaffcf712ba9c0e91af81b906 Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Sun, 29 Sep 2019 06:11:57 +0200 Subject: [PATCH] Fix: no-useless-return autofix removes comments (#12292) --- lib/rules/no-useless-return.js | 5 +++-- tests/lib/rules/no-useless-return.js | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/rules/no-useless-return.js b/lib/rules/no-useless-return.js index 46b5f4b648f..7b12e85091c 100644 --- a/lib/rules/no-useless-return.js +++ b/lib/rules/no-useless-return.js @@ -82,6 +82,7 @@ module.exports = { create(context) { const segmentInfoMap = new WeakMap(); const usedUnreachableSegments = new WeakSet(); + const sourceCode = context.getSourceCode(); let scopeInfo = null; /** @@ -216,7 +217,7 @@ module.exports = { loc: node.loc, message: "Unnecessary return statement.", fix(fixer) { - if (isRemovable(node)) { + if (isRemovable(node) && !sourceCode.getCommentsInside(node).length) { /* * Extend the replacement range to include the @@ -224,7 +225,7 @@ module.exports = { * no-else-return. * https://github.com/eslint/eslint/issues/8026 */ - return new FixTracker(fixer, context.getSourceCode()) + return new FixTracker(fixer, sourceCode) .retainEnclosingFunction(node) .remove(node); } diff --git a/tests/lib/rules/no-useless-return.js b/tests/lib/rules/no-useless-return.js index 200f840eb24..1f41f96bb51 100644 --- a/tests/lib/rules/no-useless-return.js +++ b/tests/lib/rules/no-useless-return.js @@ -196,6 +196,14 @@ ruleTester.run("no-useless-return", rule, { code: "function foo() { if (foo) return; }", output: "function foo() { if (foo) return; }" }, + { + code: "function foo() { bar(); return/**/; }", + output: null + }, + { + code: "function foo() { bar(); return//\n; }", + output: null + }, { code: "foo(); return;", output: "foo(); ",