Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | /* eslint "complexity": [ "error", 5 ] */ |
| 4 | |
| 5 | /** |
| 6 | * @fileoverview Disallow async functions as arguments to describe |
| 7 | */ |
| 8 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 9 | const createAstUtils = require('../util/ast'); |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 10 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 11 | module.exports = { |
| 12 | meta: { |
| 13 | type: 'problem', |
| 14 | docs: { |
Tim van der Lippe | 0ceb465 | 2022-01-06 14:23:36 +0100 | [diff] [blame^] | 15 | description: 'Disallow async functions passed to describe', |
| 16 | url: 'https://github.com/lo1tuma/eslint-plugin-mocha/blob/master/docs/rules/no-async-describe.md' |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 17 | }, |
Tim van der Lippe | 0ceb465 | 2022-01-06 14:23:36 +0100 | [diff] [blame^] | 18 | fixable: 'code', |
| 19 | schema: [] |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 20 | }, |
| 21 | create(context) { |
| 22 | const astUtils = createAstUtils(context.settings); |
| 23 | const sourceCode = context.getSourceCode(); |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 24 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 25 | function isFunction(node) { |
| 26 | return ( |
| 27 | node.type === 'FunctionExpression' || |
| 28 | node.type === 'FunctionDeclaration' || |
| 29 | node.type === 'ArrowFunctionExpression' |
| 30 | ); |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 31 | } |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 32 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 33 | function containsDirectAwait(node) { |
| 34 | if (node.type === 'AwaitExpression') { |
| 35 | return true; |
| 36 | } else if (node.type && !isFunction(node)) { |
| 37 | return Object.keys(node).some(function (key) { |
| 38 | if (Array.isArray(node[key])) { |
| 39 | return node[key].some(containsDirectAwait); |
| 40 | } else if (key !== 'parent' && node[key] && typeof node[key] === 'object') { |
| 41 | return containsDirectAwait(node[key]); |
| 42 | } |
| 43 | return false; |
| 44 | }); |
| 45 | } |
| 46 | return false; |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 47 | } |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 48 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 49 | function fixAsyncFunction(fixer, fn) { |
| 50 | if (!containsDirectAwait(fn.body)) { |
| 51 | // Remove the "async" token and all the whitespace before "function": |
| 52 | const [ asyncToken, functionToken ] = sourceCode.getFirstTokens(fn, 2); |
| 53 | return fixer.removeRange([ asyncToken.range[0], functionToken.range[0] ]); |
| 54 | } |
| 55 | return undefined; |
| 56 | } |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 57 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 58 | function isAsyncFunction(node) { |
| 59 | return node && (node.type === 'FunctionExpression' || |
| 60 | node.type === 'ArrowFunctionExpression') && node.async; |
| 61 | } |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 62 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 63 | return { |
| 64 | CallExpression(node) { |
| 65 | const name = astUtils.getNodeName(node.callee); |
| 66 | |
| 67 | if (astUtils.isDescribe(node)) { |
| 68 | const fnArg = node.arguments.slice(-1)[0]; |
| 69 | if (isAsyncFunction(fnArg)) { |
| 70 | context.report({ |
| 71 | node: fnArg, |
| 72 | message: `Unexpected async function in ${name}()`, |
| 73 | fix(fixer) { |
| 74 | return fixAsyncFunction(fixer, fnArg); |
| 75 | } |
| 76 | }); |
| 77 | } |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 80 | }; |
| 81 | } |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 82 | }; |