|
| 1 | +import { |
| 2 | + TSESTree, |
| 3 | + TSESLint, |
| 4 | + AST_NODE_TYPES, |
| 5 | +} from '@typescript-eslint/experimental-utils'; |
| 6 | +import baseRule from 'eslint/lib/rules/require-await'; |
| 7 | +import * as tsutils from 'tsutils'; |
| 8 | +import ts from 'typescript'; |
| 9 | +import * as util from '../util'; |
| 10 | + |
| 11 | +type Options = util.InferOptionsTypeFromRule<typeof baseRule>; |
| 12 | +type MessageIds = util.InferMessageIdsTypeFromRule<typeof baseRule>; |
| 13 | + |
| 14 | +interface ScopeInfo { |
| 15 | + upper: ScopeInfo | null; |
| 16 | + returnsPromise: boolean; |
| 17 | +} |
| 18 | + |
| 19 | +export default util.createRule<Options, MessageIds>({ |
| 20 | + name: 'require-await', |
| 21 | + meta: { |
| 22 | + type: 'suggestion', |
| 23 | + docs: { |
| 24 | + description: 'Disallow async functions which have no `await` expression', |
| 25 | + category: 'Best Practices', |
| 26 | + recommended: false, |
| 27 | + }, |
| 28 | + schema: baseRule.meta.schema, |
| 29 | + messages: baseRule.meta.messages, |
| 30 | + }, |
| 31 | + defaultOptions: [], |
| 32 | + create(context) { |
| 33 | + const rules = baseRule.create(context); |
| 34 | + const parserServices = util.getParserServices(context); |
| 35 | + const checker = parserServices.program.getTypeChecker(); |
| 36 | + |
| 37 | + let scopeInfo: ScopeInfo | null = null; |
| 38 | + |
| 39 | + /** |
| 40 | + * Push the scope info object to the stack. |
| 41 | + * |
| 42 | + * @returns {void} |
| 43 | + */ |
| 44 | + function enterFunction( |
| 45 | + node: |
| 46 | + | TSESTree.FunctionDeclaration |
| 47 | + | TSESTree.FunctionExpression |
| 48 | + | TSESTree.ArrowFunctionExpression, |
| 49 | + ) { |
| 50 | + scopeInfo = { |
| 51 | + upper: scopeInfo, |
| 52 | + returnsPromise: false, |
| 53 | + }; |
| 54 | + |
| 55 | + switch (node.type) { |
| 56 | + case AST_NODE_TYPES.FunctionDeclaration: |
| 57 | + rules.FunctionDeclaration(node); |
| 58 | + break; |
| 59 | + |
| 60 | + case AST_NODE_TYPES.FunctionExpression: |
| 61 | + rules.FunctionExpression(node); |
| 62 | + break; |
| 63 | + |
| 64 | + case AST_NODE_TYPES.ArrowFunctionExpression: |
| 65 | + rules.ArrowFunctionExpression(node); |
| 66 | + break; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Pop the top scope info object from the stack. |
| 72 | + * Passes through to the base rule if the function doesn't return a promise |
| 73 | + * |
| 74 | + * @param {ASTNode} node - The node exiting |
| 75 | + * @returns {void} |
| 76 | + */ |
| 77 | + function exitFunction( |
| 78 | + node: |
| 79 | + | TSESTree.FunctionDeclaration |
| 80 | + | TSESTree.FunctionExpression |
| 81 | + | TSESTree.ArrowFunctionExpression, |
| 82 | + ) { |
| 83 | + if (scopeInfo) { |
| 84 | + if (!scopeInfo.returnsPromise) { |
| 85 | + switch (node.type) { |
| 86 | + case AST_NODE_TYPES.FunctionDeclaration: |
| 87 | + rules['FunctionDeclaration:exit'](node); |
| 88 | + break; |
| 89 | + |
| 90 | + case AST_NODE_TYPES.FunctionExpression: |
| 91 | + rules['FunctionExpression:exit'](node); |
| 92 | + break; |
| 93 | + |
| 94 | + case AST_NODE_TYPES.ArrowFunctionExpression: |
| 95 | + rules['ArrowFunctionExpression:exit'](node); |
| 96 | + break; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + scopeInfo = scopeInfo.upper; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return { |
| 105 | + 'FunctionDeclaration[async = true]': enterFunction, |
| 106 | + 'FunctionExpression[async = true]': enterFunction, |
| 107 | + 'ArrowFunctionExpression[async = true]': enterFunction, |
| 108 | + 'FunctionDeclaration[async = true]:exit': exitFunction, |
| 109 | + 'FunctionExpression[async = true]:exit': exitFunction, |
| 110 | + 'ArrowFunctionExpression[async = true]:exit': exitFunction, |
| 111 | + |
| 112 | + ReturnStatement(node: TSESTree.ReturnStatement) { |
| 113 | + if (!scopeInfo) { |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + const { expression } = parserServices.esTreeNodeToTSNodeMap.get< |
| 118 | + ts.ReturnStatement |
| 119 | + >(node); |
| 120 | + if (!expression) { |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + const type = checker.getTypeAtLocation(expression); |
| 125 | + if (tsutils.isThenableType(checker, expression, type)) { |
| 126 | + scopeInfo.returnsPromise = true; |
| 127 | + } |
| 128 | + }, |
| 129 | + |
| 130 | + AwaitExpression: rules.AwaitExpression as TSESLint.RuleFunction< |
| 131 | + TSESTree.Node |
| 132 | + >, |
| 133 | + ForOfStatement: rules.ForOfStatement as TSESLint.RuleFunction< |
| 134 | + TSESTree.Node |
| 135 | + >, |
| 136 | + }; |
| 137 | + }, |
| 138 | +}); |
0 commit comments