UNPKG

rollup-plugin-data-qa

Version:

[![npm](https://img.shields.io/npm/dm/rollup-plugin-data-qa.svg)](https://www.npmjs.com/package/rollup-plugin-data-qa) [![semantic-release](https://img.shields.io/badge/semantic-release-e10079.svg?logo=semantic-release)](https://github.com/semantic-releas

53 lines (52 loc) 1.69 kB
import { DATA_QA } from "../../pluginConstants.js"; import isObjectAssigning from "../../utils/isObjectAssigning/index.js"; import appendObject from "../../utils/magicString/appendObject/index.js"; import insertToObject from "../../utils/magicString/insertToObject/index.js"; import overwriteWithObject from "../../utils/magicString/overwriteWithObject/index.js"; import isReactNode from "../../utils/react/isReactNode/index.js"; import { isEmpty, last } from "lodash-es"; //#region src/core/injectReactFunctionComponent/index.ts function injectReactFunctionComponent({ node, code, componentName, childOverrideParent = false }) { if (!isReactNode(node)) return false; const tagProps = node.arguments[1]; if (!tagProps?.start && isEmpty(tagProps?.properties)) return false; if (!(tagProps.value !== null)) { overwriteWithObject({ code, startPosition: tagProps.start, endPosition: tagProps.end, attrs: { [DATA_QA]: componentName } }); return true; } if (isObjectAssigning(tagProps)) { const firstArgs = tagProps.arguments?.[0]; if (!firstArgs) return false; insertToObject({ code, node: firstArgs, attrs: { [DATA_QA]: componentName }, childOverrideParent }); return true; } if (isEmpty(tagProps.properties)) { insertToObject({ code, node: tagProps, attrs: { [DATA_QA]: componentName }, childOverrideParent }); return true; } const properties = tagProps.properties; appendObject({ code, startPosition: childOverrideParent ? last(properties).end : properties[0].start, attrs: { [DATA_QA]: componentName }, childOverrideParent }); return true; } //#endregion export { injectReactFunctionComponent as default };