|
| 1 | +/** |
| 2 | + * ESLint rule to enforce React.ReactElement for render helper functions. |
| 3 | + * |
| 4 | + * Wrong: |
| 5 | + * const renderItem = (item: Item): React.ReactNode => { ... } |
| 6 | + * const renderHeader = (): JSX.Element => { ... } |
| 7 | + * |
| 8 | + * Correct: |
| 9 | + * const renderItem = (item: Item): React.ReactElement => { ... } |
| 10 | + * const renderBadge = (): React.ReactElement | null => { ... } |
| 11 | + * |
| 12 | + * This rule targets camelCase functions starting with "render" to distinguish |
| 13 | + * render helpers from components (which should use React.FC<Props>). |
| 14 | + */ |
| 15 | + |
| 16 | +// Types that should be flagged and replaced with ReactElement |
| 17 | +const DISALLOWED_TYPES = [ |
| 18 | + 'ReactNode', // Too broad - use ReactElement or explicit union |
| 19 | + 'Element' // JSX.Element - use ReactElement for consistency |
| 20 | +] |
| 21 | + |
| 22 | +function isRenderFunction(name) { |
| 23 | + return /^render[A-Z]/.test(name) |
| 24 | +} |
| 25 | + |
| 26 | +function getTypeName(typeName) { |
| 27 | + if (typeName.type === 'Identifier') { |
| 28 | + return typeName.name |
| 29 | + } |
| 30 | + // Handle qualified names like `React.ReactNode` or `JSX.Element` |
| 31 | + // We only need the rightmost name since we use exact matches |
| 32 | + if (typeName.type === 'TSQualifiedName') { |
| 33 | + return typeName.right.name |
| 34 | + } |
| 35 | + return '' |
| 36 | +} |
| 37 | + |
| 38 | +function isDisallowedType(node) { |
| 39 | + if (!node) return null |
| 40 | + |
| 41 | + // Handle `React.ReactNode`, `JSX.Element`, etc. |
| 42 | + if (node.type === 'TSTypeReference') { |
| 43 | + const typeName = getTypeName(node.typeName) |
| 44 | + if (DISALLOWED_TYPES.includes(typeName)) { |
| 45 | + return typeName |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return null |
| 50 | +} |
| 51 | + |
| 52 | +function checkReturnType(typeAnnotation) { |
| 53 | + if (!typeAnnotation) return null |
| 54 | + |
| 55 | + const { typeAnnotation: innerType } = typeAnnotation |
| 56 | + |
| 57 | + // Handle union types like `React.ReactElement | null` |
| 58 | + // Only flag if the union contains a disallowed type |
| 59 | + if (innerType.type === 'TSUnionType') { |
| 60 | + for (const t of innerType.types) { |
| 61 | + const disallowed = isDisallowedType(t) |
| 62 | + if (disallowed != null) { |
| 63 | + return disallowed |
| 64 | + } |
| 65 | + } |
| 66 | + return null |
| 67 | + } |
| 68 | + |
| 69 | + return isDisallowedType(innerType) |
| 70 | +} |
| 71 | + |
| 72 | +export default { |
| 73 | + meta: { |
| 74 | + type: 'suggestion', |
| 75 | + docs: { |
| 76 | + description: |
| 77 | + 'Enforce React.ReactElement for render helper functions instead of ReactNode or JSX.Element', |
| 78 | + category: 'Stylistic Issues', |
| 79 | + recommended: false |
| 80 | + }, |
| 81 | + schema: [] |
| 82 | + }, |
| 83 | + create(context) { |
| 84 | + return { |
| 85 | + VariableDeclarator(node) { |
| 86 | + // Check if this is an arrow function |
| 87 | + if (node.init?.type !== 'ArrowFunctionExpression') return |
| 88 | + |
| 89 | + // Check if the variable name starts with "render" (camelCase render helper) |
| 90 | + const variableName = node.id?.name |
| 91 | + if (!variableName || !isRenderFunction(variableName)) return |
| 92 | + |
| 93 | + // Check if the arrow function has an explicit return type |
| 94 | + const arrowFunction = node.init |
| 95 | + if (!arrowFunction.returnType) return |
| 96 | + |
| 97 | + // Check if the return type is a disallowed type |
| 98 | + const disallowedType = checkReturnType(arrowFunction.returnType) |
| 99 | + if (disallowedType == null) return |
| 100 | + |
| 101 | + const suggestion = |
| 102 | + disallowedType === 'ReactNode' |
| 103 | + ? 'React.ReactElement (or React.ReactElement | null for nullable returns)' |
| 104 | + : 'React.ReactElement' |
| 105 | + |
| 106 | + context.report({ |
| 107 | + node: arrowFunction.returnType, |
| 108 | + message: `Render function '${variableName}' should return ${suggestion} instead of ${disallowedType}. Use: const ${variableName} = (...): React.ReactElement => { ... }` |
| 109 | + }) |
| 110 | + }, |
| 111 | + |
| 112 | + // Also check regular function declarations |
| 113 | + FunctionDeclaration(node) { |
| 114 | + const functionName = node.id?.name |
| 115 | + if (!functionName || !isRenderFunction(functionName)) return |
| 116 | + |
| 117 | + if (!node.returnType) return |
| 118 | + |
| 119 | + const disallowedType = checkReturnType(node.returnType) |
| 120 | + if (disallowedType == null) return |
| 121 | + |
| 122 | + const suggestion = |
| 123 | + disallowedType === 'ReactNode' |
| 124 | + ? 'React.ReactElement (or React.ReactElement | null for nullable returns)' |
| 125 | + : 'React.ReactElement' |
| 126 | + |
| 127 | + context.report({ |
| 128 | + node: node.returnType, |
| 129 | + message: `Render function '${functionName}' should return ${suggestion} instead of ${disallowedType}. Use: function ${functionName}(...): React.ReactElement { ... }` |
| 130 | + }) |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments