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

New: no-useless-return rule (fixes #7309) #7441

Merged
merged 10 commits into from Oct 28, 2016
Merged

Conversation

mysticatea
Copy link
Member

What is the purpose of this pull request? (put an "X" next to item)

[X] New rule (template)

See #7309 for the template.

What changes did you make? (Give an overview)

This PR implement no-useless-return rule.
The logic is:

  1. At ReturnStatement nodes which may be useless, it stores the node into both the current code path segment and the current code path.
  2. Every new code path segment inherits the ReturnStatement nodes from the previous segments.
    • If a previous segment is unreachable and the previous segment was terminated by a ReturnStatement, the new segment inherits ReturnStatement nodes from the unreachable segment. If the unreachable segment was terminated by other kinds of statements, the new segment ignores the unreachable segment. This is the emulation for the case that ReturnStatement nodes don't exist.
  3. At every statement nodes, it removes ReturnStatement nodes which are on the current segments from the code path.
  4. At the end of a code path, it reports remaining ReturnStatement nodes of the code path.

In addition, this PR enable no-useless-return rule for dogfooding.

Is there anything you'd like reviewers to focus on?

  • Other examples for tests which should cover.
  • no-useless-return rule seems to conflict to callback-return rule. Please check 52f8391. Thought about that?

@mysticatea mysticatea added rule Relates to ESLint's core rules accepted There is consensus among the team that this change meets the criteria for inclusion feature This change adds a new feature to ESLint labels Oct 24, 2016
@mention-bot
Copy link

@mysticatea, thanks for your PR! By analyzing the history of the files in this pull request, we identified @IanVS, @vitorbal and @kaicataldo to be potential reviewers.

@eslintbot
Copy link

LGTM

Copy link
Member

@not-an-aardvark not-an-aardvark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This mostly looks good, but I have a few questions.

@@ -0,0 +1,83 @@
# Disallow redundant return statements (no-useless-return)

A `return;` statement with nothing after it is redundant, and has no effect on the runtime behavior of a function. This can be confusing, so it's better to disallow these redundant statements
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I forgot a . at the end of the sentence: "to disallow these redundant statements**.**"

* @param {ASTNode} node - The return statement node to check.
* @returns {boolean} `true` if the node is removeable.
*/
function isRemoveable(node) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think the word is "removable" (no "e" in the middle).

ForOfStatement: markReturnStatementsOnCurrentSegmentsAsUsed,
ForStatement: markReturnStatementsOnCurrentSegmentsAsUsed,
IfStatement: markReturnStatementsOnCurrentSegmentsAsUsed,
ImportDeclaration: markReturnStatementsOnCurrentSegmentsAsUsed,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do ImportDeclaration nodes need to be on this list? I think import declarations are hoisted, so the following would be redundant:

import {foo} from 'bar';

return;

However, I'm not sure about the correct semantics, because I don't know of any environments with globalReturn: true that support import.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also don't know such environments 😃 .
In this case, I guessed ImportDeclarations behaves as similar to VariableDeclarations. On the other hand, even if globalReturn: true does not support import, this will not trigger false positive/negative.

@eslintbot
Copy link

LGTM

@eslintbot
Copy link

LGTM

Copy link
Member

@not-an-aardvark not-an-aardvark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@eslintbot
Copy link

LGTM

@@ -651,7 +651,7 @@ module.exports = {
if (elements.length > 0 && node.type === "ArrayExpression") {
elements.forEach(el => {
if (el.loc.start.line === node.loc.start.line) {
return;
return; // eslint-disable-line no-useless-return
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I'm not sure what the correct fix is.
I tried to change the forEach to a for-of statement, but several tests got failing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can just remove the entire elements.forEach block (see #7237 (comment))

@eslintbot
Copy link

LGTM

@mysticatea
Copy link
Member Author

OK, ready to merge.
Thank you, �@not-an-aardvark .

@ilyavolodin ilyavolodin merged commit d933516 into master Oct 28, 2016
@mysticatea mysticatea deleted the no-useless-return-7309 branch October 29, 2016 10:58
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 6, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion feature This change adds a new feature to ESLint rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants