oxc icon indicating copy to clipboard operation
oxc copied to clipboard

no-fallthrough has some false positive effects when multiple cases present

Open pumano opened this issue 1 year ago • 4 comments

in some edge cases (looks like problem in || and &&):

    const notification = { type: 'PRIMARY' };

    switch (true) {
      case notification.type === 'PRIMARY' || notification.type === 'SECONDARY':
        // TODO
        break;
      case notification.type === 'SECONDARY':
        // TODO
        break;
      case notification.type === 'OUTLINE' || notification.type === 'SECONDARY':
        // TODO
        break;
      case notification.type === 'ETC':
        // TODO
        break;
    }
    const notification = { type: 'PRIMARY' };
    const other = true;

    switch (true) {
      case notification.type === 'PRIMARY' && other:
        // TODO
        break;
      case notification.type === 'SECONDARY':
        // TODO
        break;
      case notification.type === 'OUTLINE' && other:
        // TODO
        break;
      case notification.type === 'ETC':
        // TODO
        break;
    }

pumano avatar Oct 10 '24 05:10 pumano

@Boshen

Could I take this issue?

I have reproduced the different behavior between eslint and oxlint for the no-fallthrough rule.

kth496 avatar Oct 12 '24 09:10 kth496

@Boshen @rzvxa I've been looking into this issue and am having the following difficulties.

In the switch-case syntax, when the condition contains a logical expression, the rule is not checked because the tests variable is not populated with the appropriate value. https://github.com/oxc-project/oxc/blob/a9260cf6d1b83917c7a61b25cabd2d40858b0fff/crates/oxc_linter/src/rules/eslint/no_fallthrough.rs#L269

The tests is generated from the get_switch_semantic_cases function, and the comments above this function say to treat this function as a black box, which is not an easy situation to fix.

Can I get some help to resolve this issue?

kth496 avatar Oct 14 '24 14:10 kth496

@Boshen @rzvxa I've been looking into this issue and am having the following difficulties.

In the switch-case syntax, when the condition contains a logical expression, the rule is not checked because the tests variable is not populated with the appropriate value.

https://github.com/oxc-project/oxc/blob/a9260cf6d1b83917c7a61b25cabd2d40858b0fff/crates/oxc_linter/src/rules/eslint/no_fallthrough.rs#L269

The tests is generated from the get_switch_semantic_cases function, and the comments above this function say to treat this function as a black box, which is not an easy situation to fix.

Can I get some help to resolve this issue?

Yeah, We have some limitations at the moment that makes accessing cases really difficult. Feel free to tinker with that demonic function to get it to work.

Let me know how I can help you with this.

rzvxa avatar Oct 15 '24 15:10 rzvxa

@rzvxa I thought modifying that function was prohibited, I'll see if I can improve it, thanks.

kth496 avatar Oct 17 '24 13:10 kth496

@kth496 do you have a PR? I'll let someone else work on this if you don't have the time.

Boshen avatar Dec 05 '24 09:12 Boshen

@Boshen I have spent quite some time reviewing the codebase with great interest, but unfortunately, I was unable to make progress on the work. Could you kindly unassign me?

kth496 avatar Dec 06 '24 13:12 kth496

There seems to be something wrong with this case as well, but it works in ESLint (v9.17.0).

oxlint version: 0.15.5

// index.ts
export const getLangByCodeType = (codeType: string): string => {
  switch (codeType) {
    case 'Apache Maven':
    case 'Apache Ivy':
    default:
      return 'xml'

    case 'Gradle Groovy DSL':
    case 'Gradle Kotlin DSL':
      return 'groovy'

    case 'Scala SBT':
      return 'scala'

    case 'Composer':
      return 'json'
  }
}
// .oxlintrc.json
{
  "rules": {
    "no-fallthrough": ["error", { "allowEmptyCase": true }]
  }
}

image

vikiboss avatar Jan 13 '25 02:01 vikiboss

#11315

ityuany avatar May 27 '25 06:05 ityuany

@vikiboss do you mind making a new issue for that case? i couldn't reproduce it, but if feels separate to the original one here

camc314 avatar May 27 '25 11:05 camc314

@vikiboss do you mind making a new issue for that case? i couldn't reproduce it, but if feels separate to the original one here

Sure! I'll open a new issue with a minimal reproduction repo to demonstrate the bug.

vikiboss avatar May 28 '25 01:05 vikiboss

done. https://github.com/oxc-project/oxc/issues/11340

vikiboss avatar May 28 '25 02:05 vikiboss

fixed via #14811

camc314 avatar Oct 20 '25 12:10 camc314