4 * Copyright 2013 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 utils_1 = require("../utils");
24 var curly_examples_1 = require("./code-examples/curly.examples");
25 var OPTION_AS_NEEDED = "as-needed";
26 var OPTION_IGNORE_SAME_LINE = "ignore-same-line";
27 var Rule = /** @class */ (function (_super) {
28 tslib_1.__extends(Rule, _super);
30 return _super !== null && _super.apply(this, arguments) || this;
32 Rule.FAILURE_STRING_FACTORY = function (kind) {
33 return kind + " statements must be braced";
35 Rule.prototype.apply = function (sourceFile) {
36 if (this.ruleArguments.indexOf(OPTION_AS_NEEDED) !== -1) {
37 return this.applyWithFunction(sourceFile, walkAsNeeded);
39 return this.applyWithWalker(new CurlyWalker(sourceFile, this.ruleName, {
40 ignoreSameLine: this.ruleArguments.indexOf(OPTION_IGNORE_SAME_LINE) !== -1,
43 /* tslint:disable:object-literal-sort-keys */
46 description: "Enforces braces for `if`/`for`/`do`/`while` statements.",
47 rationale: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n ```ts\n if (foo === bar)\n foo++;\n bar++;\n ```\n\n In the code above, the author almost certainly meant for both `foo++` and `bar++`\n to be executed only if `foo === bar`. However, they forgot braces and `bar++` will be executed\n no matter what. This rule could prevent such a mistake."], ["\n \\`\\`\\`ts\n if (foo === bar)\n foo++;\n bar++;\n \\`\\`\\`\n\n In the code above, the author almost certainly meant for both \\`foo++\\` and \\`bar++\\`\n to be executed only if \\`foo === bar\\`. However, they forgot braces and \\`bar++\\` will be executed\n no matter what. This rule could prevent such a mistake."]))),
48 optionsDescription: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n One of the following options may be provided:\n\n * `\"", "\"` forbids any unnecessary curly braces.\n * `\"", "\"` skips checking braces for control-flow statements\n that are on one line and start on the same line as their control-flow keyword\n "], ["\n One of the following options may be provided:\n\n * \\`\"", "\"\\` forbids any unnecessary curly braces.\n * \\`\"", "\"\\` skips checking braces for control-flow statements\n that are on one line and start on the same line as their control-flow keyword\n "])), OPTION_AS_NEEDED, OPTION_IGNORE_SAME_LINE),
53 enum: [OPTION_AS_NEEDED, OPTION_IGNORE_SAME_LINE],
56 optionExamples: [true, [true, OPTION_IGNORE_SAME_LINE], [true, OPTION_AS_NEEDED]],
57 type: "functionality",
58 typescriptOnly: false,
60 codeExamples: curly_examples_1.codeExamples,
62 /* tslint:enable:object-literal-sort-keys */
63 Rule.FAILURE_STRING_AS_NEEDED = "Block contains only one statement; remove the curly braces.";
65 }(Lint.Rules.AbstractRule));
67 function walkAsNeeded(ctx) {
68 ts.forEachChild(ctx.sourceFile, function cb(node) {
69 if (tsutils_1.isBlock(node) && isBlockUnnecessary(node)) {
70 ctx.addFailureAt(node.statements.pos - 1, 1, Rule.FAILURE_STRING_AS_NEEDED);
72 ts.forEachChild(node, cb);
75 function isBlockUnnecessary(node) {
76 var parent = node.parent;
77 if (node.statements.length !== 1) {
80 var statement = node.statements[0];
81 if (tsutils_1.isIterationStatement(parent)) {
85 Watch out for this case:
92 return (tsutils_1.isIfStatement(parent) &&
93 !(tsutils_1.isIfStatement(statement) &&
94 statement.elseStatement === undefined &&
95 parent.thenStatement === node &&
96 parent.elseStatement !== undefined));
98 var CurlyWalker = /** @class */ (function (_super) {
99 tslib_1.__extends(CurlyWalker, _super);
100 function CurlyWalker() {
101 return _super !== null && _super.apply(this, arguments) || this;
103 CurlyWalker.prototype.walk = function (sourceFile) {
105 var cb = function (node) {
106 if (tsutils_1.isIterationStatement(node)) {
107 _this.checkStatement(node.statement, node, 0, node.end);
109 else if (tsutils_1.isIfStatement(node)) {
110 _this.checkStatement(node.thenStatement, node, 0);
111 if (node.elseStatement !== undefined &&
112 node.elseStatement.kind !== ts.SyntaxKind.IfStatement) {
113 _this.checkStatement(node.elseStatement, node, 5);
116 return ts.forEachChild(node, cb);
118 return ts.forEachChild(sourceFile, cb);
120 CurlyWalker.prototype.checkStatement = function (statement, node, childIndex, end) {
121 if (end === void 0) { end = statement.end; }
122 var sameLine = tsutils_1.isSameLine(this.sourceFile, statement.pos, statement.end);
123 if (statement.kind !== ts.SyntaxKind.Block && !(this.options.ignoreSameLine && sameLine)) {
124 var token = node.getChildAt(childIndex, this.sourceFile);
125 var tokenText = ts.tokenToString(token.kind);
126 this.addFailure(token.end - tokenText.length, end, Rule.FAILURE_STRING_FACTORY(tokenText), this.createMissingBraceFix(statement, node, sameLine));
129 /** Generate the necessary replacement to add braces to a statement that needs them. */
130 CurlyWalker.prototype.createMissingBraceFix = function (statement, node, sameLine) {
133 Lint.Replacement.appendText(statement.pos, " {"),
134 Lint.Replacement.appendText(statement.end, " }"),
138 var newLine = utils_1.newLineWithIndentation(node, this.sourceFile);
140 Lint.Replacement.appendText(statement.pos, " {"),
141 Lint.Replacement.appendText(statement.end, newLine + "}"),
146 }(Lint.AbstractWalker));
147 var templateObject_1, templateObject_2;