This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 889
/
noUnusedExpressionRule.ts
258 lines (238 loc) · 9.08 KB
/
noUnusedExpressionRule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/**
* @license
* Copyright 2014 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
isAssignmentKind,
isBinaryExpression,
isConditionalExpression,
isExpressionStatement,
isIdentifier,
isNumericLiteral,
isParenthesizedExpression,
isVoidExpression,
} from "tsutils";
import * as ts from "typescript";
import * as Lint from "../index";
const ALLOW_FAST_NULL_CHECKS = "allow-fast-null-checks";
const ALLOW_NEW = "allow-new";
const ALLOW_TAGGED_TEMPLATE = "allow-tagged-template";
interface Options {
allowFastNullChecks: boolean;
allowNew: boolean;
allowTaggedTemplate: boolean;
}
export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
ruleName: "no-unused-expression",
description: "Disallows unused expression statements.",
descriptionDetails: Lint.Utils.dedent`
Unused expressions are expression statements which are not assignments or function calls
(and thus usually no-ops).`,
rationale: Lint.Utils.dedent`
Detects potential errors where an assignment or function call was intended.`,
optionsDescription: Lint.Utils.dedent`
Three arguments may be optionally provided:
* \`${ALLOW_FAST_NULL_CHECKS}\` allows to use logical operators to perform fast null checks and perform
method or function calls for side effects (e.g. \`e && e.preventDefault()\`).
* \`${ALLOW_NEW}\` allows 'new' expressions for side effects (e.g. \`new ModifyGlobalState();\`.
* \`${ALLOW_TAGGED_TEMPLATE}\` allows tagged templates for side effects (e.g. \`this.add\\\`foo\\\`;\`.`,
options: {
type: "array",
items: {
type: "string",
enum: [ALLOW_FAST_NULL_CHECKS, ALLOW_NEW, ALLOW_TAGGED_TEMPLATE],
},
minLength: 0,
maxLength: 3,
},
optionExamples: [true, [true, ALLOW_FAST_NULL_CHECKS]],
type: "functionality",
typescriptOnly: false,
};
/* tslint:enable:object-literal-sort-keys */
public static FAILURE_STRING = "unused expression, expected an assignment or function call";
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithFunction(sourceFile, walk, {
allowFastNullChecks: this.ruleArguments.indexOf(ALLOW_FAST_NULL_CHECKS) !== -1,
allowNew: this.ruleArguments.indexOf(ALLOW_NEW) !== -1,
allowTaggedTemplate: this.ruleArguments.indexOf(ALLOW_TAGGED_TEMPLATE) !== -1,
});
}
}
function walk(ctx: Lint.WalkContext<Options>) {
let checking = false;
let allowFastNullChecks = true;
return ts.forEachChild(ctx.sourceFile, cb);
function cb(node: ts.Node): boolean | undefined {
if (checking) {
if (isParenthesizedExpression(node) || isVoidExpression(node)) {
return cb(node.expression);
} else if (isConditionalExpression(node)) {
noCheck(node.condition, cb);
return both(node.whenTrue, node.whenFalse);
} else if (isBinaryExpression(node)) {
switch (node.operatorToken.kind) {
case ts.SyntaxKind.CommaToken:
if (isIndirectEval(node)) {
return false;
}
return both(node.left, node.right);
case ts.SyntaxKind.AmpersandAmpersandToken:
case ts.SyntaxKind.BarBarToken:
if (allowFastNullChecks) {
noCheck(node.left, cb);
return cb(node.right);
}
}
}
noCheck(node, forEachChild);
return isUnusedExpression(node, ctx.options);
}
if (isExpressionStatement(node)) {
allowFastNullChecks = ctx.options.allowFastNullChecks;
if (!isDirective(node)) {
check(node.expression, node);
}
allowFastNullChecks = true;
return false;
} else if (isVoidExpression(node)) {
// allow `void 0` and `void(0)`
if (
!isLiteralZero(
isParenthesizedExpression(node.expression)
? node.expression.expression
: node.expression,
)
) {
check(node.expression);
}
return false;
} else if (
isBinaryExpression(node) &&
node.operatorToken.kind === ts.SyntaxKind.CommaToken &&
!isIndirectEval(node)
) {
check(node.left);
return cb(node.right);
}
return ts.forEachChild(node, cb);
}
function forEachChild(node: ts.Node) {
return ts.forEachChild(node, cb);
}
function check(node: ts.Node, failNode?: ts.Node): void {
checking = true;
if (cb(node)) {
ctx.addFailureAtNode(failNode === undefined ? node : failNode, Rule.FAILURE_STRING);
}
checking = false;
}
function noCheck(node: ts.Node, callback: (node: ts.Node) => void): void {
const old = allowFastNullChecks;
checking = false;
allowFastNullChecks = true;
callback(node);
allowFastNullChecks = old;
checking = true;
}
function both(one: ts.Node, two: ts.Node): boolean {
if (cb(one)) {
if (cb(two)) {
return true;
} else {
ctx.addFailureAtNode(one, Rule.FAILURE_STRING);
}
} else if (cb(two)) {
ctx.addFailureAtNode(two, Rule.FAILURE_STRING);
}
return false;
}
}
function isUnusedExpression(node: ts.Node, options: Options): boolean {
switch (node.kind) {
case ts.SyntaxKind.CallExpression:
case ts.SyntaxKind.YieldExpression:
case ts.SyntaxKind.DeleteExpression:
case ts.SyntaxKind.AwaitExpression:
case ts.SyntaxKind.PostfixUnaryExpression:
return false;
case ts.SyntaxKind.NewExpression:
return !options.allowNew;
case ts.SyntaxKind.TaggedTemplateExpression:
return !options.allowTaggedTemplate;
case ts.SyntaxKind.BinaryExpression:
return !isAssignmentKind((node as ts.BinaryExpression).operatorToken.kind);
case ts.SyntaxKind.PrefixUnaryExpression:
return (
(node as ts.PrefixUnaryExpression).operator !== ts.SyntaxKind.PlusPlusToken &&
(node as ts.PrefixUnaryExpression).operator !== ts.SyntaxKind.MinusMinusToken
);
default:
return true;
}
}
function isLiteralZero(node: ts.Expression) {
return isNumericLiteral(node) && node.text === "0";
}
function isIndirectEval(node: ts.BinaryExpression): boolean {
return (
isIdentifier(node.right) &&
node.right.text === "eval" &&
isLiteralZero(node.left) &&
node.parent.kind === ts.SyntaxKind.ParenthesizedExpression &&
node.parent.parent.kind === ts.SyntaxKind.CallExpression
);
}
function isDirective(node: ts.ExpressionStatement) {
if (node.expression.kind !== ts.SyntaxKind.StringLiteral || !canContainDirective(node.parent)) {
return false;
}
const parent = node.parent as ts.BlockLike;
// check if all previous statements in block are also directives
for (let i = parent.statements.indexOf(node) - 1; i >= 0; --i) {
const statement = parent.statements[i];
if (
!isExpressionStatement(statement) ||
statement.expression.kind !== ts.SyntaxKind.StringLiteral
) {
return false;
}
}
return true;
}
function canContainDirective(node: ts.Node): boolean {
switch (node.kind) {
case ts.SyntaxKind.SourceFile:
case ts.SyntaxKind.ModuleBlock:
return true;
case ts.SyntaxKind.Block:
switch (node.parent.kind) {
case ts.SyntaxKind.ArrowFunction:
case ts.SyntaxKind.FunctionExpression:
case ts.SyntaxKind.FunctionDeclaration:
case ts.SyntaxKind.MethodDeclaration:
case ts.SyntaxKind.Constructor:
case ts.SyntaxKind.GetAccessor:
case ts.SyntaxKind.SetAccessor:
return true;
default:
return false;
}
default:
return false;
}
}