2 Object.defineProperty(exports, "__esModule", { value: true });
3 var tslib_1 = require("tslib");
7 * Copyright 2019 Palantir Technologies, Inc.
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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);
29 return _super !== null && _super.apply(this, arguments) || this;
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.";
35 Rule.prototype.apply = function (sourceFile) {
36 return this.applyWithFunction(sourceFile, walk, parseOptions(this.ruleArguments[0]));
38 /* tslint:disable:object-literal-sort-keys */
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)]],
46 _b[OPTION_ALLOW_ELSE_IF] = { type: "boolean" },
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",
53 typescriptOnly: false,
54 codeExamples: unnecessaryElse_examples_1.codeExamples,
57 }(Lint.Rules.AbstractRule));
59 function parseOptions(option) {
61 return tslib_1.__assign((_a = {}, _a[OPTION_ALLOW_ELSE_IF] = false, _a), option);
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));
77 ts.forEachChild(node, visitNode);
78 ifStatementStack.pop();
80 function recentStackParentMissingJumpStatement() {
81 if (ifStatementStack.length <= 1) {
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) {
89 if (jumpStatement === undefined) {
95 function visitNode(node) {
96 if (utils.isIfStatement(node)) {
97 visitIfStatement(node);
100 ts.forEachChild(node, visitNode);
103 ts.forEachChild(ctx.sourceFile, visitNode);
105 function getPositionOfElseKeyword(node, kind) {
106 return node.getChildren().filter(function (child) { return child.kind === kind; })[0];
108 function getJumpStatement(node) {
109 if (node === undefined) {
113 case ts.SyntaxKind.BreakStatement:
115 case ts.SyntaxKind.ContinueStatement:
117 case ts.SyntaxKind.ThrowStatement:
119 case ts.SyntaxKind.ReturnStatement:
125 function getLastStatement(clause) {
126 var block = clause.statements[0];
127 var statements = clause.statements.length === 1 && utils.isBlock(block)
130 return last(statements);
133 return arr[arr.length - 1];
135 var templateObject_1, templateObject_2, templateObject_3;