Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 3 | const createAstUtils = require('../util/ast'); |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 4 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 5 | module.exports = { |
| 6 | meta: { |
| 7 | type: 'suggestion', |
| 8 | docs: { |
| 9 | description: 'Disallow hooks' |
| 10 | }, |
| 11 | schema: [ |
| 12 | { |
| 13 | type: 'object', |
| 14 | properties: { |
| 15 | allow: { |
| 16 | type: 'array', |
| 17 | items: { |
| 18 | type: 'string' |
| 19 | } |
| 20 | } |
| 21 | }, |
| 22 | additionalProperties: false |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 23 | } |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 24 | ] |
| 25 | }, |
| 26 | |
| 27 | create(context) { |
| 28 | const astUtils = createAstUtils(context.settings); |
| 29 | const [ config = {} ] = context.options; |
| 30 | const { allow = [] } = config; |
| 31 | |
| 32 | return { |
| 33 | CallExpression(node) { |
| 34 | const isHookAllowed = allow.includes(node.callee.name); |
| 35 | |
| 36 | if (astUtils.isHookIdentifier(node.callee) && !isHookAllowed) { |
| 37 | context.report({ |
| 38 | node: node.callee, |
| 39 | message: `Unexpected use of Mocha \`${ node.callee.name }\` hook` |
| 40 | }); |
| 41 | } |
| 42 | } |
| 43 | }; |
| 44 | } |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 45 | }; |