|
| 1 | +import { |
| 2 | + TSESTree, |
| 3 | + AST_NODE_TYPES, |
| 4 | +} from '@typescript-eslint/experimental-utils'; |
| 5 | +import baseRule from 'eslint/lib/rules/no-empty-function'; |
| 6 | +import * as util from '../util'; |
| 7 | + |
| 8 | +type Options = util.InferOptionsTypeFromRule<typeof baseRule>; |
| 9 | +type MessageIds = util.InferMessageIdsTypeFromRule<typeof baseRule>; |
| 10 | + |
| 11 | +export default util.createRule<Options, MessageIds>({ |
| 12 | + name: 'no-empty-function', |
| 13 | + meta: { |
| 14 | + type: 'suggestion', |
| 15 | + docs: { |
| 16 | + description: 'Disallow empty functions', |
| 17 | + category: 'Best Practices', |
| 18 | + recommended: false, |
| 19 | + }, |
| 20 | + schema: baseRule.meta.schema, |
| 21 | + messages: baseRule.meta.messages, |
| 22 | + }, |
| 23 | + defaultOptions: [ |
| 24 | + { |
| 25 | + allow: [], |
| 26 | + }, |
| 27 | + ], |
| 28 | + create(context) { |
| 29 | + const rules = baseRule.create(context); |
| 30 | + |
| 31 | + /** |
| 32 | + * Checks if the node is a constructor |
| 33 | + * @param node the node to ve validated |
| 34 | + * @returns true if the node is a constructor |
| 35 | + * @private |
| 36 | + */ |
| 37 | + function isConstructor( |
| 38 | + node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression, |
| 39 | + ): boolean { |
| 40 | + return !!( |
| 41 | + node.parent && |
| 42 | + node.parent.type === 'MethodDefinition' && |
| 43 | + node.parent.kind === 'constructor' |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Check if the method body is empty |
| 49 | + * @param node the node to be validated |
| 50 | + * @returns true if the body is empty |
| 51 | + * @private |
| 52 | + */ |
| 53 | + function isBodyEmpty( |
| 54 | + node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression, |
| 55 | + ): boolean { |
| 56 | + return !node.body || node.body.body.length === 0; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Check if method has parameter properties |
| 61 | + * @param node the node to be validated |
| 62 | + * @returns true if the body has parameter properties |
| 63 | + * @private |
| 64 | + */ |
| 65 | + function hasParameterProperties( |
| 66 | + node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression, |
| 67 | + ): boolean { |
| 68 | + return ( |
| 69 | + node.params && |
| 70 | + node.params.some( |
| 71 | + param => param.type === AST_NODE_TYPES.TSParameterProperty, |
| 72 | + ) |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Checks if the method is a concise constructor (no function body, but has parameter properties) |
| 78 | + * @param node the node to be validated |
| 79 | + * @returns true if the method is a concise constructor |
| 80 | + * @private |
| 81 | + */ |
| 82 | + function isConciseConstructor( |
| 83 | + node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression, |
| 84 | + ) { |
| 85 | + // Check TypeScript specific nodes |
| 86 | + return ( |
| 87 | + isConstructor(node) && isBodyEmpty(node) && hasParameterProperties(node) |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + return { |
| 92 | + FunctionDeclaration(node: TSESTree.FunctionDeclaration) { |
| 93 | + if (!isConciseConstructor(node)) { |
| 94 | + rules.FunctionDeclaration(node); |
| 95 | + } |
| 96 | + }, |
| 97 | + FunctionExpression(node: TSESTree.FunctionExpression) { |
| 98 | + if (!isConciseConstructor(node)) { |
| 99 | + rules.FunctionExpression(node); |
| 100 | + } |
| 101 | + }, |
| 102 | + }; |
| 103 | + }, |
| 104 | +}); |
0 commit comments