Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Added a no-duplicate-switch-case rule #2937

Merged
merged 8 commits into from
Sep 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/configs/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export const rules = {
"no-construct": true,
"no-debugger": true,
"no-duplicate-super": true,
"no-duplicate-switch-case": true,
"no-duplicate-variable": [
true,
"check-parameters",
Expand Down
3 changes: 3 additions & 0 deletions src/configs/latest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ export const rules = {
"check-rest-spread",
],
},

// added in v5.8.0
"no-duplicate-switch-case": true,
};
// tslint:enable object-literal-sort-keys

Expand Down
3 changes: 1 addition & 2 deletions src/rules/deprecationRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,10 @@ function isDeclaration(identifier: ts.Identifier): boolean {
case ts.SyntaxKind.GetAccessor:
case ts.SyntaxKind.SetAccessor:
case ts.SyntaxKind.EnumDeclaration:
case ts.SyntaxKind.ModuleDeclaration:
return true;
case ts.SyntaxKind.VariableDeclaration:
case ts.SyntaxKind.TypeAliasDeclaration:
case ts.SyntaxKind.Parameter:
case ts.SyntaxKind.ModuleDeclaration:
case ts.SyntaxKind.PropertyDeclaration:
case ts.SyntaxKind.PropertyAssignment:
case ts.SyntaxKind.EnumMember:
Expand Down
66 changes: 66 additions & 0 deletions src/rules/noDuplicateSwitchCaseRule.ts
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));
}
}
}
82 changes: 82 additions & 0 deletions test/rules/no-duplicate-switch-case/test.ts.lint
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'.
5 changes: 5 additions & 0 deletions test/rules/no-duplicate-switch-case/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"no-duplicate-switch-case": [true]
}
}