|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { getDocsUrl, ASYNC_UTILS } = require('../utils'); |
| 4 | + |
| 5 | +const VALID_PARENTS = [ |
| 6 | + 'AwaitExpression', |
| 7 | + 'ArrowFunctionExpression', |
| 8 | + 'ReturnStatement', |
| 9 | +]; |
| 10 | + |
| 11 | +const ASYNC_UTILS_REGEXP = new RegExp(`^(${ASYNC_UTILS.join('|')})$`); |
| 12 | + |
| 13 | +module.exports = { |
| 14 | + meta: { |
| 15 | + type: 'problem', |
| 16 | + docs: { |
| 17 | + description: 'Enforce async utils to be awaited properly', |
| 18 | + category: 'Best Practices', |
| 19 | + recommended: true, |
| 20 | + url: getDocsUrl('await-async-utils'), |
| 21 | + }, |
| 22 | + messages: { |
| 23 | + awaitAsyncUtil: 'Promise returned from `{{ name }}` must be handled', |
| 24 | + }, |
| 25 | + fixable: null, |
| 26 | + schema: [], |
| 27 | + }, |
| 28 | + |
| 29 | + create: function(context) { |
| 30 | + const testingLibraryUtilUsage = []; |
| 31 | + return { |
| 32 | + [`CallExpression > Identifier[name=${ASYNC_UTILS_REGEXP}]`](node) { |
| 33 | + if (!isAwaited(node.parent.parent) && !isPromiseResolved(node)) { |
| 34 | + testingLibraryUtilUsage.push(node); |
| 35 | + } |
| 36 | + }, |
| 37 | + 'Program:exit'() { |
| 38 | + testingLibraryUtilUsage.forEach(node => { |
| 39 | + const variableDeclaratorParent = node.parent.parent; |
| 40 | + |
| 41 | + const references = |
| 42 | + (variableDeclaratorParent.type === 'VariableDeclarator' && |
| 43 | + context |
| 44 | + .getDeclaredVariables(variableDeclaratorParent)[0] |
| 45 | + .references.slice(1)) || |
| 46 | + []; |
| 47 | + |
| 48 | + if ( |
| 49 | + references && |
| 50 | + references.length === 0 && |
| 51 | + !isAwaited(node.parent.parent) && |
| 52 | + !isPromiseResolved(node) |
| 53 | + ) { |
| 54 | + context.report({ |
| 55 | + node, |
| 56 | + messageId: 'awaitAsyncUtil', |
| 57 | + data: { |
| 58 | + name: node.name, |
| 59 | + }, |
| 60 | + }); |
| 61 | + } else { |
| 62 | + for (const reference of references) { |
| 63 | + const referenceNode = reference.identifier; |
| 64 | + if ( |
| 65 | + !isAwaited(referenceNode.parent) && |
| 66 | + !isPromiseResolved(referenceNode) |
| 67 | + ) { |
| 68 | + context.report({ |
| 69 | + node, |
| 70 | + messageId: 'awaitAsyncUtil', |
| 71 | + data: { |
| 72 | + name: node.name, |
| 73 | + }, |
| 74 | + }); |
| 75 | + |
| 76 | + break; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + }); |
| 81 | + }, |
| 82 | + }; |
| 83 | + }, |
| 84 | +}; |
| 85 | + |
| 86 | +function isAwaited(node) { |
| 87 | + return VALID_PARENTS.includes(node.type); |
| 88 | +} |
| 89 | + |
| 90 | +const hasAThenProperty = node => |
| 91 | + node.type === 'MemberExpression' && node.property.name === 'then'; |
| 92 | + |
| 93 | +function isPromiseResolved(node) { |
| 94 | + const parent = node.parent; |
| 95 | + |
| 96 | + // wait(...).then(...) |
| 97 | + if (parent.type === 'CallExpression') { |
| 98 | + return hasAThenProperty(parent.parent); |
| 99 | + } |
| 100 | + |
| 101 | + // promise.then(...) |
| 102 | + return hasAThenProperty(parent); |
| 103 | +} |
0 commit comments