@lobehub/ui
Version:
Lobe UI is an open-source UI component library for building AIGC web apps
44 lines (43 loc) • 1.43 kB
JavaScript
//#region src/eslint/noInlineFormSchema.ts
const SCHEMA_NAMESPACES = /* @__PURE__ */ new Set([
"z",
"v",
"yup"
]);
const rootIdentifier = (node) => {
let current = node;
while (current) {
if (current.type === "Identifier") return current.name;
if (current.type === "CallExpression") current = current.callee;
else if (current.type === "MemberExpression") current = current.object;
else return void 0;
}
};
const isInlineSchema = (node) => node?.type === "CallExpression" && SCHEMA_NAMESPACES.has(rootIdentifier(node) ?? "");
const MESSAGE = "Schemas built inline are recreated on every render. Define the schema at module scope (or in useMemo) and pass the reference.";
const noInlineFormSchema = {
create: (context) => ({
"CallExpression[callee.name=\"useForm\"] > ObjectExpression > Property": function(node) {
if (node.key?.name === "schema" && isInlineSchema(node.value)) context.report({
message: MESSAGE,
node: node.value
});
},
"JSXAttribute": function(node) {
if (node.name?.name !== "validate") return;
const expression = node.value?.expression;
if (isInlineSchema(expression)) context.report({
message: MESSAGE,
node: expression
});
}
}),
meta: {
docs: { description: "Disallow constructing form schemas inline in render" },
schema: [],
type: "suggestion"
}
};
//#endregion
export { noInlineFormSchema };
//# sourceMappingURL=noInlineFormSchema.mjs.map