.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / switchFinalBreakRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2017 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 tsutils_1 = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 var OPTION_ALWAYS = "always";
24 var Rule = /** @class */ (function (_super) {
25     tslib_1.__extends(Rule, _super);
26     function Rule() {
27         return _super !== null && _super.apply(this, arguments) || this;
28     }
29     Rule.prototype.apply = function (sourceFile) {
30         return this.applyWithFunction(sourceFile, walk, {
31             always: this.ruleArguments.indexOf(OPTION_ALWAYS) !== -1,
32         });
33     };
34     /* tslint:disable:object-literal-sort-keys */
35     Rule.metadata = {
36         ruleName: "switch-final-break",
37         description: "Checks whether the final clause of a switch statement ends in `break;`.",
38         hasFix: true,
39         optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n            If no options are passed, a final 'break;' is forbidden.\n            If the \"always\" option is passed this will require a 'break;' to always be present\n            unless control flow is escaped in some other way."], ["\n            If no options are passed, a final 'break;' is forbidden.\n            If the \"always\" option is passed this will require a 'break;' to always be present\n            unless control flow is escaped in some other way."]))),
40         options: {
41             type: "string",
42             enum: [OPTION_ALWAYS],
43         },
44         optionExamples: [true, [true, OPTION_ALWAYS]],
45         type: "style",
46         typescriptOnly: false,
47     };
48     /* tslint:enable:object-literal-sort-keys */
49     Rule.FAILURE_STRING_ALWAYS = "Final clause in 'switch' statement should end with 'break;'.";
50     Rule.FAILURE_STRING_NEVER = "Final clause in 'switch' statement should not end with 'break;'.";
51     return Rule;
52 }(Lint.Rules.AbstractRule));
53 exports.Rule = Rule;
54 function walk(ctx) {
55     var sourceFile = ctx.sourceFile, always = ctx.options.always;
56     ts.forEachChild(sourceFile, function cb(node) {
57         if (tsutils_1.isSwitchStatement(node)) {
58             check(node);
59         }
60         ts.forEachChild(node, cb);
61     });
62     function check(node) {
63         var clause = last(node.caseBlock.clauses);
64         if (clause === undefined) {
65             return;
66         }
67         if (always) {
68             if (!tsutils_1.endsControlFlow(clause)) {
69                 ctx.addFailureAtNode(clause.getChildAt(0), Rule.FAILURE_STRING_ALWAYS, createAddFix(clause));
70             }
71             return;
72         }
73         var lastStatement = getLastStatement(clause);
74         if (lastStatement === undefined || !tsutils_1.isBreakStatement(lastStatement)) {
75             return;
76         }
77         if (lastStatement.label !== undefined) {
78             var parent = node.parent;
79             if (!tsutils_1.isLabeledStatement(parent) || parent.label === lastStatement.label) {
80                 // break jumps somewhere else, don't complain
81                 return;
82             }
83         }
84         ctx.addFailureAtNode(lastStatement, Rule.FAILURE_STRING_NEVER, createRemoveFix(lastStatement));
85     }
86     function createAddFix(clause) {
87         var lastStatement = getLastStatement(clause);
88         if (lastStatement === undefined) {
89             return Lint.Replacement.appendText(clause.end, " break;");
90         }
91         var fullText = lastStatement.getFullText(ctx.sourceFile);
92         var indentation = fullText.slice(0, fullText.search(/\S+/));
93         return Lint.Replacement.appendText(lastStatement.end, indentation + "break;");
94     }
95     function createRemoveFix(lastStatement) {
96         return Lint.Replacement.replaceFromTo(lastStatement.getFullStart(), lastStatement.end, "");
97     }
98 }
99 function getLastStatement(clause) {
100     if (clause.statements.length === 0) {
101         return undefined;
102     }
103     var block = clause.statements[0];
104     var statements = clause.statements.length === 1 && tsutils_1.isBlock(block) ? block.statements : clause.statements;
105     return last(statements);
106 }
107 function last(arr) {
108     return arr[arr.length - 1];
109 }
110 var templateObject_1;