-
Notifications
You must be signed in to change notification settings - Fork 279
Integrate eslint react-fc-component-definition, react-render-function-definition
#5902
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Jon-edge
wants to merge
2
commits into
develop
Choose a base branch
from
jon/eslint-react-fc
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+252
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
scripts/eslint-plugin-edge/react-fc-component-definition.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| /** | ||
| * ESLint rule to enforce React.FC<Props> for component definitions. | ||
| * | ||
| * Wrong: | ||
| * export const Component = (props: Props): React.ReactElement | null => { ... } | ||
| * | ||
| * Correct: | ||
| * export const Component: React.FC<Props> = props => { ... } | ||
| * | ||
| * This rule only targets PascalCase-named arrow functions to distinguish | ||
| * components from render helper functions (which should use explicit return types). | ||
| */ | ||
|
|
||
| const JSX_RETURN_TYPES = [ | ||
| 'ReactElement', | ||
| 'ReactNode', | ||
| 'Element', // JSX.Element | ||
| 'JSXElement' | ||
| ] | ||
|
|
||
| function isPascalCase(name) { | ||
| return /^[A-Z][a-zA-Z0-9]*$/.test(name) | ||
| } | ||
|
|
||
| function isJsxReturnType(typeAnnotation) { | ||
| if (!typeAnnotation) return false | ||
|
|
||
| const { typeAnnotation: innerType } = typeAnnotation | ||
|
|
||
| // Handle union types like `React.ReactElement | null` | ||
| if (innerType.type === 'TSUnionType') { | ||
| return innerType.types.some(t => isJsxType(t)) | ||
| } | ||
|
|
||
| return isJsxType(innerType) | ||
| } | ||
|
|
||
| function isJsxType(node) { | ||
| if (!node) return false | ||
|
|
||
| // Handle `React.ReactElement`, `React.JSX.Element`, etc. | ||
| if (node.type === 'TSTypeReference') { | ||
| const typeName = getTypeName(node.typeName) | ||
| return JSX_RETURN_TYPES.some(t => typeName.endsWith(t)) | ||
| } | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| function getTypeName(typeName) { | ||
| if (typeName.type === 'Identifier') { | ||
| return typeName.name | ||
| } | ||
| // Handle qualified names like `React.ReactElement` or `React.JSX.Element` | ||
| // We only need the rightmost name since we use `.endsWith()` checks | ||
| if (typeName.type === 'TSQualifiedName') { | ||
| return typeName.right.name | ||
| } | ||
| return '' | ||
| } | ||
|
|
||
| export default { | ||
| meta: { | ||
| type: 'suggestion', | ||
| docs: { | ||
| description: | ||
| 'Enforce React.FC<Props> for component definitions instead of explicit return types', | ||
| category: 'Stylistic Issues', | ||
| recommended: false | ||
| }, | ||
| schema: [] | ||
| }, | ||
| create(context) { | ||
| return { | ||
| VariableDeclarator(node) { | ||
| // Check if this is an arrow function | ||
| if (node.init?.type !== 'ArrowFunctionExpression') return | ||
|
|
||
| // Check if the variable name is PascalCase (component convention) | ||
| const variableName = node.id?.name | ||
| if (!variableName || !isPascalCase(variableName)) return | ||
|
|
||
| // Check if the arrow function has an explicit return type | ||
| const arrowFunction = node.init | ||
| if (!arrowFunction.returnType) return | ||
|
|
||
| // Check if the return type is a JSX-related type | ||
| if (!isJsxReturnType(arrowFunction.returnType)) return | ||
|
|
||
| // Check if the variable already has a React.FC type annotation | ||
| const variableType = node.id.typeAnnotation | ||
| if (variableType) { | ||
| const typeName = getTypeName( | ||
| variableType.typeAnnotation?.typeName || {} | ||
| ) | ||
| if ( | ||
| typeName.endsWith('FC') || | ||
| typeName.endsWith('FunctionComponent') | ||
| ) { | ||
| return // Already using React.FC | ||
| } | ||
| } | ||
|
|
||
| context.report({ | ||
| node: arrowFunction.returnType, | ||
| message: `Component '${variableName}' should use React.FC<Props> instead of an explicit return type. Use: const ${variableName}: React.FC<Props> = props => { ... }` | ||
| }) | ||
| } | ||
| } | ||
| } | ||
| } | ||
134 changes: 134 additions & 0 deletions
134
scripts/eslint-plugin-edge/react-render-function-definition.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| /** | ||
| * ESLint rule to enforce React.ReactElement for render helper functions. | ||
| * | ||
| * Wrong: | ||
| * const renderItem = (item: Item): React.ReactNode => { ... } | ||
| * const renderHeader = (): JSX.Element => { ... } | ||
| * | ||
| * Correct: | ||
| * const renderItem = (item: Item): React.ReactElement => { ... } | ||
| * const renderBadge = (): React.ReactElement | null => { ... } | ||
| * | ||
| * This rule targets camelCase functions starting with "render" to distinguish | ||
| * render helpers from components (which should use React.FC<Props>). | ||
| */ | ||
|
|
||
| // Types that should be flagged and replaced with ReactElement | ||
| const DISALLOWED_TYPES = [ | ||
| 'ReactNode', // Too broad - use ReactElement or explicit union | ||
| 'Element' // JSX.Element - use ReactElement for consistency | ||
| ] | ||
|
|
||
| function isRenderFunction(name) { | ||
| return /^render[A-Z]/.test(name) | ||
| } | ||
|
|
||
| function getTypeName(typeName) { | ||
| if (typeName.type === 'Identifier') { | ||
| return typeName.name | ||
| } | ||
| // Handle qualified names like `React.ReactNode` or `JSX.Element` | ||
| // We only need the rightmost name since we use exact matches | ||
| if (typeName.type === 'TSQualifiedName') { | ||
| return typeName.right.name | ||
| } | ||
| return '' | ||
| } | ||
|
|
||
| function isDisallowedType(node) { | ||
| if (!node) return null | ||
|
|
||
| // Handle `React.ReactNode`, `JSX.Element`, etc. | ||
| if (node.type === 'TSTypeReference') { | ||
| const typeName = getTypeName(node.typeName) | ||
| if (DISALLOWED_TYPES.includes(typeName)) { | ||
| return typeName | ||
| } | ||
| } | ||
|
|
||
| return null | ||
| } | ||
|
|
||
| function checkReturnType(typeAnnotation) { | ||
| if (!typeAnnotation) return null | ||
|
|
||
| const { typeAnnotation: innerType } = typeAnnotation | ||
|
|
||
| // Handle union types like `React.ReactElement | null` | ||
| // Only flag if the union contains a disallowed type | ||
| if (innerType.type === 'TSUnionType') { | ||
| for (const t of innerType.types) { | ||
| const disallowed = isDisallowedType(t) | ||
| if (disallowed != null) { | ||
| return disallowed | ||
| } | ||
| } | ||
| return null | ||
| } | ||
|
|
||
| return isDisallowedType(innerType) | ||
| } | ||
|
|
||
| export default { | ||
| meta: { | ||
| type: 'suggestion', | ||
| docs: { | ||
| description: | ||
| 'Enforce React.ReactElement for render helper functions instead of ReactNode or JSX.Element', | ||
| category: 'Stylistic Issues', | ||
| recommended: false | ||
| }, | ||
| schema: [] | ||
| }, | ||
| create(context) { | ||
| return { | ||
| VariableDeclarator(node) { | ||
| // Check if this is an arrow function | ||
| if (node.init?.type !== 'ArrowFunctionExpression') return | ||
|
|
||
| // Check if the variable name starts with "render" (camelCase render helper) | ||
| const variableName = node.id?.name | ||
| if (!variableName || !isRenderFunction(variableName)) return | ||
|
|
||
| // Check if the arrow function has an explicit return type | ||
| const arrowFunction = node.init | ||
| if (!arrowFunction.returnType) return | ||
|
|
||
| // Check if the return type is a disallowed type | ||
| const disallowedType = checkReturnType(arrowFunction.returnType) | ||
| if (disallowedType == null) return | ||
|
|
||
| const suggestion = | ||
| disallowedType === 'ReactNode' | ||
| ? 'React.ReactElement (or React.ReactElement | null for nullable returns)' | ||
| : 'React.ReactElement' | ||
|
|
||
| context.report({ | ||
| node: arrowFunction.returnType, | ||
| message: `Render function '${variableName}' should return ${suggestion} instead of ${disallowedType}. Use: const ${variableName} = (...): React.ReactElement => { ... }` | ||
| }) | ||
| }, | ||
|
|
||
| // Also check regular function declarations | ||
| FunctionDeclaration(node) { | ||
| const functionName = node.id?.name | ||
| if (!functionName || !isRenderFunction(functionName)) return | ||
|
|
||
| if (!node.returnType) return | ||
|
|
||
| const disallowedType = checkReturnType(node.returnType) | ||
| if (disallowedType == null) return | ||
|
|
||
| const suggestion = | ||
| disallowedType === 'ReactNode' | ||
| ? 'React.ReactElement (or React.ReactElement | null for nullable returns)' | ||
| : 'React.ReactElement' | ||
|
|
||
| context.report({ | ||
| node: node.returnType, | ||
| message: `Render function '${functionName}' should return ${suggestion} instead of ${disallowedType}. Use: function ${functionName}(...): React.ReactElement { ... }` | ||
| }) | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Type matching incorrectly flags non-JSX types ending with "Element"
Low Severity
The
.endsWith()check combined with'Element'inJSX_RETURN_TYPEScauses false positives. SincegetTypeNamereturns only the rightmost identifier (e.g.,'HTMLElement'for the typeHTMLElement), the check'HTMLElement'.endsWith('Element')evaluates to true. This incorrectly flags PascalCase functions returningHTMLElement,SVGElement, or any custom type ending withElementas needingReact.FC<Props>. The second rule correctly uses.includes()for exact matching, but this rule's.endsWith()approach is too broad.Additional Locations (1)
scripts/eslint-plugin-edge/react-fc-component-definition.mjs#L16-L17