Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

chore(eslint): detect assertion-wrapped components (#15724)

Ben Bachem3bca62fb0db1
diff --git a/packages/eslint-plugin/src/react-components.ts b/packages/eslint-plugin/src/react-components.ts
index 770365d91f..8fa75afada 100644
--- a/packages/eslint-plugin/src/react-components.ts
+++ b/packages/eslint-plugin/src/react-components.ts
@@ -299,18 +299,35 @@ function isComponentWrapperCall(
function unwrapComponentInit(node: TSESTree.Node): TSESTree.Node {
let current = node;
- while (isComponentWrapperCall(current) && current.arguments[0]) {
- const firstArg = current.arguments[0];
+ while (true) {
if (
- firstArg.type !== AST_NODE_TYPES.ArrowFunctionExpression &&
- firstArg.type !== AST_NODE_TYPES.FunctionExpression &&
- firstArg.type !== AST_NODE_TYPES.CallExpression
+ current.type === AST_NODE_TYPES.TSAsExpression ||
+ current.type === AST_NODE_TYPES.TSNonNullExpression ||
+ current.type === AST_NODE_TYPES.TSSatisfiesExpression ||
+ current.type === AST_NODE_TYPES.TSTypeAssertion
) {
- break;
+ current = current.expression;
+ continue;
}
- current = firstArg;
+
+ if (isComponentWrapperCall(current) && current.arguments[0]) {
+ const firstArg = current.arguments[0];
+ if (
+ firstArg.type === AST_NODE_TYPES.ArrowFunctionExpression ||
+ firstArg.type === AST_NODE_TYPES.FunctionExpression ||
+ firstArg.type === AST_NODE_TYPES.CallExpression ||
+ firstArg.type === AST_NODE_TYPES.TSAsExpression ||
+ firstArg.type === AST_NODE_TYPES.TSNonNullExpression ||
+ firstArg.type === AST_NODE_TYPES.TSSatisfiesExpression ||
+ firstArg.type === AST_NODE_TYPES.TSTypeAssertion
+ ) {
+ current = firstArg;
+ continue;
+ }
+ }
+
+ return current;
}
- return current;
}
export function createComponentRootElementVisitors(
@@ -345,6 +362,15 @@ export function createComponentRootElementVisitors(
maybeVisitFunctionComponentRoots(init, node.id.name);
}
},
+ ExportDefaultDeclaration(node: TSESTree.ExportDefaultDeclaration) {
+ const init = unwrapComponentInit(node.declaration);
+ if (
+ init.type === AST_NODE_TYPES.ArrowFunctionExpression ||
+ init.type === AST_NODE_TYPES.FunctionExpression
+ ) {
+ maybeVisitFunctionComponentRoots(init, null);
+ }
+ },
};
}
@@ -352,15 +378,41 @@ function getWrappedForwardRefPropsType(
node: TSESTree.Node,
): TSESTree.TypeNode | null {
let current = node;
- while (isComponentWrapperCall(current)) {
+ while (true) {
+ if (
+ current.type === AST_NODE_TYPES.TSAsExpression ||
+ current.type === AST_NODE_TYPES.TSSatisfiesExpression ||
+ current.type === AST_NODE_TYPES.TSTypeAssertion
+ ) {
+ const propsType = getReactComponentPropsType(current.typeAnnotation);
+ if (propsType) return propsType;
+
+ current = current.expression;
+ continue;
+ }
+
+ if (current.type === AST_NODE_TYPES.TSNonNullExpression) {
+ current = current.expression;
+ continue;
+ }
+
+ if (!isComponentWrapperCall(current)) return null;
+
const propsType = getForwardRefPropsType(current);
if (propsType) return propsType;
const firstArg = current.arguments[0];
- if (firstArg?.type !== AST_NODE_TYPES.CallExpression) break;
+ if (
+ firstArg?.type !== AST_NODE_TYPES.CallExpression &&
+ firstArg?.type !== AST_NODE_TYPES.TSAsExpression &&
+ firstArg?.type !== AST_NODE_TYPES.TSNonNullExpression &&
+ firstArg?.type !== AST_NODE_TYPES.TSSatisfiesExpression &&
+ firstArg?.type !== AST_NODE_TYPES.TSTypeAssertion
+ ) {
+ return null;
+ }
current = firstArg;
}
- return null;
}
function getFunctionPropsType(
@@ -529,9 +581,20 @@ export function createComponentPropTypeVisitors(callbacks: PropTypeCallbacks) {
if (init.type === AST_NODE_TYPES.ClassExpression) {
addPropTypeRoot(getClassPropsType(init));
}
- if (node.init.type === AST_NODE_TYPES.CallExpression) {
- addPropTypeRoot(getWrappedForwardRefPropsType(node.init));
+ addPropTypeRoot(getWrappedForwardRefPropsType(node.init));
+ },
+ ExportDefaultDeclaration(node: TSESTree.ExportDefaultDeclaration) {
+ const init = unwrapComponentInit(node.declaration);
+ if (
+ init.type === AST_NODE_TYPES.ArrowFunctionExpression ||
+ init.type === AST_NODE_TYPES.FunctionExpression
+ ) {
+ maybeAddFunctionComponent(init, null);
+ }
+ if (init.type === AST_NODE_TYPES.ClassExpression) {
+ addPropTypeRoot(getClassPropsType(init));
}
+ addPropTypeRoot(getWrappedForwardRefPropsType(node.declaration));
},
ClassDeclaration(node: TSESTree.ClassDeclaration) {
if (!node.id || !isCapitalizedName(node.id.name)) return;
diff --git a/packages/eslint-plugin/src/rules/no-margin-on-root-elements.test.ts b/packages/eslint-plugin/src/rules/no-margin-on-root-elements.test.ts
index c26059cea1..5a313715ec 100644
--- a/packages/eslint-plugin/src/rules/no-margin-on-root-elements.test.ts
+++ b/packages/eslint-plugin/src/rules/no-margin-on-root-elements.test.ts
@@ -82,6 +82,18 @@ ruleTester.run("no-margin-on-root-elements", rule, {
code: `const Card = () => <div className="m-2" />;`,
errors: [{ messageId: "unexpectedClassName", data: { utility: "m-2" } }],
},
+ {
+ code: `const Card = (() => <div className="m-2" />) as React.FC;`,
+ errors: [{ messageId: "unexpectedClassName", data: { utility: "m-2" } }],
+ },
+ {
+ code: `const Card = (() => <div className="m-2" />) satisfies React.FC;`,
+ errors: [{ messageId: "unexpectedClassName", data: { utility: "m-2" } }],
+ },
+ {
+ code: `export default (() => <div className="m-2" />) as React.FC;`,
+ errors: [{ messageId: "unexpectedClassName", data: { utility: "m-2" } }],
+ },
{
code: `const Card = () => <div className="-mx-2" />;`,
errors: [
@@ -224,6 +236,18 @@ ruleTester.run("no-margin-on-root-elements", rule, {
code: `const Card = memo(() => <div className="mt-2" />);`,
errors: [{ messageId: "unexpectedClassName", data: { utility: "mt-2" } }],
},
+ {
+ code: `const Card = memo((() => <div className="mt-2" />) as React.FC);`,
+ errors: [{ messageId: "unexpectedClassName", data: { utility: "mt-2" } }],
+ },
+ {
+ code: `const Card = memo((() => <div className="mt-2" />) satisfies React.FC);`,
+ errors: [{ messageId: "unexpectedClassName", data: { utility: "mt-2" } }],
+ },
+ {
+ code: `export default memo(() => <div className="mt-2" />);`,
+ errors: [{ messageId: "unexpectedClassName", data: { utility: "mt-2" } }],
+ },
{
code: `const Card = forwardRef<HTMLDivElement, Props>(() => <div className="mt-2" />);`,
errors: [{ messageId: "unexpectedClassName", data: { utility: "mt-2" } }],
diff --git a/packages/eslint-plugin/src/rules/no-style-props.test.ts b/packages/eslint-plugin/src/rules/no-style-props.test.ts
index 72d3c6b234..3cc92782a8 100644
--- a/packages/eslint-plugin/src/rules/no-style-props.test.ts
+++ b/packages/eslint-plugin/src/rules/no-style-props.test.ts
@@ -151,6 +151,37 @@ ruleTester.run("no-style-props", rule, {
const Button: React.FC<ButtonProps> = (props) => <div />;`,
errors: [{ messageId: "unexpectedProp", data: { propName: "style" } }],
},
+ {
+ code: `type ButtonProps = { className?: string };
+ const Button = ((props: ButtonProps) => <div />) as React.FC<ButtonProps>;`,
+ errors: [
+ { messageId: "unexpectedProp", data: { propName: "className" } },
+ ],
+ },
+ {
+ code: `type ButtonProps = { className?: string };
+ const Button = ((props: ButtonProps) => <div />) satisfies React.FC<ButtonProps>;`,
+ errors: [
+ { messageId: "unexpectedProp", data: { propName: "className" } },
+ ],
+ },
+ {
+ code: `type ButtonProps = { className?: string };
+ const Button = <React.FC<ButtonProps>>((props: ButtonProps) => React.createElement("div"));`,
+ languageOptions: {
+ parserOptions: { ecmaFeatures: { jsx: false } },
+ },
+ errors: [
+ { messageId: "unexpectedProp", data: { propName: "className" } },
+ ],
+ },
+ {
+ code: `type ButtonProps = { className?: string };
+ export default ((props: ButtonProps) => <div />) as React.FC<ButtonProps>;`,
+ errors: [
+ { messageId: "unexpectedProp", data: { propName: "className" } },
+ ],
+ },
{
code: `const Button: React.FC<{ style?: React.CSSProperties }> = (props) => <div />;`,
errors: [{ messageId: "unexpectedProp", data: { propName: "style" } }],
@@ -291,6 +322,46 @@ ruleTester.run("no-style-props", rule, {
{ messageId: "unexpectedProp", data: { propName: "className" } },
],
},
+ {
+ code: `type ButtonProps = { className?: string };
+ const Button = forwardRef<HTMLButtonElement, ButtonProps>((props, ref) => <div ref={ref} />) as React.FC;`,
+ errors: [
+ { messageId: "unexpectedProp", data: { propName: "className" } },
+ ],
+ },
+ {
+ code: `type ButtonProps = { style?: React.CSSProperties };
+ const Button = memo(((props: ButtonProps) => <div />) as React.FC<ButtonProps>);`,
+ errors: [{ messageId: "unexpectedProp", data: { propName: "style" } }],
+ },
+ {
+ code: `type ButtonProps = { className?: string };
+ const Button = (forwardRef(((props) => <div />) as React.FC<ButtonProps>))!;`,
+ errors: [
+ { messageId: "unexpectedProp", data: { propName: "className" } },
+ ],
+ },
+ {
+ code: `type ButtonProps = { className?: string };
+ export default forwardRef<HTMLButtonElement, ButtonProps>((props, ref) => <div ref={ref} />);`,
+ errors: [
+ { messageId: "unexpectedProp", data: { propName: "className" } },
+ ],
+ },
+ {
+ code: `type ButtonProps = { className?: string };
+ export default (class extends React.Component<ButtonProps> { render() { return <div />; } });`,
+ errors: [
+ { messageId: "unexpectedProp", data: { propName: "className" } },
+ ],
+ },
+ {
+ code: `type ButtonProps = { className?: string };
+ const Button = factoryResult as React.FC<ButtonProps>;`,
+ errors: [
+ { messageId: "unexpectedProp", data: { propName: "className" } },
+ ],
+ },
{
code: `type ButtonProps = { className?: string };
class Button extends React.Component<ButtonProps> { render() { return <div />; } }`,
diff --git a/web/src/features/models/components/UpsertModelFormDialog.tsx b/web/src/features/models/components/UpsertModelFormDialog.tsx
index 3d405288b8..2eeb5df712 100644
--- a/web/src/features/models/components/UpsertModelFormDialog.tsx
+++ b/web/src/features/models/components/UpsertModelFormDialog.tsx
@@ -1,3 +1,4 @@
+/* eslint @repo/no-style-props: "off" */
import Link from "next/link";
import { useEffect, useState, useMemo } from "react";
import { useForm, useFieldArray } from "react-hook-form";