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 1 commit
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
72 changes: 72 additions & 0 deletions src/rules/noDuplicateSwitchCaseRule.ts
@@ -0,0 +1,72 @@
/**
* @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);
}
}

const walk = (ctx: Lint.WalkContext<void>): void => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider converting these lambdas to regular function declarations

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason behind the preference for regular function declarations? Elsewhere I've seen folks prefer arrow lambdas for the saner scoping & lack of hoisting weirdness.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rest of the codebase seems to follow the pattern to use function declarations where possible and only use arrow functions where this reference matters.
I prefer regular functions because I think it's easier to notice while scanning through the code

const cb = (node: ts.Node): void => {
if (node.kind === ts.SyntaxKind.CaseBlock) {
visitCaseBlock(node as ts.CaseBlock);
}

ts.forEachChild(node, cb);
};

const visitCaseBlock = (() => {
const previousCases = new Set<string>();

return (node: ts.CaseBlock): void => {
previousCases.clear();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to return a closure or clear the set.
just make previousCases a local variable and loop over the cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a minor performance optimization to keep the same set across iterations. Do you consider the lessened clarity not worth it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would guess clearing the set is as expensive as creating a new one.
I don't think it's worth optimizing here


for (const clause of node.clauses) {
if (clause.kind === ts.SyntaxKind.DefaultClause) {
continue;
}

const text = clause.expression.getText();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider passing ctx.sourceFile to .getText to avoid unnecessary work

if (!previousCases.has(text)) {
previousCases.add(text);
continue;
}

ctx.addFailureAtNode(clause.expression, Rule.FAILURE_STRING_FACTORY(text));
}
};
})();

ts.forEachChild(ctx.sourceFile, cb);
};
82 changes: 82 additions & 0 deletions test/rules/no-duplicate-switch-case/test.ts.lint
@@ -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 % ('"bbb"')]
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
@@ -0,0 +1,5 @@
{
"rules": {
"no-duplicate-switch-case": [true]
}
}