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

[change] Only report first instance of unreachable code #6583

Closed
adamreisnz opened this issue Jul 3, 2016 · 15 comments
Closed

[change] Only report first instance of unreachable code #6583

adamreisnz opened this issue Jul 3, 2016 · 15 comments
Assignees
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 enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules

Comments

@adamreisnz
Copy link

In ESLint 2.11.1 (bundled with Atom linter-eslint), the error for no-unreachable marks every single line of code after the return statement. Maybe this is redundant and it should only mark the first instance of unreachable code? It's pretty obvious that everything after that line will also be unreachable.

Illustration:

image

Perhaps we could control this via a flag/setting, in case some people do prefer to have every line marked.

@eslintbot eslintbot added the triage An ESLint team member will look at this issue soon label Jul 3, 2016
@platinumazure platinumazure added enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion and removed triage An ESLint team member will look at this issue soon labels Jul 3, 2016
@platinumazure
Copy link
Member

I think it's worth discussing for sure. I think having it be an option will make the enhancement more palatable.

@eslint/eslint-team Thoughts?

@adamreisnz
Copy link
Author

I find that one problem with marking every line was that it lead to "error blindness", e.g. I could not spot an actual error anymore because I was trying to ignore all the irrelevant duplicate errors.

@nzakas
Copy link
Member

nzakas commented Jul 4, 2016

I can support this as an option, but think we should keep the current default behavior. 👍

@adambuczynski are you willing to implement this change if others agree?

@adamreisnz
Copy link
Author

@nzakas yeah I could have a look

@mysticatea
Copy link
Member

mysticatea commented Jul 7, 2016

Hm...

At first, I'm sorry that I have not implemented #3307.
But what about that it merges warnings of continuous nodes?
If #3307 is implemented, a warning can show range. Then,

var startNode = null;
var endNode = null;

function reportIfUnreachable(node) {
    var nextNode = null;

    if (node && currentCodePath.currentSegments.every(isUnreachable)) {
        // Store this statement to distinguish consecutive statements.
        if (!startNode) {
            startNode = endNode = node;
            return;
        }

        // Skip if this statement is inside of the current range.
        if (startNode.range[0] <= node.range[0] && node.range[1] <= endNode.range[1]) {
            return;
        }

        // Skip if the previous token of this statement is inside of the current range.
        // This is meant that this statement is consecutive from the current range.
        var prev = context.getTokenBefore(node);
        if (startNode.range[0] <= prev.range[0] && prev.range[1] <= endNode.range[1]) {
            endNode = node;
            return;
        }

        nextNode = node;
    }

    // Report the current range since this node and the current range are not consecutive.
    if (startNode) {
        context.report({
            message: "Unreachable code.",
            loc: {start: startNode.loc.start, end: endNode.loc.end},
            node: startNode
        });
    }

    // Update the current range.
    startNode = endNode = nextNode;
}

@nzakas
Copy link
Member

nzakas commented Jul 8, 2016

@mysticatea I think the problem with that is it will skip over blank lines and comments, so we will still have multiple warnings.

@mysticatea
Copy link
Member

@nzakas It checks whether the previous token of a statement node is included in the current unreachable range, in order to distinguish that a node is consecutive from the current range. So blank lines and comments are not a barrier. e.g.

function foo() {
    return;

    a();  // ← ERROR: Unreachable code. (no-unreachable)

    b()   // ↑ ';' token is included in the unreachable code, so this statement will be merged.
    // comment
    c();  // ↑ ')' token is included in the unreachable code, so this statement will be merged.
}

This is a screenshot of a prototype.

image

image


If there are unreachable codes non-consecutive, there are multiple warnings:

image

@adamreisnz
Copy link
Author

That looks good to me

@nzakas
Copy link
Member

nzakas commented Jul 18, 2016

@mysticatea that looks good. Is that with a new option or by default?

@mysticatea
Copy link
Member

I think it's good for default behavior.
CLI does not show the range of problems, but people can distinguish whether unreachable code(s) exist or not.

@nzakas
Copy link
Member

nzakas commented Jul 19, 2016

Sounds good. We should make sure to update the docs to make it clear what this new behavior is.

@eslint/eslint-team we need a couple more 👍s here.

@markelog
Copy link
Member

SGTM

@ilyavolodin
Copy link
Member

👍

@mysticatea mysticatea self-assigned this Jul 20, 2016
@nzakas nzakas added accepted There is consensus among the team that this change meets the criteria for inclusion and removed evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Jul 20, 2016
@nzakas
Copy link
Member

nzakas commented Jul 20, 2016

Assuming that @markelog's "SGTM" is the same as 👍, marking as accepted.

@adamreisnz
Copy link
Author

Thanks guys 🎉

@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 enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules
Projects
None yet
Development

No branches or pull requests

7 participants