4 * Copyright 2014 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 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);
29 return _super !== null && _super.apply(this, arguments) || this;
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,
38 /* tslint:disable:object-literal-sort-keys */
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),
49 enum: [ALLOW_FAST_NULL_CHECKS, ALLOW_NEW, ALLOW_TAGGED_TEMPLATE],
54 optionExamples: [true, [true, ALLOW_FAST_NULL_CHECKS]],
55 type: "functionality",
56 typescriptOnly: false,
58 /* tslint:enable:object-literal-sort-keys */
59 Rule.FAILURE_STRING = "unused expression, expected an assignment or function call";
61 }(Lint.Rules.AbstractRule));
65 var allowFastNullChecks = true;
66 return ts.forEachChild(ctx.sourceFile, cb);
69 if (tsutils_1.isParenthesizedExpression(node) || tsutils_1.isVoidExpression(node)) {
70 return cb(node.expression);
72 else if (tsutils_1.isConditionalExpression(node)) {
73 noCheck(node.condition, cb);
74 return both(node.whenTrue, node.whenFalse);
76 else if (tsutils_1.isBinaryExpression(node)) {
77 switch (node.operatorToken.kind) {
78 case ts.SyntaxKind.CommaToken:
79 if (isIndirectEval(node)) {
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);
91 noCheck(node, forEachChild);
92 return isUnusedExpression(node, ctx.options);
94 if (tsutils_1.isExpressionStatement(node)) {
95 allowFastNullChecks = ctx.options.allowFastNullChecks;
96 if (!isDirective(node)) {
97 check(node.expression, node);
99 allowFastNullChecks = true;
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);
111 else if (tsutils_1.isBinaryExpression(node) &&
112 node.operatorToken.kind === ts.SyntaxKind.CommaToken &&
113 !isIndirectEval(node)) {
115 return cb(node.right);
117 return ts.forEachChild(node, cb);
119 function forEachChild(node) {
120 return ts.forEachChild(node, cb);
122 function check(node, failNode) {
125 ctx.addFailureAtNode(failNode === undefined ? node : failNode, Rule.FAILURE_STRING);
129 function noCheck(node, callback) {
130 var old = allowFastNullChecks;
132 allowFastNullChecks = true;
134 allowFastNullChecks = old;
137 function both(one, two) {
143 ctx.addFailureAtNode(one, Rule.FAILURE_STRING);
147 ctx.addFailureAtNode(two, Rule.FAILURE_STRING);
152 function isUnusedExpression(node, options) {
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:
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);
173 function isLiteralZero(node) {
174 return tsutils_1.isNumericLiteral(node) && node.text === "0";
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);
183 function isDirective(node) {
184 if (node.expression.kind !== ts.SyntaxKind.StringLiteral || !canContainDirective(node.parent)) {
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) {
198 function canContainDirective(node) {
200 case ts.SyntaxKind.SourceFile:
201 case ts.SyntaxKind.ModuleBlock:
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:
220 var templateObject_1, templateObject_2, templateObject_3;