UNPKG

@lobehub/ui

Version:

Lobe UI is an open-source UI component library for building AIGC web apps

1 lines • 2.34 kB
{"version":3,"file":"noInlineFormSchema.mjs","names":[],"sources":["../../src/eslint/noInlineFormSchema.ts"],"sourcesContent":["import type { Rule } from 'eslint';\n\nconst SCHEMA_NAMESPACES = new Set(['z', 'v', 'yup']);\n\nconst rootIdentifier = (node: any): string | undefined => {\n let current = node;\n while (current) {\n if (current.type === 'Identifier') return current.name;\n if (current.type === 'CallExpression') current = current.callee;\n else if (current.type === 'MemberExpression') current = current.object;\n else return undefined;\n }\n return undefined;\n};\n\nconst isInlineSchema = (node: any) =>\n node?.type === 'CallExpression' && SCHEMA_NAMESPACES.has(rootIdentifier(node) ?? '');\n\nconst MESSAGE =\n 'Schemas built inline are recreated on every render. Define the schema at module scope (or in useMemo) and pass the reference.';\n\nexport const noInlineFormSchema: Rule.RuleModule = {\n create: (context) => ({\n 'CallExpression[callee.name=\"useForm\"] > ObjectExpression > Property': function (node: any) {\n if (node.key?.name === 'schema' && isInlineSchema(node.value))\n context.report({ message: MESSAGE, node: node.value });\n },\n 'JSXAttribute': function(node: any) {\n if (node.name?.name !== 'validate') return;\n const expression = node.value?.expression;\n if (isInlineSchema(expression)) context.report({ message: MESSAGE, node: expression });\n },\n }),\n meta: {\n docs: { description: 'Disallow constructing form schemas inline in render' },\n schema: [],\n type: 'suggestion',\n },\n};\n"],"mappings":";AAEA,MAAM,oCAAoB,IAAI,IAAI;CAAC;CAAK;CAAK;AAAK,CAAC;AAEnD,MAAM,kBAAkB,SAAkC;CACxD,IAAI,UAAU;CACd,OAAO,SAAS;EACd,IAAI,QAAQ,SAAS,cAAc,OAAO,QAAQ;EAClD,IAAI,QAAQ,SAAS,kBAAkB,UAAU,QAAQ;OACpD,IAAI,QAAQ,SAAS,oBAAoB,UAAU,QAAQ;OAC3D,OAAO,KAAA;CACd;AAEF;AAEA,MAAM,kBAAkB,SACtB,MAAM,SAAS,oBAAoB,kBAAkB,IAAI,eAAe,IAAI,KAAK,EAAE;AAErF,MAAM,UACJ;AAEF,MAAa,qBAAsC;CACjD,SAAS,aAAa;EACpB,yEAAuE,SAAU,MAAW;GAC1F,IAAI,KAAK,KAAK,SAAS,YAAY,eAAe,KAAK,KAAK,GAC1D,QAAQ,OAAO;IAAE,SAAS;IAAS,MAAM,KAAK;GAAM,CAAC;EACzD;EACA,gBAAgB,SAAS,MAAW;GAClC,IAAI,KAAK,MAAM,SAAS,YAAY;GACpC,MAAM,aAAa,KAAK,OAAO;GAC/B,IAAI,eAAe,UAAU,GAAG,QAAQ,OAAO;IAAE,SAAS;IAAS,MAAM;GAAW,CAAC;EACvF;CACF;CACA,MAAM;EACJ,MAAM,EAAE,aAAa,sDAAsD;EAC3E,QAAQ,CAAC;EACT,MAAM;CACR;AACF"}