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 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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]
}
}