Skip to content

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.

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
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
@mdjermanovic
Copy link
Member

Thanks for contributing!

@kirkwaiblinger
Copy link
Contributor Author

Thanks for contributing!

Thanks! This was fun! 🙂

@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Sep 11, 2024
@eslint-github-bot eslint-github-bot bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Sep 11, 2024
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 contributor pool feature This change adds a new feature to ESLint rule Relates to ESLint's core rules

Projects

Archived in project

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