Skip to main content

js/noDuplicateCase

disallow duplicate case labels

ESLint Equivalent: no-duplicate-case

Examples

Invalid

const expr = 'a';
switch (expr) {
	case 'a':
		break;
	case 'b':
		break;
	case 'c':
		break;
	case 'd':
		break;
	case 'c':
		break;
	default:
		break;
}

 file.ts:7:6 lint/js/noDuplicateCase ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  Do not duplicate the "c" case.

    5  case 'b':
    6    break;
  > 7  case 'c':
            ^^^
    8    break;
    9  case 'd':

  Duplicated switch logic paths are hard to follow and usually typos.

  Defined already here

     9  case 'd':
    10    break;
  > 11  case 'c':
             ^^^
    12    break;
    13  default:


const expr = 3;
switch (expr) {
	case 1:
		break;
	case 2:
		break;
	case 3:
		break;
	case 2:
		break;
	default:
		break;
}

 file.ts:5:6 lint/js/noDuplicateCase ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  Do not duplicate the 2 case.

    3  case 1:
    4    break;
  > 5  case 2:
            ^
    6    break;
    7  case 3:

  Duplicated switch logic paths are hard to follow and usually typos.

  Defined already here

     7  case 3:
     8    break;
   > 9  case 2:
             ^
    10    break;
    11  default:


const expr = 3;
switch (expr) {
	case 1:
		break;
	case 2n:
		break;
	case 3:
		break;
	case 2n:
		break;
	default:
		break;
}

 file.ts:5:6 lint/js/noDuplicateCase ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  Do not duplicate the 2n case.

    3  case 1:
    4    break;
  > 5  case 2n:
            ^^
    6    break;
    7  case 3:

  Duplicated switch logic paths are hard to follow and usually typos.

  Defined already here

     7  case 3:
     8    break;
   > 9  case 2n:
             ^^
    10    break;
    11  default:


const foo = 'a';
switch ('a') {
	case foo:
		break;
	case foo:
		break;
	default:
		break;
}

 file.ts:3:6 lint/js/noDuplicateCase ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  Do not duplicate the "a" case.

    1const foo = 'a';
    2switch ('a') {
  > 3  case foo:
            ^^^
    4    break;
    5  case foo:

  Duplicated switch logic paths are hard to follow and usually typos.

  Defined already here

    3  case foo:
    4    break;
  > 5  case foo:
            ^^^
    6    break;
    7  default:


const foo = 'a';
switch ('a') {
	case 'foo':
		break;
	case 'foo':
		break;
	default:
		break;
}

 file.ts:3:6 lint/js/noDuplicateCase ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  Do not duplicate the "foo" case.

    1const foo = 'a';
    2switch ('a') {
  > 3  case 'foo':
            ^^^^^
    4    break;
    5  case 'foo':

  Duplicated switch logic paths are hard to follow and usually typos.

  Defined already here

    3  case 'foo':
    4    break;
  > 5  case 'foo':
            ^^^^^
    6    break;
    7  default:


const foo = 'a';
switch ('a') {
	case null:
		break;
	case null:
		break;
	default:
		break;
}

 file.ts:3:6 lint/js/noDuplicateCase ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  Do not duplicate the null case.

    1const foo = 'a';
    2switch ('a') {
  > 3  case null:
            ^^^^
    4    break;
    5  case null:

  Duplicated switch logic paths are hard to follow and usually typos.

  Defined already here

    3  case null:
    4    break;
  > 5  case null:
            ^^^^
    6    break;
    7  default:

Valid

const expr = 'a';
switch (expr) {
	case 'a':
		break;
	case 'b':
		break;
	case 'c':
		break;
	case 'd':
		break;
	default:
		break;
}
const expr = 3;
switch (expr) {
	case 1:
		break;
	case 2:
		break;
	case 3:
		break;
	case 2n:
		break;
	default:
		break;
}
const expr = 3;
switch (expr) {
	case 1:
		break;
	case '\n1':
		break;
	case 1n:
		break;
	case 'null':
		break;
	case null:
		break;
	case foo:
		break;
	case 'foo':
		break;
	default:
		break;
}