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
await-promise: check for-await-of #3297
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c270c28
--test: allow target other than default
ajafff cf94671
await-promise: check for-await-of
ajafff 2389746
test only works with typescript@>=2.3.0
ajafff 7e1816b
allow AsyncIterable
ajafff 96ea4d2
add test for Iterable
ajafff 8d7b259
rephrase error message
ajafff File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ | |
* limitations under the License. | ||
*/ | ||
|
||
import { isAwaitExpression, isUnionOrIntersectionType } from "tsutils"; | ||
import { isAwaitExpression, isForOfStatement, isTypeReference, isUnionOrIntersectionType } from "tsutils"; | ||
import * as ts from "typescript"; | ||
import * as Lint from "../index"; | ||
|
||
|
@@ -42,6 +42,7 @@ export class Rule extends Lint.Rules.TypedRule { | |
/* tslint:enable:object-literal-sort-keys */ | ||
|
||
public static FAILURE_STRING = "'await' of non-Promise."; | ||
public static FAILURE_FOR_AWAIT_OF = "'for-await-of' of non-AsyncIterable."; | ||
|
||
public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] { | ||
const promiseTypes = new Set(["Promise", ...this.ruleArguments as string[]]); | ||
|
@@ -54,27 +55,33 @@ function walk(ctx: Lint.WalkContext<Set<string>>, tc: ts.TypeChecker) { | |
return ts.forEachChild(ctx.sourceFile, cb); | ||
|
||
function cb(node: ts.Node): void { | ||
if (isAwaitExpression(node) && !couldBePromise(tc.getTypeAtLocation(node.expression))) { | ||
if (isAwaitExpression(node) && !containsType(tc.getTypeAtLocation(node.expression), isPromiseType)) { | ||
ctx.addFailureAtNode(node, Rule.FAILURE_STRING); | ||
} else if (isForOfStatement(node) && node.awaitModifier !== undefined && | ||
!containsType(tc.getTypeAtLocation(node.expression), isAsyncIterable)) { | ||
ctx.addFailureAtNode(node.expression, Rule.FAILURE_FOR_AWAIT_OF); | ||
} | ||
return ts.forEachChild(node, cb); | ||
} | ||
|
||
function couldBePromise(type: ts.Type): boolean { | ||
if (Lint.isTypeFlagSet(type, ts.TypeFlags.Any) || isPromiseType(type)) { | ||
return true; | ||
} | ||
|
||
if (isUnionOrIntersectionType(type)) { | ||
return type.types.some(couldBePromise); | ||
} | ||
|
||
const bases = type.getBaseTypes(); | ||
return bases !== undefined && bases.some(couldBePromise); | ||
function isPromiseType(name: string) { | ||
return promiseTypes.has(name); | ||
} | ||
} | ||
|
||
function isPromiseType(type: ts.Type): boolean { | ||
const { target } = type as ts.TypeReference; | ||
return target !== undefined && target.symbol !== undefined && promiseTypes.has(target.symbol.name); | ||
function containsType(type: ts.Type, predicate: (name: string) => boolean): boolean { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems like a utility we might want in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense. We also use a similar function in |
||
if (Lint.isTypeFlagSet(type, ts.TypeFlags.Any) || | ||
isTypeReference(type) && type.target.symbol !== undefined && predicate(type.target.symbol.name)) { | ||
return true; | ||
} | ||
if (isUnionOrIntersectionType(type)) { | ||
return type.types.some((t) => containsType(t, predicate)); | ||
} | ||
|
||
const bases = type.getBaseTypes(); | ||
return bases !== undefined && bases.some((t) => containsType(t, predicate)); | ||
} | ||
|
||
function isAsyncIterable(name: string) { | ||
return name === "AsyncIterable" || name === "AsyncIterableIterator"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
[typescript]: >= 2.3.0 | ||
async function correct(foo: AsyncIterableIterator<string>) { | ||
for await (const element of foo) {} | ||
} | ||
|
||
async function correct2() { | ||
for await (const element of asyncGenerator()) {} | ||
} | ||
|
||
async function correct(foo: AsyncIterable<string>) { | ||
for await (const element of foo) {} | ||
} | ||
|
||
async function correct3(foo: AsyncIterableIterator<string> | AsyncIterableIterator<number>) { | ||
for await (const element of foo) {} | ||
} | ||
|
||
async function incorrect(foo: <Array<Promise<string>>) { | ||
for await (const element of foo) {} | ||
~~~ [0] | ||
} | ||
|
||
async function incorrect2(foo: IterableIterator<Promise<string>>) { | ||
for await (const element of foo) {} | ||
~~~ [0] | ||
} | ||
|
||
async function incorrect3() { | ||
for await (const element of asyncGenerator) {} | ||
~~~~~~~~~~~~~~ [0] | ||
} | ||
|
||
async function incorrect4() { | ||
for await (const element of generator()) {} | ||
~~~~~~~~~~~ [0] | ||
} | ||
|
||
async function incorrect5(foo: Iterable<string>) { | ||
for await (const element of foo) {} | ||
~~~ [0] | ||
} | ||
|
||
async function* asyncGenerator() { | ||
yield 1; | ||
} | ||
|
||
function* generator() { | ||
yield 1; | ||
} | ||
|
||
[0]: 'for-await-of' of non-AsyncIterable. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "esnext" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rules": { | ||
"await-promise": true | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realized both of these error messages are a bit too terse -- how about