forked from palantir/tslint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a no-duplicate-switch-case rule (palantir#2937)
- Loading branch information
Showing
6 changed files
with
158 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* @license | ||
* Copyright 2017 Palantir Technologies, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as ts from "typescript"; | ||
|
||
import * as Lint from "../index"; | ||
|
||
export class Rule extends Lint.Rules.AbstractRule { | ||
public static metadata: Lint.IRuleMetadata = { | ||
description: "Prevents duplicate cases in switch statements.", | ||
optionExamples: [true], | ||
options: null, | ||
optionsDescription: "", | ||
ruleName: "no-duplicate-switch-case", | ||
type: "functionality", | ||
typescriptOnly: false, | ||
}; | ||
|
||
public static readonly FAILURE_STRING_FACTORY = (text: string) => `Duplicate switch case: '${text}'.`; | ||
|
||
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
return this.applyWithFunction(sourceFile, walk); | ||
} | ||
} | ||
|
||
function walk(ctx: Lint.WalkContext<void>): void { | ||
ts.forEachChild(ctx.sourceFile, function cb(node: ts.Node): void { | ||
if (node.kind === ts.SyntaxKind.CaseBlock) { | ||
visitCaseBlock(node as ts.CaseBlock); | ||
} | ||
|
||
ts.forEachChild(node, cb); | ||
}); | ||
|
||
function visitCaseBlock(node: ts.CaseBlock): void { | ||
const previousCases = new Set<string>(); | ||
|
||
for (const clause of node.clauses) { | ||
if (clause.kind === ts.SyntaxKind.DefaultClause) { | ||
continue; | ||
} | ||
|
||
const text = clause.expression.getText(ctx.sourceFile); | ||
if (!previousCases.has(text)) { | ||
previousCases.add(text); | ||
continue; | ||
} | ||
|
||
ctx.addFailureAtNode(clause.expression, Rule.FAILURE_STRING_FACTORY(text)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
const withNumber = (value: number) => { | ||
switch (value): | ||
case 0: | ||
break; | ||
|
||
case 1: | ||
break; | ||
|
||
case 0: | ||
~ [error % ("0")] | ||
case 1: { | ||
~ [error % ("1")] | ||
break; | ||
} | ||
}; | ||
|
||
class WithString { | ||
constructor(param: string) { | ||
switch (param) { | ||
case "aaa": | ||
break; | ||
|
||
case "bbb": | ||
case "ccc": | ||
break; | ||
|
||
case "bbb": | ||
~~~~~ [error % ('"bbb"')] | ||
case "ddd": | ||
switch (param.length) { | ||
case 0: | ||
break; | ||
|
||
case 0: | ||
~ [error % ("0")] | ||
case 1: | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
|
||
case "eee": | ||
case "eee": | ||
~~~~~ [error % ('"eee"')] | ||
break; | ||
|
||
case "default": | ||
break; | ||
|
||
case 0: | ||
break; | ||
|
||
case "1": | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
} | ||
|
||
test(obj: object) { | ||
switch (obj) { | ||
case undefined: | ||
break; | ||
|
||
case null: | ||
case Infinity: | ||
break; | ||
|
||
case this: | ||
case null: | ||
~~~~ [error % ("null")] | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
} | ||
} | ||
|
||
[error]: Duplicate switch case: '%s'. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rules": { | ||
"no-duplicate-switch-case": [true] | ||
} | ||
} |