{"path":"data.jsonl","content":"{\"instruction\":\"Write an ESLint rule 'component-function-max-lines.ts' and a test for it.\",\"rule_code\":\"import { ESLintUtils } from \\\"@typescript-eslint/utils\\\";\\nimport type { TSESTree } from \\\"@typescript-eslint/utils\\\";\\nimport { RuleContext } from \\\"@typescript-eslint/utils/ts-eslint\\\";\\n\\nconst url = \\\"https://example.com/rules/\\\";\\n\\nconst createRule = ESLintUtils.RuleCreator((ruleName: string) => `${url}${ruleName}`);\\n\\nfunction isMultilineFunction(\\n node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression | TSESTree.ArrowFunctionExpression,\\n) {\\n return node.body.type === \\\"BlockStatement\\\" && node.body.body.length > 1;\\n}\\n\\ntype AnyFunctionNode =\\n | TSESTree.FunctionDeclaration\\n | TSESTree.FunctionExpression\\n | TSESTree.ArrowFunctionExpression;\\n\\nfunction isComponentLikeFunction(node: AnyFunctionNode | null) {\\n if (typeof node?.body?.type !== \\\"string\\\" || node.body.type !== \\\"BlockStatement\\\") return false;\\n\\n return node.body.body.some((statement) => {\\n return (\\n statement.type === \\\"ReturnStatement\\\" &&\\n statement.argument &&\\n (statement.argument.type === \\\"JSXElement\\\" || statement.argument.type === \\\"JSXFragment\\\")\\n );\\n });\\n}\\n\\nfunction isComponentFunction(node: AnyFunctionNode | null) {\\n if (!node) return false;\\n\\n if (!isComponentLikeFunction(node)) return false;\\n\\n const parent = node.parent;\\n\\n if (parent?.type === \\\"VariableDeclarator\\\" && parent.id.type === \\\"Identifier\\\") {\\n return true;\\n }\\n\\n return (\\n node.type === \\\"FunctionDeclaration\\\" &&\\n (parent?.type === \\\"ExportNamedDeclaration\\\" ||\\n parent?.type === \\\"ExportDefaultDeclaration\\\" ||\\n parent?.type === \\\"Program\\\")\\n );\\n}\\n\\nfunction isInsideComponentFunction(node: TSESTree.Node | null) {\\n let currentNode = node;\\n while (currentNode) {\\n if (\\n currentNode.type === \\\"FunctionDeclaration\\\" ||\\n currentNode.type === \\\"FunctionExpression\\\" ||\\n currentNode.type === \\\"ArrowFunctionExpression\\\"\\n ) {\\n if (isComponentLikeFunction(currentNode)) return true;\\n }\\n\\n currentNode = currentNode.parent ?? null;\\n }\\n return false;\\n}\\n\\nconst componentFunctionMaxLines = createRule({\\n name: \\\"component-function-max-lines\\\",\\n meta: {\\n type: \\\"problem\\\",\\n docs: {\\n description: \\\"Functions declared inside components can have maximum 1 line\\\",\\n },\\n messages: {\\n componentFunctionMaxLines: \\\"Functions declared inside components can have maximum 1 line\\\",\\n },\\n schema: [],\\n },\\n create(context: RuleContext<\\\"componentFunctionMaxLines\\\", []>) {\\n return {\\n ArrowFunctionExpression(node) {\\n if (\\n !isComponentFunction(node) &&\\n isInsideComponentFunction(node.parent) &&\\n isMultilineFunction(node)\\n ) {\\n context.report({ node, messageId: \\\"componentFunctionMaxLines\\\" });\\n }\\n },\\n FunctionExpression(node) {\\n if (\\n !isComponentFunction(node) &&\\n isInsideComponentFunction(node.parent) &&\\n isMultilineFunction(node)\\n ) {\\n context.report({ node, messageId: \\\"componentFunctionMaxLines\\\" });\\n }\\n },\\n FunctionDeclaration(node) {\\n if (\\n !isComponentFunction(node) &&\\n isInsideComponentFunction(node.parent) &&\\n isMultilineFunction(node)\\n ) {\\n context.report({ node, messageId: \\\"componentFunctionMaxLines\\\" });\\n }\\n },\\n\\n JSXExpressionContainer(node) {\\n const expr = node.expression;\\n\\n if (\\n expr &&\\n expr.type === \\\"CallExpression\\\" &&\\n expr.callee.type === \\\"ArrowFunctionExpression\\\" &&\\n !isComponentFunction(expr.callee) &&\\n isMultilineFunction(expr.callee) &&\\n isInsideComponentFunction(node.parent)\\n ) {\\n context.report({ node, messageId: \\\"componentFunctionMaxLines\\\" });\\n }\\n },\\n };\\n },\\n defaultOptions: [],\\n});\\n\\nexport default componentFunctionMaxLines;\\n\",\"test_code\":\"import { runTests } from \\\"@root/test-utils/eslint-rule-tester\\\";\\nimport rule from \\\"@root/rules/component-function-max-lines\\\";\\n\\nconst validCases = {\\n valid: [\\n {\\n code: `\\n export const Component1 = () => {\\n const oneLineFunc = () => 42; // Ок\\n return
{description}
\\n{description}
\\n