.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / tslint / lib / rules / noUnusedExpressionRule.js
1 "use strict";
2 /**
3  * @license
4  * Copyright 2014 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 ALLOW_FAST_NULL_CHECKS = "allow-fast-null-checks";
24 var ALLOW_NEW = "allow-new";
25 var ALLOW_TAGGED_TEMPLATE = "allow-tagged-template";
26 var Rule = /** @class */ (function (_super) {
27     tslib_1.__extends(Rule, _super);
28     function Rule() {
29         return _super !== null && _super.apply(this, arguments) || this;
30     }
31     Rule.prototype.apply = function (sourceFile) {
32         return this.applyWithFunction(sourceFile, walk, {
33             allowFastNullChecks: this.ruleArguments.indexOf(ALLOW_FAST_NULL_CHECKS) !== -1,
34             allowNew: this.ruleArguments.indexOf(ALLOW_NEW) !== -1,
35             allowTaggedTemplate: this.ruleArguments.indexOf(ALLOW_TAGGED_TEMPLATE) !== -1,
36         });
37     };
38     /* tslint:disable:object-literal-sort-keys */
39     Rule.metadata = {
40         ruleName: "no-unused-expression",
41         description: "Disallows unused expression statements.",
42         descriptionDetails: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n            Unused expressions are expression statements which are not assignments or function calls\n            (and thus usually no-ops)."], ["\n            Unused expressions are expression statements which are not assignments or function calls\n            (and thus usually no-ops)."]))),
43         rationale: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n            Detects potential errors where an assignment or function call was intended."], ["\n            Detects potential errors where an assignment or function call was intended."]))),
44         optionsDescription: Lint.Utils.dedent(templateObject_3 || (templateObject_3 = tslib_1.__makeTemplateObject(["\n            Three arguments may be optionally provided:\n\n            * `", "` allows to use logical operators to perform fast null checks and perform\n            method or function calls for side effects (e.g. `e && e.preventDefault()`).\n            * `", "` allows 'new' expressions for side effects (e.g. `new ModifyGlobalState();`.\n            * `", "` allows tagged templates for side effects (e.g. `this.add\\`foo\\`;`."], ["\n            Three arguments may be optionally provided:\n\n            * \\`", "\\` allows to use logical operators to perform fast null checks and perform\n            method or function calls for side effects (e.g. \\`e && e.preventDefault()\\`).\n            * \\`", "\\` allows 'new' expressions for side effects (e.g. \\`new ModifyGlobalState();\\`.\n            * \\`", "\\` allows tagged templates for side effects (e.g. \\`this.add\\\\\\`foo\\\\\\`;\\`."])), ALLOW_FAST_NULL_CHECKS, ALLOW_NEW, ALLOW_TAGGED_TEMPLATE),
45         options: {
46             type: "array",
47             items: {
48                 type: "string",
49                 enum: [ALLOW_FAST_NULL_CHECKS, ALLOW_NEW, ALLOW_TAGGED_TEMPLATE],
50             },
51             minLength: 0,
52             maxLength: 3,
53         },
54         optionExamples: [true, [true, ALLOW_FAST_NULL_CHECKS]],
55         type: "functionality",
56         typescriptOnly: false,
57     };
58     /* tslint:enable:object-literal-sort-keys */
59     Rule.FAILURE_STRING = "unused expression, expected an assignment or function call";
60     return Rule;
61 }(Lint.Rules.AbstractRule));
62 exports.Rule = Rule;
63 function walk(ctx) {
64     var checking = false;
65     var allowFastNullChecks = true;
66     return ts.forEachChild(ctx.sourceFile, cb);
67     function cb(node) {
68         if (checking) {
69             if (tsutils_1.isParenthesizedExpression(node) || tsutils_1.isVoidExpression(node)) {
70                 return cb(node.expression);
71             }
72             else if (tsutils_1.isConditionalExpression(node)) {
73                 noCheck(node.condition, cb);
74                 return both(node.whenTrue, node.whenFalse);
75             }
76             else if (tsutils_1.isBinaryExpression(node)) {
77                 switch (node.operatorToken.kind) {
78                     case ts.SyntaxKind.CommaToken:
79                         if (isIndirectEval(node)) {
80                             return false;
81                         }
82                         return both(node.left, node.right);
83                     case ts.SyntaxKind.AmpersandAmpersandToken:
84                     case ts.SyntaxKind.BarBarToken:
85                         if (allowFastNullChecks) {
86                             noCheck(node.left, cb);
87                             return cb(node.right);
88                         }
89                 }
90             }
91             noCheck(node, forEachChild);
92             return isUnusedExpression(node, ctx.options);
93         }
94         if (tsutils_1.isExpressionStatement(node)) {
95             allowFastNullChecks = ctx.options.allowFastNullChecks;
96             if (!isDirective(node)) {
97                 check(node.expression, node);
98             }
99             allowFastNullChecks = true;
100             return false;
101         }
102         else if (tsutils_1.isVoidExpression(node)) {
103             // allow `void 0` and `void(0)`
104             if (!isLiteralZero(tsutils_1.isParenthesizedExpression(node.expression)
105                 ? node.expression.expression
106                 : node.expression)) {
107                 check(node.expression);
108             }
109             return false;
110         }
111         else if (tsutils_1.isBinaryExpression(node) &&
112             node.operatorToken.kind === ts.SyntaxKind.CommaToken &&
113             !isIndirectEval(node)) {
114             check(node.left);
115             return cb(node.right);
116         }
117         return ts.forEachChild(node, cb);
118     }
119     function forEachChild(node) {
120         return ts.forEachChild(node, cb);
121     }
122     function check(node, failNode) {
123         checking = true;
124         if (cb(node)) {
125             ctx.addFailureAtNode(failNode === undefined ? node : failNode, Rule.FAILURE_STRING);
126         }
127         checking = false;
128     }
129     function noCheck(node, callback) {
130         var old = allowFastNullChecks;
131         checking = false;
132         allowFastNullChecks = true;
133         callback(node);
134         allowFastNullChecks = old;
135         checking = true;
136     }
137     function both(one, two) {
138         if (cb(one)) {
139             if (cb(two)) {
140                 return true;
141             }
142             else {
143                 ctx.addFailureAtNode(one, Rule.FAILURE_STRING);
144             }
145         }
146         else if (cb(two)) {
147             ctx.addFailureAtNode(two, Rule.FAILURE_STRING);
148         }
149         return false;
150     }
151 }
152 function isUnusedExpression(node, options) {
153     switch (node.kind) {
154         case ts.SyntaxKind.CallExpression:
155         case ts.SyntaxKind.YieldExpression:
156         case ts.SyntaxKind.DeleteExpression:
157         case ts.SyntaxKind.AwaitExpression:
158         case ts.SyntaxKind.PostfixUnaryExpression:
159             return false;
160         case ts.SyntaxKind.NewExpression:
161             return !options.allowNew;
162         case ts.SyntaxKind.TaggedTemplateExpression:
163             return !options.allowTaggedTemplate;
164         case ts.SyntaxKind.BinaryExpression:
165             return !tsutils_1.isAssignmentKind(node.operatorToken.kind);
166         case ts.SyntaxKind.PrefixUnaryExpression:
167             return (node.operator !== ts.SyntaxKind.PlusPlusToken &&
168                 node.operator !== ts.SyntaxKind.MinusMinusToken);
169         default:
170             return true;
171     }
172 }
173 function isLiteralZero(node) {
174     return tsutils_1.isNumericLiteral(node) && node.text === "0";
175 }
176 function isIndirectEval(node) {
177     return (tsutils_1.isIdentifier(node.right) &&
178         node.right.text === "eval" &&
179         isLiteralZero(node.left) &&
180         node.parent.kind === ts.SyntaxKind.ParenthesizedExpression &&
181         node.parent.parent.kind === ts.SyntaxKind.CallExpression);
182 }
183 function isDirective(node) {
184     if (node.expression.kind !== ts.SyntaxKind.StringLiteral || !canContainDirective(node.parent)) {
185         return false;
186     }
187     var parent = node.parent;
188     // check if all previous statements in block are also directives
189     for (var i = parent.statements.indexOf(node) - 1; i >= 0; --i) {
190         var statement = parent.statements[i];
191         if (!tsutils_1.isExpressionStatement(statement) ||
192             statement.expression.kind !== ts.SyntaxKind.StringLiteral) {
193             return false;
194         }
195     }
196     return true;
197 }
198 function canContainDirective(node) {
199     switch (node.kind) {
200         case ts.SyntaxKind.SourceFile:
201         case ts.SyntaxKind.ModuleBlock:
202             return true;
203         case ts.SyntaxKind.Block:
204             switch (node.parent.kind) {
205                 case ts.SyntaxKind.ArrowFunction:
206                 case ts.SyntaxKind.FunctionExpression:
207                 case ts.SyntaxKind.FunctionDeclaration:
208                 case ts.SyntaxKind.MethodDeclaration:
209                 case ts.SyntaxKind.Constructor:
210                 case ts.SyntaxKind.GetAccessor:
211                 case ts.SyntaxKind.SetAccessor:
212                     return true;
213                 default:
214                     return false;
215             }
216         default:
217             return false;
218     }
219 }
220 var templateObject_1, templateObject_2, templateObject_3;