Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | const isNil = require('ramda/src/isNil'); |
| 4 | const find = require('ramda/src/find'); |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 5 | const createAstUtils = require('../util/ast'); |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 6 | |
| 7 | const asyncMethods = [ 'async', 'callback', 'promise' ]; |
| 8 | |
| 9 | function hasAsyncCallback(functionExpression) { |
| 10 | return functionExpression.params.length === 1; |
| 11 | } |
| 12 | |
| 13 | function isAsyncFunction(functionExpression) { |
| 14 | return functionExpression.async === true; |
| 15 | } |
| 16 | |
| 17 | function findPromiseReturnStatement(nodes) { |
| 18 | return find(function (node) { |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 19 | return ( |
| 20 | node.type === 'ReturnStatement' && |
| 21 | node.argument && |
| 22 | node.argument.type !== 'Literal' |
| 23 | ); |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 24 | }, nodes); |
| 25 | } |
| 26 | |
| 27 | function doesReturnPromise(functionExpression) { |
| 28 | const bodyStatement = functionExpression.body; |
| 29 | let returnStatement = null; |
| 30 | |
| 31 | if (bodyStatement.type === 'BlockStatement') { |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 32 | returnStatement = findPromiseReturnStatement( |
| 33 | functionExpression.body.body |
| 34 | ); |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 35 | } else if (bodyStatement.type !== 'Literal') { |
| 36 | // allow arrow statements calling a promise with implicit return. |
| 37 | returnStatement = bodyStatement; |
| 38 | } |
| 39 | |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 40 | return returnStatement !== null && typeof returnStatement !== 'undefined'; |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 43 | module.exports = { |
| 44 | meta: { |
| 45 | type: 'suggestion', |
| 46 | docs: { |
Tim van der Lippe | 0ceb465 | 2022-01-06 14:23:36 +0100 | [diff] [blame^] | 47 | description: 'Disallow synchronous tests', |
| 48 | url: 'https://github.com/lo1tuma/eslint-plugin-mocha/blob/master/docs/rules/no-synchronous-tests.md' |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 49 | }, |
| 50 | schema: [ |
| 51 | { |
| 52 | type: 'object', |
| 53 | properties: { |
| 54 | allowed: { |
| 55 | type: 'array', |
| 56 | items: { |
| 57 | type: 'string', |
| 58 | enum: asyncMethods |
| 59 | }, |
| 60 | minItems: 1, |
| 61 | uniqueItems: true |
| 62 | } |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 63 | } |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 64 | } |
| 65 | ] |
| 66 | }, |
| 67 | create(context) { |
| 68 | const astUtils = createAstUtils(context.settings); |
| 69 | const options = context.options[0] || {}; |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 70 | const allowedAsyncMethods = isNil(options.allowed) ? |
| 71 | asyncMethods : |
| 72 | options.allowed; |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 73 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 74 | function check(node) { |
| 75 | if (astUtils.hasParentMochaFunctionCall(node)) { |
| 76 | // For each allowed async test method, check if it is used in the test |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 77 | const testAsyncMethods = allowedAsyncMethods.map(function ( |
| 78 | method |
| 79 | ) { |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 80 | switch (method) { |
| 81 | case 'async': |
| 82 | return isAsyncFunction(node); |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 83 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 84 | case 'callback': |
| 85 | return hasAsyncCallback(node); |
| 86 | |
| 87 | default: |
| 88 | return doesReturnPromise(node); |
| 89 | } |
| 90 | }); |
| 91 | |
| 92 | // Check that at least one allowed async test method is used in the test |
Tim van der Lippe | 2c89197 | 2021-07-29 16:22:50 +0100 | [diff] [blame] | 93 | const isAsyncTest = testAsyncMethods.includes(true); |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 94 | |
| 95 | if (!isAsyncTest) { |
Tim van der Lippe | 0ceb465 | 2022-01-06 14:23:36 +0100 | [diff] [blame^] | 96 | context.report({ node, message: 'Unexpected synchronous test.' }); |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 97 | } |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 100 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 101 | return { |
| 102 | FunctionExpression: check, |
| 103 | ArrowFunctionExpression: check |
| 104 | }; |
| 105 | } |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 106 | }; |