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

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

Closed
1 task done
kirkwaiblinger opened this issue Mar 9, 2024 · 4 comments · Fixed by #18188
Closed
1 task done
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules

Comments

@kirkwaiblinger
Copy link
Contributor

kirkwaiblinger commented Mar 9, 2024

What rule do you want to change?

no-fallthrough

What change do you want to make?

Generate more warnings

How do you think the change should be implemented?

A new default behavior

Example code

const aVariable = "something";
switch (aVariable) {
  case 1:
    console.log('did something in case 1 that will not fall through to case 2!')
    break;
    // fallthrough
  case 2:
    console.log('did something in case 2!')
    break;
}

What does the rule currently do for this code?

No warnings

What will the rule do after it's changed?

the // fallthrough comment should be flagged, because it incorrectly implies to a reader that a fallthrough can/will occur.

Participation

  • I am willing to submit a pull request to implement this change.

Additional comments

This very similar in concept to --report-unused-disable-directives, so I'm not terribly sure whether it ought to flag as part of the rule per se, or when running --report-unused-disable-directives. But I think that having this would be especially valuable for cases with a multiple branches, where it doesn't literally terminate in a break/return. For example, something like

function myBigConfusingSwitch() {
  const aVariable = "something";
  switch (aVariable) {
    case 1:
      if (Math.random() > 0.5) {
        if (Math.random() > 0.5) {
          console.log('do something!')
          return 42;
        } else if (Math.random() > 0.5) {
          console.log('do something!');
          break;
        } else {
          break;
        }
      } else {
        return 4242
      }
      // fallthrough
    case 2:
      // did I fall through or not?
      console.log('do something in case 2!')
      break;
  }
}

Note that if I put code where the fallthrough comment is, it would get flagged for being unreachable

PS I discovered this organically, making this very mistake during refactoring

@kirkwaiblinger kirkwaiblinger added enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules labels Mar 9, 2024
@Tanujkanti4441
Copy link
Contributor

Hi @kirkwaiblinger, thanks for the issue, i think reporting comments shouldn't be the default behavior of no-fallthrough rule as it mainly reports fallthrough from switch cases, but an option can be added for this.

@mdjermanovic
Copy link
Member

the // fallthrough comment should be flagged, because it incorrectly implies to a reader that a fallthrough can/will occur

Makes sense to me 👍

I agree with @Tanujkanti4441 that this behavior should be behind an option.

@eslint/eslint-team thoughts about this?

@JoshuaKGoldberg
Copy link
Contributor

👍 agreed this is a good idea for an opt-in.

kirkwaiblinger added a commit to kirkwaiblinger/eslint that referenced this issue Mar 10, 2024
kirkwaiblinger added a commit to kirkwaiblinger/eslint that referenced this issue Mar 10, 2024
@fasttime
Copy link
Member

It looks like there is consensus to add the ability to flag useless // fallthrough comments behind an option. @kirkwaiblinger feel free to submit a PR.

@fasttime fasttime added the accepted There is consensus among the team that this change meets the criteria for inclusion label Mar 11, 2024
mdjermanovic added a commit that referenced this issue Mar 14, 2024
…ule (#18188)

* feat: (no-fallthrough) Report unused fallthrough comments

fixes #18182

* add space

* add a few test cases to ensure state doesn't leak across multiple switches

* add correct case in docs

* fix leaked state

* Fix docs typo

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* add some test coverage

---------

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
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 enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

5 participants