UNPKG

eslint-plugin-rut

Version:

ESLint plugin for enforcing Rut (React testing) best practices.

49 lines (48 loc) 1.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const RENDER_NAMES = ['render', 'renderAndWait']; const rule = { meta: { type: 'suggestion', docs: { category: 'Best Practices', description: 'Require generics for `render()` and `renderAndWait()` functions.', recommended: true, }, messages: { missingGeneric: 'Render is missing the props generic type.', }, }, create(context) { return { CallExpression(node) { if (node.type === 'CallExpression' && node.callee.type === 'Identifier' && RENDER_NAMES.includes(node.callee.name)) { if (node.arguments.length === 0) { return; } // @ts-expect-error const element = node.arguments[0]; // Not JSX if (element.type !== 'JSXElement' || element.openingElement.type !== 'JSXOpeningElement' || element.openingElement.name.type !== 'JSXIdentifier') { return; } // Don't need generics for host elements if (element.openingElement.name.name.match(/^[a-z]/u)) { return; } if (!node.typeParameters) { context.report({ node, messageId: 'missingGeneric', }); } } }, }; }, }; exports.default = rule;