Skip to content

Commit

Permalink
Fix: no-useless-return autofix removes comments (#12292)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic authored and platinumazure committed Sep 29, 2019
1 parent 0e68677 commit 7dc1ea9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/rules/no-useless-return.js
Expand Up @@ -82,6 +82,7 @@ module.exports = {
create(context) {
const segmentInfoMap = new WeakMap();
const usedUnreachableSegments = new WeakSet();
const sourceCode = context.getSourceCode();
let scopeInfo = null;

/**
Expand Down Expand Up @@ -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);
}
Expand Down
8 changes: 8 additions & 0 deletions tests/lib/rules/no-useless-return.js
Expand Up @@ -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(); ",
Expand Down

0 comments on commit 7dc1ea9

Please sign in to comment.