4 * Copyright 2017 Palantir Technologies, Inc.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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);
27 return _super !== null && _super.apply(this, arguments) || this;
29 Rule.prototype.apply = function (sourceFile) {
30 return this.applyWithFunction(sourceFile, walk, {
31 always: this.ruleArguments.indexOf(OPTION_ALWAYS) !== -1,
34 /* tslint:disable:object-literal-sort-keys */
36 ruleName: "switch-final-break",
37 description: "Checks whether the final clause of a switch statement ends in `break;`.",
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."]))),
42 enum: [OPTION_ALWAYS],
44 optionExamples: [true, [true, OPTION_ALWAYS]],
46 typescriptOnly: false,
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;'.";
52 }(Lint.Rules.AbstractRule));
55 var sourceFile = ctx.sourceFile, always = ctx.options.always;
56 ts.forEachChild(sourceFile, function cb(node) {
57 if (tsutils_1.isSwitchStatement(node)) {
60 ts.forEachChild(node, cb);
62 function check(node) {
63 var clause = last(node.caseBlock.clauses);
64 if (clause === undefined) {
68 if (!tsutils_1.endsControlFlow(clause)) {
69 ctx.addFailureAtNode(clause.getChildAt(0), Rule.FAILURE_STRING_ALWAYS, createAddFix(clause));
73 var lastStatement = getLastStatement(clause);
74 if (lastStatement === undefined || !tsutils_1.isBreakStatement(lastStatement)) {
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
84 ctx.addFailureAtNode(lastStatement, Rule.FAILURE_STRING_NEVER, createRemoveFix(lastStatement));
86 function createAddFix(clause) {
87 var lastStatement = getLastStatement(clause);
88 if (lastStatement === undefined) {
89 return Lint.Replacement.appendText(clause.end, " break;");
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;");
95 function createRemoveFix(lastStatement) {
96 return Lint.Replacement.replaceFromTo(lastStatement.getFullStart(), lastStatement.end, "");
99 function getLastStatement(clause) {
100 if (clause.statements.length === 0) {
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);
108 return arr[arr.length - 1];
110 var templateObject_1;