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: { |
Tim van der Lippe | 0ceb465 | 2022-01-06 14:23:36 +0100 | [diff] [blame^] | 9 | description: 'Disallow hooks', |
| 10 | url: 'https://github.com/lo1tuma/eslint-plugin-mocha/blob/master/docs/rules/no-hooks.md' |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 11 | }, |
| 12 | schema: [ |
| 13 | { |
| 14 | type: 'object', |
| 15 | properties: { |
| 16 | allow: { |
| 17 | type: 'array', |
| 18 | items: { |
| 19 | type: 'string' |
| 20 | } |
| 21 | } |
| 22 | }, |
| 23 | additionalProperties: false |
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 | ] |
| 26 | }, |
| 27 | |
| 28 | create(context) { |
| 29 | const astUtils = createAstUtils(context.settings); |
| 30 | const [ config = {} ] = context.options; |
| 31 | const { allow = [] } = config; |
| 32 | |
| 33 | return { |
| 34 | CallExpression(node) { |
| 35 | const isHookAllowed = allow.includes(node.callee.name); |
| 36 | |
| 37 | if (astUtils.isHookIdentifier(node.callee) && !isHookAllowed) { |
| 38 | context.report({ |
| 39 | node: node.callee, |
| 40 | message: `Unexpected use of Mocha \`${ node.callee.name }\` hook` |
| 41 | }); |
| 42 | } |
| 43 | } |
| 44 | }; |
| 45 | } |
Jack Franklin | 8b9aa2f | 2020-02-12 16:35:15 +0000 | [diff] [blame] | 46 | }; |