-
Notifications
You must be signed in to change notification settings - Fork 889
Conversation
95f291b
to
80b2714
Compare
80b2714
to
40dc01e
Compare
src/rules/switchFinalBreakRule.ts
Outdated
Enforces consistent use of 'break;' in the final clause of a 'switch' statement. | ||
A 'default' clause should never have a 'break'. | ||
A 'case' clause should always have a 'break' (or other control flow exit) even if it is the last clause.`, | ||
optionsDescription: "Not configurable.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider making this rule configurable with options like "always" and "never" (and if you really want it, you can add another option to handle default
different. although I don't understand why the default clause should be handled differently at all).
For the record, I'd prefer setting this rule to "never" in this repository to avoid unnecessary statements.
1c7faa9
to
817179a
Compare
src/rules/switchFinalBreakRule.ts
Outdated
public static metadata: Lint.IRuleMetadata = { | ||
ruleName: "switch-final-break", | ||
description: Lint.Utils.dedent` | ||
Forbids the final clause of a switch statement to have an unnecessary \`break;\`.`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs to be updated to sth like: Forbids or requires ...
} | ||
} | ||
|
||
function last<T>(arr: ReadonlyArray<T>): T | undefined { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know why you need this one, because you still have to check for undefined
(instead of arr.length
without this function), but I don't have a problem with it being there
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to ask forgiveness than permission. I'd rather check for an undefined return value than to have some test for whether the function I'm about to call should return a defined value.
@@ -0,0 +1,46 @@ | |||
switch (x) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a test where the last clause is empty
src/rules/switchFinalBreakRule.ts
Outdated
|
||
function check(node: ts.CaseBlock): void { | ||
const clause = last(node.clauses); | ||
if (clause === undefined || clause.statements.length === 0) { return; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if always
is specified, the rule should also complain if the final clause is empty
PR checklist
Overview of change:
Added the 'switch-final-break' lint rule, which:
case
, enforces 'no-switch-case-fall-through' even for the last case.default:
clause, forbids a uselessbreak;
.CHANGELOG.md entry:
[new-rule]
switch-final-break