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

feat: add reportUnusedFallthroughComment option to no-fallthrough rule #18188

Merged
merged 7 commits into from Mar 14, 2024

Conversation

kirkwaiblinger
Copy link
Contributor

fixes #18182

Prerequisites checklist

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

[ ] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[x] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

#18182

What changes did you make? (Give an overview)

Add option (disabled by default) to report unused fallthrough comments in no-fallthrough.

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

Changes are pretty straightforward I think.

PS this is my first PR to ESLint! very exciting

@kirkwaiblinger kirkwaiblinger requested a review from a team as a code owner March 11, 2024 13:13
Copy link

linux-foundation-easycla bot commented Mar 11, 2024

CLA Signed

The committers listed above are authorized under a signed CLA.

@eslint-github-bot eslint-github-bot bot added the feature This change adds a new feature to ESLint label Mar 11, 2024
@github-actions github-actions bot added the rule Relates to ESLint's core rules label Mar 11, 2024
Copy link

netlify bot commented Mar 11, 2024

Deploy Preview for docs-eslint canceled.

Name Link
🔨 Latest commit afed1bf
🔍 Latest deploy log https://app.netlify.com/sites/docs-eslint/deploys/65f1e3803ee9a80008020c00

@mdjermanovic mdjermanovic added accepted There is consensus among the team that this change meets the criteria for inclusion contributor pool labels Mar 11, 2024
Comment on lines +273 to +275

:::

Copy link
Contributor

Choose a reason for hiding this comment

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

i think we can add a correct section here with an example having a comment that doesn't permit fallthrough.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

makes sense! Added

Comment on lines +209 to +217
const isSwitchExitReachable = isAnySegmentReachable(currentCodePathSegments);
const isFallthrough = isSwitchExitReachable && (node.consequent.length > 0 || (!allowEmptyCase && hasBlankLinesBetween(node, nextToken))) &&
node.parent.cases.at(-1) !== node;

previousCase = {
node,
isSwitchExitReachable,
isFallthrough
};
Copy link
Member

Choose a reason for hiding this comment

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

The state can leak across multiple switches this way:

/* eslint no-fallthrough: ["error", { "reportUnusedFallthroughComment": true }] */

switch(foo){
    case 1:
        doSomething();
        break;
}

function f() {
    switch(foo){
        // falls through comment false positive
        case 1:
            if (a) {
                throw new Error();
            } else if (b) {
                break;
            } else {
                return;
            }
        case 2:
            break;
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh dear. And I thought I was being all clever by adding those test cases 🤦

Fixed!

@mdjermanovic mdjermanovic changed the title feat: (no-fallthrough) Report unused fallthrough comments feat: add reportUnusedFallthroughComment option to no-fallthrough rule Mar 13, 2024
Copy link
Member

@mdjermanovic mdjermanovic left a comment

Choose a reason for hiding this comment

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

Just one typo in the docs to fix and a couple of tests I think we should add, then LGTM.

docs/src/rules/no-fallthrough.md Outdated Show resolved Hide resolved
Comment on lines +479 to +495
{
code: `
switch(foo){
case 1:
doSomething();
break;
// falls through
case 2: doSomething();
}`,
options: [{ reportUnusedFallthroughComment: true }],
errors: [
{
line: 6,
messageId: "unusedFallthroughComment"
}
]
}, {
Copy link
Member

Choose a reason for hiding this comment

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

Can you also add a test where a fallthrough comment in a block is reported, for example:

        {
            code: `
switch (foo) {
    case 0: {
        a();
        break;
        // falls through
    }
    case 1:
        b();
}`,
            options: [{ reportUnusedFallthroughComment: true }],
            errors: [
                {
                    line: 6,
                    messageId: "unusedFallthroughComment"
                }
            ]
        },

Copy link
Contributor Author

Choose a reason for hiding this comment

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

makes sense! added

Comment on lines +176 to +188
{
code: `
switch(foo){
case 1:
doSomething();
break;
// just a comment
case 2: doSomething();
}
`,
options: [{ reportUnusedFallthroughComment: true }]

},
Copy link
Member

Choose a reason for hiding this comment

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

Can you also add a valid test with a fallthrough comment, for example:

        {
            code: `
switch(foo){
    case 1:
        doSomething();
        // falls through
    case 2: doSomething();
}
          `,
            options: [{ reportUnusedFallthroughComment: true }]

        },

Copy link
Contributor Author

Choose a reason for hiding this comment

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

makes sense! added

kirkwaiblinger and others added 2 commits March 13, 2024 11:30
Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
Copy link
Member

@mdjermanovic mdjermanovic 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! Leaving open for @Tanujkanti4441 to verify.

Copy link
Contributor

@Tanujkanti4441 Tanujkanti4441 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.

@mdjermanovic mdjermanovic merged commit b8fb572 into eslint:main Mar 14, 2024
19 checks passed
@mdjermanovic
Copy link
Member

Thanks for contributing!

@kirkwaiblinger
Copy link
Contributor Author

Thanks for contributing!

Thanks! This was fun! 🙂

@kirkwaiblinger kirkwaiblinger deleted the eslint-18182 branch April 30, 2024 04:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion contributor pool feature This change adds a new feature to ESLint rule Relates to ESLint's core rules
Projects
Status: Complete
Development

Successfully merging this pull request may close these issues.

Rule Change: no-fallthrough should not permit "falls through" comment when code will not fall through.
3 participants