SonarSource / eslint-plugin-sonarjs

SonarJS rules for ESLint
GNU Lesser General Public License v3.0
1.2k stars 73 forks source link

`no-nested-switch` false-positive with lambda #397

Closed Lonli-Lokli closed 1 year ago

Lonli-Lokli commented 1 year ago

I want to report a bug.

Reproducer

// minimal reproducer if relevant
type Executor = {
  process: (onMessage: (message: 'old' | 'new') => void) => boolean;
};

export const onHandler = (command: 'add' | 'remove', item: any, executor: Executor) => {
  switch (command) {
    case 'add':
      return executor.process((message) => {
        switch (message) {// False-positive here for no-nested-switch
          case 'new':
            return item !== null;
            break;
          case 'old':
            return true;
        }
      })
    case 'remove':
      break;
  }
}

Expected behavior

no error as those switches are in different contexts

eslint-plugin-sonarjs version: 0.19.0
eslint version: 8.15.0
Node.js version: v16.19.1

Rule key: no-nested-switch

ilia-kebets-sonarsource commented 1 year ago

Hello @Lonli-Lokli,

Technically, you are right that there is indeed a function declaration in between those switches. However, the rule is about readability and therefore we assess this case as a true positive.