.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / noConditionalAssignmentRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2015 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 Rule = /** @class */ (function (_super) {
24     tslib_1.__extends(Rule, _super);
25     function Rule() {
26         return _super !== null && _super.apply(this, arguments) || this;
27     }
28     Rule.prototype.apply = function (sourceFile) {
29         return this.applyWithFunction(sourceFile, walk);
30     };
31     /* tslint:disable:object-literal-sort-keys */
32     Rule.metadata = {
33         ruleName: "no-conditional-assignment",
34         description: "Disallows any type of assignment in conditionals.",
35         descriptionDetails: "This applies to `do-while`, `for`, `if`, and `while` statements and conditional (ternary) expressions.",
36         rationale: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n            Assignments in conditionals are often typos:\n            for example `if (var1 = var2)` instead of `if (var1 == var2)`.\n            They also can be an indicator of overly clever code which decreases maintainability."], ["\n            Assignments in conditionals are often typos:\n            for example \\`if (var1 = var2)\\` instead of \\`if (var1 == var2)\\`.\n            They also can be an indicator of overly clever code which decreases maintainability."]))),
37         optionsDescription: "Not configurable.",
38         options: null,
39         optionExamples: [true],
40         type: "functionality",
41         typescriptOnly: false,
42     };
43     /* tslint:enable:object-literal-sort-keys */
44     Rule.FAILURE_STRING = "Assignments in conditional expressions are forbidden";
45     return Rule;
46 }(Lint.Rules.AbstractRule));
47 exports.Rule = Rule;
48 function walk(ctx) {
49     var checking = 0;
50     return ts.forEachChild(ctx.sourceFile, cb);
51     function cb(node) {
52         var kind = node.kind;
53         if (!tsutils_1.isNodeKind(kind)) {
54             return; // return early for tokens
55         }
56         switch (kind) {
57             case ts.SyntaxKind.ConditionalExpression:
58                 check(node.condition);
59                 cb(node.whenTrue);
60                 cb(node.whenFalse);
61                 return;
62             case ts.SyntaxKind.IfStatement:
63                 check(node.expression);
64                 cb(node.thenStatement);
65                 maybeCallback(cb, node.elseStatement);
66                 return;
67             case ts.SyntaxKind.DoStatement:
68             case ts.SyntaxKind.WhileStatement:
69                 check(node.expression);
70                 cb(node.statement);
71                 return;
72             case ts.SyntaxKind.ForStatement:
73                 maybeCallback(cb, node.initializer);
74                 maybeCallback(check, node.condition);
75                 maybeCallback(cb, node.incrementor);
76                 cb(node.statement);
77                 return;
78         }
79         if (checking !== 0) {
80             switch (kind) {
81                 case ts.SyntaxKind.BinaryExpression:
82                     if (tsutils_1.isAssignmentKind(node.operatorToken.kind)) {
83                         ctx.addFailureAtNode(node, Rule.FAILURE_STRING);
84                     }
85                     cb(node.left);
86                     cb(node.right);
87                     return;
88                 case ts.SyntaxKind.ParenthesizedExpression:
89                 case ts.SyntaxKind.NonNullExpression:
90                 case ts.SyntaxKind.AsExpression:
91                 case ts.SyntaxKind.TypeAssertionExpression:
92                     return cb(node.expression);
93                 case ts.SyntaxKind.PrefixUnaryExpression:
94                     return cb(node.operand);
95                 default:
96                     return noCheck(node);
97             }
98         }
99         return ts.forEachChild(node, cb);
100     }
101     function check(node) {
102         ++checking;
103         cb(node);
104         --checking;
105     }
106     function noCheck(node) {
107         var old = checking;
108         checking = 0;
109         ts.forEachChild(node, cb);
110         checking = old;
111     }
112 }
113 function maybeCallback(cb, node) {
114     if (node !== undefined) {
115         cb(node);
116     }
117 }
118 var templateObject_1;