Skip to content
Permalink
Browse files

Fix: no-useless-return autofix removes comments (#12292)

  • Loading branch information
mdjermanovic authored and platinumazure committed Sep 29, 2019
1 parent 0e68677 commit 7dc1ea9a1b9a21daaffcf712ba9c0e91af81b906
Showing with 11 additions and 2 deletions.
  1. +3 −2 lib/rules/no-useless-return.js
  2. +8 −0 tests/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,15 +217,15 @@ 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
* entire function to avoid conflicting with
* 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);
}
@@ -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(); ",

0 comments on commit 7dc1ea9

Please sign in to comment.
You can’t perform that action at this time.