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

70 lines (69 loc) 2.76 kB
import isJsxElement from "../react/isJsxElement/index.js"; import injectJsxElement from "../../core/injectJsxElement/index.js"; import isReactFragment from "../react/isReactFragment/index.js"; import isReactNode from "../react/isReactNode/index.js"; import injectReactFunctionComponent from "../../core/injectReactFunctionComponent/index.js"; import injectStyledComponent from "../../core/injectStyledComponent/index.js"; import formatName from "../formatName/index.js"; import findStyledComponentName from "../react/findStyledComponentName/index.js"; import { walk } from "estree-walker"; import { isEmpty, last } from "lodash-es"; //#region src/utils/transformAst/index.ts function transformAst({ ast, code, format, childOverrideParent, disabledReactFunctionComponent, disabledStyledComponent, styledComponentNames }) { const formattedNameCache = /* @__PURE__ */ new Map(); const styledComponentNameSet = new Set(styledComponentNames); const componentStack = []; let styledComponentName = ""; const getFormattedName = (name) => { const cached = formattedNameCache.get(name); if (cached) return cached; const formattedName = formatName(name, format); formattedNameCache.set(name, formattedName); return formattedName; }; walk(ast, { enter(node, parent) { if (isReactFragment(node)) return this.skip(); const isAttrsObjectExpression = !disabledStyledComponent && node.type === "ObjectExpression" && parent?.callee?.property?.name === "attrs"; if (node.type === "ObjectExpression" && !isAttrsObjectExpression) { if (!disabledReactFunctionComponent) return this.skip(); return; } if (isReactNode(node) || isJsxElement(node)) { if (!disabledReactFunctionComponent && !isEmpty(componentStack)) { if ((isJsxElement(node) ? injectJsxElement : injectReactFunctionComponent)({ code, componentName: getFormattedName(last(componentStack)), node, childOverrideParent })) return this.skip(); } if (!disabledStyledComponent) return this.skip(); return; } const nodeName = node.id?.name; if (nodeName && !disabledReactFunctionComponent) componentStack.push(nodeName); if (!disabledStyledComponent) { styledComponentName = findStyledComponentName(node) || styledComponentName; if (styledComponentName) { if (injectStyledComponent({ code, styledComponentName: getFormattedName(styledComponentName), styledComponentNames: styledComponentNameSet, node, parent, childOverrideParent })) { styledComponentName = ""; return this.skip(); } } } }, leave(node) { if (node.id?.name && !disabledReactFunctionComponent) componentStack.pop(); } }); } //#endregion export { transformAst as default };