108 lines
1.6 KiB
Plaintext
108 lines
1.6 KiB
Plaintext
== Why is this an issue?
|
|
|
|
include::../description.adoc[]
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,javascript]
|
|
----
|
|
switch (param) { //missing default clause
|
|
case 0:
|
|
doSomething();
|
|
break;
|
|
case 1:
|
|
doSomethingElse();
|
|
break;
|
|
}
|
|
|
|
switch (param) {
|
|
default: // default clause should be the last one
|
|
error();
|
|
break;
|
|
case 0:
|
|
doSomething();
|
|
break;
|
|
case 1:
|
|
doSomethingElse();
|
|
break;
|
|
}
|
|
----
|
|
|
|
=== Compliant solution
|
|
|
|
[source,javascript]
|
|
----
|
|
switch (param) {
|
|
case 0:
|
|
doSomething();
|
|
break;
|
|
case 1:
|
|
doSomethingElse();
|
|
break;
|
|
default:
|
|
error();
|
|
break;
|
|
}
|
|
----
|
|
|
|
=== Exceptions
|
|
|
|
The rule ignores ``++switch++`` statements where the discriminant is a TypeScript union and there is a ``++case++`` branch for each constituent.
|
|
|
|
[source,javascript]
|
|
----
|
|
type Season = 'Spring' | 'Summer' | 'Fall' | 'Winter';
|
|
let season: Season;
|
|
switch (season) {
|
|
case 'Spring':
|
|
wakeUp();
|
|
break;
|
|
case 'Summer':
|
|
getOut();
|
|
break;
|
|
case 'Fall':
|
|
saveFood();
|
|
break;
|
|
case 'Winter':
|
|
sleep();
|
|
break;
|
|
}
|
|
----
|
|
|
|
The same applies for TypeScript enums.
|
|
|
|
[source,javascript]
|
|
----
|
|
enum Direction {
|
|
Up,
|
|
Down
|
|
}
|
|
|
|
let dir: Direction;
|
|
switch (dir) {
|
|
case Direction.Up:
|
|
getUp();
|
|
break;
|
|
case Direction.Down:
|
|
getDown();
|
|
break;
|
|
}
|
|
----
|
|
|
|
include::../see.adoc[]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::../message.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::../comments-and-links.adoc[]
|
|
endif::env-github,rspecator-view[]
|