Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Fix bug where await expressions were marked as unused #905

Merged
merged 1 commit into from
Jan 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/rules/noUnusedExpressionRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ class NoUnusedExpressionWalker extends Lint.RuleWalker {
this.expressionIsUnused = true;
super.visitExpressionStatement(node);
if (this.expressionIsUnused) {
// ignore valid unused expressions
if (node.expression.kind === ts.SyntaxKind.StringLiteral) {
const expressionText = node.expression.getText();
if (expressionText === "\"use strict\"" || expressionText === "'use strict'") {
return;
}
} else if (node.expression.kind === ts.SyntaxKind.DeleteExpression || node.expression.kind === ts.SyntaxKind.YieldExpression) {
return;
}
const { expression } = node;
const { kind } = expression;
const isValidStandaloneExpression = kind === ts.SyntaxKind.DeleteExpression
|| kind === ts.SyntaxKind.YieldExpression
|| kind === ts.SyntaxKind.AwaitExpression;
const isValidStringExpression = kind === ts.SyntaxKind.StringLiteral
&& (expression.getText() === '"use strict"' || expression.getText() === "'use strict'");

this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING));
if (!isValidStandaloneExpression && !isValidStringExpression) {
this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING));
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions test/files/rules/unused.expression.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,8 @@ function* g(): Iterable<number> {
yield i;
}
}

async function f(foo: Promise<void>): Promise<number> {
await foo;
return 0;
}
2 changes: 1 addition & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"check-else",
"check-whitespace"
],
"quotemark": [true, "double"],
"quotemark": [true, "double", "avoid-escape"],
"radix": true,
"semicolon": true,
"triple-equals": [true, "allow-null-check"],
Expand Down