UNPKG

eslint-codemod-utils

Version:

A collection of AST helper functions for more complex ESLint rule fixes.

40 lines (39 loc) 1.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.jsx = void 0; const __1 = require(".."); const jsx = function (type, props, ...children) { if (!type) { return; } if (typeof type === 'function') { // merge props and children const componentProps = { ...props, // @ts-expect-error children: (props.children || []).concat(children), }; // render the function const element = type(componentProps); return element; } const filteredChildren = children.filter(Boolean); const selfClosing = !Boolean(filteredChildren.length); const name = (0, __1.jsxIdentifier)(type); return (0, __1.jsxElement)({ openingElement: (0, __1.jsxOpeningElement)({ name, selfClosing, attributes: Object.keys(props).map((prop) => { return (0, __1.jsxAttribute)({ name: (0, __1.jsxIdentifier)(prop), value: (0, __1.literal)('hello'), }); }), }), closingElement: selfClosing ? null : (0, __1.jsxClosingElement)({ name }), // @ts-expect-error children: filteredChildren.map(exports.jsx), }); }; exports.jsx = jsx;