.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / curlyRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2013 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 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);
29     function Rule() {
30         return _super !== null && _super.apply(this, arguments) || this;
31     }
32     Rule.FAILURE_STRING_FACTORY = function (kind) {
33         return kind + " statements must be braced";
34     };
35     Rule.prototype.apply = function (sourceFile) {
36         if (this.ruleArguments.indexOf(OPTION_AS_NEEDED) !== -1) {
37             return this.applyWithFunction(sourceFile, walkAsNeeded);
38         }
39         return this.applyWithWalker(new CurlyWalker(sourceFile, this.ruleName, {
40             ignoreSameLine: this.ruleArguments.indexOf(OPTION_IGNORE_SAME_LINE) !== -1,
41         }));
42     };
43     /* tslint:disable:object-literal-sort-keys */
44     Rule.metadata = {
45         ruleName: "curly",
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),
49         options: {
50             type: "array",
51             items: {
52                 type: "string",
53                 enum: [OPTION_AS_NEEDED, OPTION_IGNORE_SAME_LINE],
54             },
55         },
56         optionExamples: [true, [true, OPTION_IGNORE_SAME_LINE], [true, OPTION_AS_NEEDED]],
57         type: "functionality",
58         typescriptOnly: false,
59         hasFix: true,
60         codeExamples: curly_examples_1.codeExamples,
61     };
62     /* tslint:enable:object-literal-sort-keys */
63     Rule.FAILURE_STRING_AS_NEEDED = "Block contains only one statement; remove the curly braces.";
64     return Rule;
65 }(Lint.Rules.AbstractRule));
66 exports.Rule = Rule;
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);
71         }
72         ts.forEachChild(node, cb);
73     });
74 }
75 function isBlockUnnecessary(node) {
76     var parent = node.parent;
77     if (node.statements.length !== 1) {
78         return false;
79     }
80     var statement = node.statements[0];
81     if (tsutils_1.isIterationStatement(parent)) {
82         return true;
83     }
84     /*
85     Watch out for this case:
86     if (so) {
87         if (also)
88             foo();
89     } else
90         bar();
91     */
92     return (tsutils_1.isIfStatement(parent) &&
93         !(tsutils_1.isIfStatement(statement) &&
94             statement.elseStatement === undefined &&
95             parent.thenStatement === node &&
96             parent.elseStatement !== undefined));
97 }
98 var CurlyWalker = /** @class */ (function (_super) {
99     tslib_1.__extends(CurlyWalker, _super);
100     function CurlyWalker() {
101         return _super !== null && _super.apply(this, arguments) || this;
102     }
103     CurlyWalker.prototype.walk = function (sourceFile) {
104         var _this = this;
105         var cb = function (node) {
106             if (tsutils_1.isIterationStatement(node)) {
107                 _this.checkStatement(node.statement, node, 0, node.end);
108             }
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);
114                 }
115             }
116             return ts.forEachChild(node, cb);
117         };
118         return ts.forEachChild(sourceFile, cb);
119     };
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));
127         }
128     };
129     /** Generate the necessary replacement to add braces to a statement that needs them. */
130     CurlyWalker.prototype.createMissingBraceFix = function (statement, node, sameLine) {
131         if (sameLine) {
132             return [
133                 Lint.Replacement.appendText(statement.pos, " {"),
134                 Lint.Replacement.appendText(statement.end, " }"),
135             ];
136         }
137         else {
138             var newLine = utils_1.newLineWithIndentation(node, this.sourceFile);
139             return [
140                 Lint.Replacement.appendText(statement.pos, " {"),
141                 Lint.Replacement.appendText(statement.end, newLine + "}"),
142             ];
143         }
144     };
145     return CurlyWalker;
146 }(Lint.AbstractWalker));
147 var templateObject_1, templateObject_2;