|
| 1 | +/** |
| 2 | + * @fileoverview Force the use of named functions inside a useEffect |
| 3 | + * @author Cyril Bonaccini |
| 4 | + */ |
| 5 | +"use strict"; |
| 6 | + |
| 7 | +//------------------------------------------------------------------------------ |
| 8 | +// Rule Definition |
| 9 | +//------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 12 | +module.exports = { |
| 13 | + meta: { |
| 14 | + type: "suggestion", // `problem`, `suggestion`, or `layout` |
| 15 | + docs: { |
| 16 | + description: "Enforces the use of named functions inside a useEffect", |
| 17 | + recommended: false, |
| 18 | + url: "https://github.com/bamlab/react-native-project-config/tree/main/packages/eslint-plugin/docs/rules/require-named-effect.md", // URL to the documentation page for this rule |
| 19 | + }, |
| 20 | + fixable: null, // Or `code` or `whitespace` |
| 21 | + schema: [], // Add a schema if the rule has options, |
| 22 | + messages: { |
| 23 | + useNamedFunction: "Complex effects must be a named function.", |
| 24 | + }, |
| 25 | + }, |
| 26 | + |
| 27 | + create(context) { |
| 28 | + //---------------------------------------------------------------------- |
| 29 | + // Helpers |
| 30 | + //---------------------------------------------------------------------- |
| 31 | + |
| 32 | + const isUseEffect = (node) => node.callee.name === "useEffect"; |
| 33 | + |
| 34 | + const argumentIsArrowFunction = (node) => |
| 35 | + node.arguments[0].type === "ArrowFunctionExpression"; |
| 36 | + |
| 37 | + const effectBodyIsSingleFunction = (node) => { |
| 38 | + const { body } = node.arguments[0]; |
| 39 | + |
| 40 | + // It's a single unwrapped function call: |
| 41 | + // `useEffect(() => theNameOfAFunction(), []);` |
| 42 | + if (body.type === "CallExpression") { |
| 43 | + return true; |
| 44 | + } |
| 45 | + |
| 46 | + // There's a function body, but it just calls another function: |
| 47 | + // `useEffect(() => { |
| 48 | + // theOnlyChildIsAFunctionCall(); |
| 49 | + // }, []);` |
| 50 | + if ( |
| 51 | + body.body.length && |
| 52 | + body.body.length === 1 && |
| 53 | + body.body[0] && |
| 54 | + body.body[0].expression && |
| 55 | + body.body[0].expression.type === "CallExpression" |
| 56 | + ) { |
| 57 | + return true; |
| 58 | + } |
| 59 | + |
| 60 | + return false; |
| 61 | + }; |
| 62 | + |
| 63 | + const fail = (report, node) => |
| 64 | + report({ node, messageId: "useNamedFunction" }); |
| 65 | + |
| 66 | + //---------------------------------------------------------------------- |
| 67 | + // Public |
| 68 | + //---------------------------------------------------------------------- |
| 69 | + |
| 70 | + return { |
| 71 | + CallExpression(node) { |
| 72 | + if ( |
| 73 | + isUseEffect(node) && |
| 74 | + argumentIsArrowFunction(node) && |
| 75 | + !effectBodyIsSingleFunction(node) |
| 76 | + ) { |
| 77 | + fail(context.report, node); |
| 78 | + } |
| 79 | + }, |
| 80 | + }; |
| 81 | + }, |
| 82 | +}; |
0 commit comments