UNPKG

eslint-codemod-utils

Version:

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

49 lines (48 loc) 1.6 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') { const existing = Array.isArray(props.children) ? props.children : props.children ? [props.children] : []; const componentProps = { ...props, children: [...existing, ...children], }; return type(componentProps); } const filteredChildren = children.filter((child) => Boolean(child)); const selfClosing = filteredChildren.length === 0; const name = (0, __1.jsxIdentifier)(type); return (0, __1.jsxElement)({ openingElement: (0, __1.jsxOpeningElement)({ name, selfClosing, attributes: Object.keys(props) .filter((key) => key !== 'children') .map((prop) => { return (0, __1.jsxAttribute)({ name: (0, __1.jsxIdentifier)(prop), value: (0, __1.literal)('hello'), }); }), }), closingElement: selfClosing ? null : (0, __1.jsxClosingElement)({ name }), children: filteredChildren .map((child) => { if (typeof child === 'string' || typeof child === 'number') { return undefined; } return child; }) .filter((child) => Boolean(child)), }); }; exports.jsx = jsx;