This repository was archived by the owner on Mar 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 878
[New Rule] add noAsyncWithoutAwait rule #3945
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
96a0c36
add noAsyncWithoutAwait rule
eranshabi 5b4e3c3
fix lint
eranshabi 7f6d5c7
code review updates
eranshabi 5abe892
Merge remote-tracking branch 'upstream/master'
eranshabi f4680ef
allow return as well as await
eranshabi d28b2d3
remove unneeded lint ignore
eranshabi 337cd30
Merge remote-tracking branch 'upstream/master'
eranshabi 7e11840
Merge remote-tracking branch 'upstream/master'
eranshabi 4f51780
Merge remote-tracking branch 'upstream/master'
eranshabi bfa4c91
improve rationale & fix performance issue
eranshabi 37aa7b5
fixes according to review
eranshabi 0d58f68
finish fixing according to review
eranshabi 96f39ed
Initial feedback cleanups
7a782a5
Merge remote-tracking branch 'upstream/master'
65e10f5
Refactored to walk function
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2018 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 * as Lint from "../../index"; | ||
|
|
||
| export const codeExamples = [ | ||
| { | ||
| config: Lint.Utils.dedent` | ||
| "rules": { "no-async-without-await": true } | ||
| `, | ||
| description: "Do not use the async keyword if it is not needed", | ||
| fail: Lint.Utils.dedent` | ||
| async function f() { | ||
| fetch(); | ||
| } | ||
|
|
||
| async function f() { | ||
| async function g() { | ||
| await h(); | ||
| } | ||
| } | ||
| `, | ||
| pass: Lint.Utils.dedent` | ||
| async function f() { | ||
| await fetch(); | ||
| } | ||
|
|
||
| const f = async () => { | ||
| await fetch(); | ||
| }; | ||
|
|
||
| const f = async () => { | ||
| return 'value'; | ||
| }; | ||
| `, | ||
| }, | ||
| ]; | ||
This file contains hidden or 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,140 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2018 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 * as tsutils from "tsutils"; | ||
| import * as ts from "typescript"; | ||
|
|
||
| import * as Lint from "../index"; | ||
|
|
||
| import { codeExamples } from "./code-examples/noAsyncWithoutAwait.examples"; | ||
|
|
||
| type FunctionNodeType = | ||
| | ts.ArrowFunction | ||
| | ts.FunctionDeclaration | ||
| | ts.MethodDeclaration | ||
| | ts.FunctionExpression; | ||
|
|
||
| export class Rule extends Lint.Rules.AbstractRule { | ||
| public static FAILURE_STRING = | ||
| "Functions marked async must contain an await or return statement."; | ||
|
|
||
| public static metadata: Lint.IRuleMetadata = { | ||
| codeExamples, | ||
| description: Rule.FAILURE_STRING, | ||
| hasFix: false, | ||
| optionExamples: [true], | ||
| options: null, | ||
| optionsDescription: "Not configurable.", | ||
| /* tslint:disable:max-line-length */ | ||
| rationale: Lint.Utils.dedent` | ||
| Marking a function as \`async\` without using \`await\` or returning a value inside it can lead to an unintended promise return and a larger transpiled output. | ||
| Often the function can be synchronous and the \`async\` keyword is there by mistake. | ||
| Return statements are allowed as sometimes it is desirable to wrap the returned value in a Promise.`, | ||
| /* tslint:enable:max-line-length */ | ||
| ruleName: "no-async-without-await", | ||
| type: "functionality", | ||
| typescriptOnly: false, | ||
| }; | ||
|
|
||
| public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
| return this.applyWithFunction(sourceFile, walk); | ||
| } | ||
| } | ||
|
|
||
| function walk(context: Lint.WalkContext) { | ||
| const reportFailureIfAsyncFunction = (node: FunctionNodeType) => { | ||
| const asyncModifier = getAsyncModifier(node); | ||
| if (asyncModifier !== undefined) { | ||
| context.addFailureAt( | ||
| asyncModifier.getStart(), | ||
| asyncModifier.getEnd() - asyncModifier.getStart(), | ||
| Rule.FAILURE_STRING, | ||
| ); | ||
| } | ||
| }; | ||
|
|
||
| const addFailureIfAsyncFunctionHasNoAwait = (node: FunctionNodeType) => { | ||
| if (node.body === undefined) { | ||
| reportFailureIfAsyncFunction(node); | ||
| return; | ||
| } | ||
|
|
||
| if ( | ||
| !isShortArrowReturn(node) && | ||
| !functionBlockHasAwait(node.body) && | ||
| !functionBlockHasReturn(node.body) | ||
| ) { | ||
| reportFailureIfAsyncFunction(node); | ||
| } | ||
| }; | ||
|
|
||
| return ts.forEachChild(context.sourceFile, function visitNode(node): void { | ||
| if ( | ||
| tsutils.isArrowFunction(node) || | ||
| tsutils.isFunctionDeclaration(node) || | ||
| tsutils.isFunctionExpression(node) || | ||
| tsutils.isMethodDeclaration(node) | ||
| ) { | ||
| addFailureIfAsyncFunctionHasNoAwait(node); | ||
| } | ||
|
|
||
| return ts.forEachChild(node, visitNode); | ||
| }); | ||
| } | ||
|
|
||
| const getAsyncModifier = (node: ts.Node) => { | ||
| if (node.modifiers !== undefined) { | ||
| return node.modifiers.find(modifier => modifier.kind === ts.SyntaxKind.AsyncKeyword); | ||
| } | ||
|
|
||
| return undefined; | ||
| }; | ||
|
|
||
| const isReturn = (node: ts.Node): boolean => node.kind === ts.SyntaxKind.ReturnKeyword; | ||
|
|
||
| const functionBlockHasAwait = (node: ts.Node): boolean => { | ||
| if (tsutils.isAwaitExpression(node)) { | ||
| return true; | ||
| } | ||
|
|
||
| if ( | ||
| node.kind === ts.SyntaxKind.ArrowFunction || | ||
| node.kind === ts.SyntaxKind.FunctionDeclaration | ||
| ) { | ||
| return false; | ||
| } | ||
|
|
||
| return node.getChildren().some(functionBlockHasAwait); | ||
| }; | ||
|
|
||
| const functionBlockHasReturn = (node: ts.Node): boolean => { | ||
| if (isReturn(node)) { | ||
| return true; | ||
| } | ||
|
|
||
| if ( | ||
| node.kind === ts.SyntaxKind.ArrowFunction || | ||
| node.kind === ts.SyntaxKind.FunctionDeclaration | ||
| ) { | ||
| return false; | ||
| } | ||
|
|
||
| return node.getChildren().some(functionBlockHasReturn); | ||
| }; | ||
|
|
||
| const isShortArrowReturn = (node: FunctionNodeType) => | ||
| node.kind === ts.SyntaxKind.ArrowFunction && node.body.kind !== ts.SyntaxKind.Block; |
This file contains hidden or 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,122 @@ | ||
| async function a(){ | ||
eranshabi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ~~~~~ [0] | ||
| let b = 1; | ||
| console.log(b); | ||
| } | ||
|
|
||
| async function a(){ | ||
| let b = 1; | ||
| await console.log(b); | ||
| } | ||
|
|
||
| async function a(){ | ||
| let b = 1; | ||
| console.log(await b()); | ||
| } | ||
|
|
||
| async function a(){ | ||
| ~~~~~ [0] | ||
| let b = 1; | ||
| let c = async () => { | ||
| await fetch(); | ||
| }; | ||
| } | ||
|
|
||
| async function a(){ | ||
| ~~~~~ [Functions marked async must contain an await or return statement.] | ||
| let b = 1; | ||
| async function f() { | ||
| await fetch(); | ||
| }; | ||
| } | ||
|
|
||
| function a(){ | ||
| let b = 1; | ||
| async function f() { | ||
| ~~~~~ [Functions marked async must contain an await or return statement.] | ||
| fetch(); | ||
| }; | ||
| } | ||
|
|
||
| const a = async () => { | ||
| ~~~~~ [Functions marked async must contain an await or return statement.] | ||
| let b = 1; | ||
| console.log(b); | ||
| } | ||
|
|
||
| class A { | ||
| async b() { | ||
| ~~~~~ [Functions marked async must contain an await or return statement.] | ||
| console.log(1); | ||
| } | ||
| } | ||
|
|
||
| class A { | ||
| async b() { | ||
| await b(); | ||
| } | ||
| } | ||
|
|
||
| class A { | ||
| public a = async function b() { | ||
| await b(); | ||
| } | ||
| } | ||
|
|
||
| class A { | ||
| public a = async function b() { | ||
| ~~~~~ [Functions marked async must contain an await or return statement.] | ||
| b(); | ||
| } | ||
| } | ||
|
|
||
| class A { | ||
| public a = async () => { | ||
| await b(); | ||
| } | ||
| } | ||
|
|
||
| class A { | ||
| public a = async () => { | ||
| ~~~~~ [Functions marked async must contain an await or return statement.] | ||
| b(); | ||
| } | ||
| } | ||
|
|
||
| class A { | ||
| public a = async () => 1; | ||
| } | ||
|
|
||
| async () => { | ||
| await a(); | ||
| class A { | ||
| async b() { | ||
| ~~~~~ [Functions marked async must contain an await or return statement.] | ||
| console.log(1); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| async function a() { | ||
| let b = 1; | ||
| return b; | ||
| } | ||
|
|
||
| let a = async () => 1; | ||
|
|
||
| async function a() { | ||
| ~~~~~ [Functions marked async must contain an await or return statement.] | ||
| let b = 1; | ||
| let a = () => { | ||
| return 1; | ||
| } | ||
| } | ||
|
|
||
| async function foo; | ||
| ~~~~~ [Functions marked async must contain an await or return statement.] | ||
|
|
||
| function * foo() { | ||
| return 1; | ||
| } | ||
|
|
||
| [0]: Functions marked async must contain an await or return statement. | ||
This file contains hidden or 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": { | ||
| "no-async-without-await": 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.
Uh oh!
There was an error while loading. Please reload this page.