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

Commit

Permalink
Fix bug where await expressions were marked as unused
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Killian committed Jan 9, 2016
1 parent 337cf67 commit 7f00366
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
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

0 comments on commit 7f00366

Please sign in to comment.