rollup-plugin-data-qa
Version:
[](https://www.npmjs.com/package/rollup-plugin-data-qa) [](https://github.com/semantic-releas
41 lines (40 loc) • 1.57 kB
JavaScript
import { DATA_QA } from "../../pluginConstants.js";
import appendObject from "../../utils/magicString/appendObject/index.js";
import chainFunctionWithProps from "../../utils/magicString/chainFunctionWithProps/index.js";
import { isEmpty, last } from "lodash-es";
//#region src/core/injectStyledComponent/index.ts
function injectStyledComponent({ styledComponentName, styledComponentNames, node, parent, code, childOverrideParent = false }) {
if (node?.type === "MemberExpression" && styledComponentNames.has(node?.object.name) && parent?.property?.name !== "attrs") {
chainFunctionWithProps({
code,
startPosition: node.property?.end,
functionName: "attrs",
attrs: { [DATA_QA]: styledComponentName },
childOverrideParent
});
return true;
}
if (parent?.callee?.property?.name === "attrs" && node.type === "ArrowFunctionExpression" && !isEmpty(node.body.properties)) {
const properties = node.body.properties;
appendObject({
code,
startPosition: childOverrideParent ? last(properties).end : properties[0].start,
attrs: { [DATA_QA]: styledComponentName },
childOverrideParent
});
return true;
}
if (parent?.callee?.property?.name === "attrs" && node.type === "ObjectExpression" && !isEmpty(node.properties)) {
const properties = node.properties;
appendObject({
code,
startPosition: childOverrideParent ? last(properties).end : properties[0].start,
attrs: { [DATA_QA]: styledComponentName },
childOverrideParent
});
return true;
}
return false;
}
//#endregion
export { injectStyledComponent as default };