Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Sep 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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