.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / unnecessaryElseRule.js
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 var tslib_1 = require("tslib");
4 var _a, _b;
5 /**
6  * @license
7  * Copyright 2019 Palantir Technologies, Inc.
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *     http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 var utils = require("tsutils");
22 var ts = require("typescript");
23 var Lint = require("../index");
24 var unnecessaryElse_examples_1 = require("./code-examples/unnecessaryElse.examples");
25 var OPTION_ALLOW_ELSE_IF = "allow-else-if";
26 var Rule = /** @class */ (function (_super) {
27     tslib_1.__extends(Rule, _super);
28     function Rule() {
29         return _super !== null && _super.apply(this, arguments) || this;
30     }
31     /* tslint:disable:object-literal-sort-keys */
32     Rule.FAILURE_STRING = function (name) {
33         return "The preceding `if` block ends with a `" + name + "` statement. This `else` is unnecessary.";
34     };
35     Rule.prototype.apply = function (sourceFile) {
36         return this.applyWithFunction(sourceFile, walk, parseOptions(this.ruleArguments[0]));
37     };
38     /* tslint:disable:object-literal-sort-keys */
39     Rule.metadata = {
40         description: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n        Disallows `else` blocks following `if` blocks ending with a `break`, `continue`, `return`, or `throw` statement."], ["\n        Disallows \\`else\\` blocks following \\`if\\` blocks ending with a \\`break\\`, \\`continue\\`, \\`return\\`, or \\`throw\\` statement."]))),
41         descriptionDetails: "",
42         optionExamples: [true, [true, (_a = {}, _a[OPTION_ALLOW_ELSE_IF] = true, _a)]],
43         options: {
44             type: "object",
45             properties: (_b = {},
46                 _b[OPTION_ALLOW_ELSE_IF] = { type: "boolean" },
47                 _b),
48         },
49         optionsDescription: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n            You can optionally specify the option `\"", "\"` to allow \"else if\" statements.\n        "], ["\n            You can optionally specify the option \\`\"", "\"\\` to allow \"else if\" statements.\n        "])), OPTION_ALLOW_ELSE_IF),
50         rationale: Lint.Utils.dedent(templateObject_3 || (templateObject_3 = tslib_1.__makeTemplateObject(["\n        When an `if` block is guaranteed to exit control flow when entered,\n        it is unnecessary to add an `else` statement.\n        The contents that would be in the `else` block can be placed after the end of the `if` block."], ["\n        When an \\`if\\` block is guaranteed to exit control flow when entered,\n        it is unnecessary to add an \\`else\\` statement.\n        The contents that would be in the \\`else\\` block can be placed after the end of the \\`if\\` block."]))),
51         ruleName: "unnecessary-else",
52         type: "style",
53         typescriptOnly: false,
54         codeExamples: unnecessaryElse_examples_1.codeExamples,
55     };
56     return Rule;
57 }(Lint.Rules.AbstractRule));
58 exports.Rule = Rule;
59 function parseOptions(option) {
60     var _a;
61     return tslib_1.__assign((_a = {}, _a[OPTION_ALLOW_ELSE_IF] = false, _a), option);
62 }
63 function walk(ctx) {
64     var ifStatementStack = [];
65     function visitIfStatement(node) {
66         var jumpStatement = utils.isBlock(node.thenStatement)
67             ? getJumpStatement(getLastStatement(node.thenStatement))
68             : getJumpStatement(node.thenStatement);
69         ifStatementStack.push({ node: node, jumpStatement: jumpStatement });
70         if (jumpStatement !== undefined &&
71             node.elseStatement !== undefined &&
72             !recentStackParentMissingJumpStatement() &&
73             (!utils.isIfStatement(node.elseStatement) || !ctx.options[OPTION_ALLOW_ELSE_IF])) {
74             var elseKeyword = getPositionOfElseKeyword(node, ts.SyntaxKind.ElseKeyword);
75             ctx.addFailureAtNode(elseKeyword, Rule.FAILURE_STRING(jumpStatement));
76         }
77         ts.forEachChild(node, visitNode);
78         ifStatementStack.pop();
79     }
80     function recentStackParentMissingJumpStatement() {
81         if (ifStatementStack.length <= 1) {
82             return false;
83         }
84         for (var i = ifStatementStack.length - 2; i >= 0; i -= 1) {
85             var _a = ifStatementStack[i], jumpStatement = _a.jumpStatement, node = _a.node;
86             if (node.elseStatement !== ifStatementStack[i + 1].node) {
87                 return false;
88             }
89             if (jumpStatement === undefined) {
90                 return true;
91             }
92         }
93         return false;
94     }
95     function visitNode(node) {
96         if (utils.isIfStatement(node)) {
97             visitIfStatement(node);
98         }
99         else {
100             ts.forEachChild(node, visitNode);
101         }
102     }
103     ts.forEachChild(ctx.sourceFile, visitNode);
104 }
105 function getPositionOfElseKeyword(node, kind) {
106     return node.getChildren().filter(function (child) { return child.kind === kind; })[0];
107 }
108 function getJumpStatement(node) {
109     if (node === undefined) {
110         return undefined;
111     }
112     switch (node.kind) {
113         case ts.SyntaxKind.BreakStatement:
114             return "break";
115         case ts.SyntaxKind.ContinueStatement:
116             return "continue";
117         case ts.SyntaxKind.ThrowStatement:
118             return "throw";
119         case ts.SyntaxKind.ReturnStatement:
120             return "return";
121         default:
122             return undefined;
123     }
124 }
125 function getLastStatement(clause) {
126     var block = clause.statements[0];
127     var statements = clause.statements.length === 1 && utils.isBlock(block)
128         ? block.statements
129         : clause.statements;
130     return last(statements);
131 }
132 function last(arr) {
133     return arr[arr.length - 1];
134 }
135 var templateObject_1, templateObject_2, templateObject_3;