.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / noSwitchCaseFallThroughRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2013 Palantir Technologies, Inc.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 Object.defineProperty(exports, "__esModule", { value: true });
19 var tslib_1 = require("tslib");
20 var utils = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 var Rule = /** @class */ (function (_super) {
24     tslib_1.__extends(Rule, _super);
25     function Rule() {
26         return _super !== null && _super.apply(this, arguments) || this;
27     }
28     /* tslint:enable:object-literal-sort-keys */
29     Rule.FAILURE_STRING = function (keyword) {
30         return "expected a 'break' before '" + ts.tokenToString(keyword) + "'";
31     };
32     Rule.prototype.apply = function (sourceFile) {
33         return this.applyWithWalker(new NoSwitchCaseFallThroughWalker(sourceFile, this.ruleName, undefined));
34     };
35     /* tslint:disable:object-literal-sort-keys */
36     Rule.metadata = {
37         ruleName: "no-switch-case-fall-through",
38         description: "Disallows falling through case statements.",
39         descriptionDetails: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n            For example, the following is not allowed:\n\n            ```ts\n            switch(foo) {\n                case 1:\n                    someFunc(foo);\n                case 2:\n                    someOtherFunc(foo);\n            }\n            ```\n\n            However, fall through is allowed when case statements are consecutive or\n            a magic `/* falls through */` comment is present. The following is valid:\n\n            ```ts\n            switch(foo) {\n                case 1:\n                    someFunc(foo);\n                    /* falls through */\n                case 2:\n                case 3:\n                    someOtherFunc(foo);\n            }\n            ```"], ["\n            For example, the following is not allowed:\n\n            \\`\\`\\`ts\n            switch(foo) {\n                case 1:\n                    someFunc(foo);\n                case 2:\n                    someOtherFunc(foo);\n            }\n            \\`\\`\\`\n\n            However, fall through is allowed when case statements are consecutive or\n            a magic \\`/* falls through */\\` comment is present. The following is valid:\n\n            \\`\\`\\`ts\n            switch(foo) {\n                case 1:\n                    someFunc(foo);\n                    /* falls through */\n                case 2:\n                case 3:\n                    someOtherFunc(foo);\n            }\n            \\`\\`\\`"]))),
40         rationale: "Fall though in switch statements is often unintentional and a bug.",
41         optionsDescription: "Not configurable.",
42         options: null,
43         optionExamples: [true],
44         type: "functionality",
45         typescriptOnly: false,
46     };
47     return Rule;
48 }(Lint.Rules.AbstractRule));
49 exports.Rule = Rule;
50 var NoSwitchCaseFallThroughWalker = /** @class */ (function (_super) {
51     tslib_1.__extends(NoSwitchCaseFallThroughWalker, _super);
52     function NoSwitchCaseFallThroughWalker() {
53         return _super !== null && _super.apply(this, arguments) || this;
54     }
55     NoSwitchCaseFallThroughWalker.prototype.walk = function (sourceFile) {
56         var _this = this;
57         var cb = function (node) {
58             if (utils.isSwitchStatement(node)) {
59                 _this.visitSwitchStatement(node);
60             }
61             return ts.forEachChild(node, cb);
62         };
63         return ts.forEachChild(sourceFile, cb);
64     };
65     NoSwitchCaseFallThroughWalker.prototype.visitSwitchStatement = function (_a) {
66         var _this = this;
67         var clauses = _a.caseBlock.clauses;
68         clauses.forEach(function (clause, i) {
69             if (i !== clauses.length - 1 &&
70                 clause.statements.length !== 0 &&
71                 !utils.endsControlFlow(clause) &&
72                 !_this.isFallThroughAllowed(clause)) {
73                 var keyword = clauses[i + 1].getChildAt(0);
74                 _this.addFailureAtNode(keyword, Rule.FAILURE_STRING(keyword.kind));
75             }
76         });
77     };
78     NoSwitchCaseFallThroughWalker.prototype.isFallThroughAllowed = function (clause) {
79         var _this = this;
80         var comments = ts.getLeadingCommentRanges(this.sourceFile.text, clause.end);
81         return (comments !== undefined &&
82             comments.some(function (comment) {
83                 return /^\s*falls through\b/i.test(_this.sourceFile.text.slice(comment.pos + 2, comment.end));
84             }));
85     };
86     return NoSwitchCaseFallThroughWalker;
87 }(Lint.AbstractWalker));
88 exports.NoSwitchCaseFallThroughWalker = NoSwitchCaseFallThroughWalker;
89 var templateObject_1;