UNPKG

2.98 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7 return extendStatics(d, b);
8 }
9 return function (d, b) {
10 extendStatics(d, b);
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13 };
14})();
15Object.defineProperty(exports, "__esModule", { value: true });
16var ts = require("typescript");
17var Lint = require("tslint");
18var tsutils = require("tsutils");
19var Rule = (function (_super) {
20 __extends(Rule, _super);
21 function Rule() {
22 return _super !== null && _super.apply(this, arguments) || this;
23 }
24 Rule.prototype.apply = function (sourceFile) {
25 if (Rule.isWarningShown === false) {
26 console.warn('Warning: no-duplicate-case rule is deprecated. Replace your usage with the TSLint no-duplicate-switch-case rule.');
27 Rule.isWarningShown = true;
28 }
29 return this.applyWithFunction(sourceFile, walk);
30 };
31 Rule.metadata = {
32 ruleName: 'no-duplicate-case',
33 type: 'maintainability',
34 description: 'Do not use duplicate case labels in switch statements.',
35 options: null,
36 optionsDescription: '',
37 typescriptOnly: true,
38 issueClass: 'Non-SDL',
39 issueType: 'Error',
40 severity: 'Critical',
41 level: 'Opportunity for Excellence',
42 recommendation: 'false',
43 group: 'Deprecated',
44 commonWeaknessEnumeration: '398, 710'
45 };
46 Rule.FAILURE_STRING = 'Duplicate case found in switch statement: ';
47 Rule.isWarningShown = false;
48 return Rule;
49}(Lint.Rules.AbstractRule));
50exports.Rule = Rule;
51function walk(ctx) {
52 function cb(node) {
53 if (tsutils.isSwitchStatement(node)) {
54 var seenLabels_1 = [];
55 node.caseBlock.clauses.forEach(function (clauseOrDefault) {
56 if (tsutils.isCaseClause(clauseOrDefault)) {
57 var clause = clauseOrDefault;
58 if (clause.expression) {
59 var caseText = clause.expression.getText();
60 if (seenLabels_1.indexOf(caseText) > -1) {
61 ctx.addFailureAt(clause.getStart(), clause.getWidth(), Rule.FAILURE_STRING + caseText);
62 }
63 else {
64 seenLabels_1.push(caseText);
65 }
66 }
67 }
68 });
69 }
70 return ts.forEachChild(node, cb);
71 }
72 return ts.forEachChild(ctx.sourceFile, cb);
73}
74//# sourceMappingURL=noDuplicateCaseRule.js.map
\No newline at end of file