{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/explicit-module-boundary-types */\nimport { NodePath, types as t } from '@babel/core'\n\nconst elements = ['svg', 'Svg']\n\ntype tag = 'title' | 'desc'\n\nexport interface Options {\n  tag: tag | null\n}\n\ninterface State {\n  opts: Options\n}\n\nconst createTagElement = (\n  tag: tag,\n  children: t.JSXExpressionContainer[] = [],\n  attributes: (t.JSXAttribute | t.JSXSpreadAttribute)[] = [],\n) => {\n  const eleName = t.jsxIdentifier(tag)\n  return t.jsxElement(\n    t.jsxOpeningElement(eleName, attributes),\n    t.jsxClosingElement(eleName),\n    children,\n  )\n}\n\nconst createTagIdAttribute = (tag: tag) =>\n  t.jsxAttribute(\n    t.jsxIdentifier('id'),\n    t.jsxExpressionContainer(t.identifier(`${tag}Id`)),\n  )\n\nconst addTagIdAttribute = (\n  tag: tag,\n  attributes: (t.JSXAttribute | t.JSXSpreadAttribute)[],\n) => {\n  const existingId = attributes.find(\n    (attribute) => t.isJSXAttribute(attribute) && attribute.name.name === 'id',\n  ) as t.JSXAttribute | undefined\n\n  if (!existingId) {\n    return [...attributes, createTagIdAttribute(tag)]\n  }\n  existingId.value = t.jsxExpressionContainer(\n    t.isStringLiteral(existingId.value)\n      ? t.logicalExpression('||', t.identifier(`${tag}Id`), existingId.value)\n      : t.identifier(`${tag}Id`),\n  )\n  return attributes\n}\n\nconst plugin = () => ({\n  visitor: {\n    JSXElement(path: NodePath<t.JSXElement>, state: State) {\n      const tag = state.opts.tag || 'title'\n      if (!elements.length) return\n\n      const openingElement = path.get('openingElement')\n      const openingElementName = openingElement.get('name')\n      if (\n        !elements.some((element) =>\n          openingElementName.isJSXIdentifier({ name: element }),\n        )\n      ) {\n        return\n      }\n\n      const getTagElement = (\n        existingTitle?: t.JSXElement,\n      ): t.JSXExpressionContainer => {\n        const tagExpression = t.identifier(tag)\n        if (existingTitle) {\n          existingTitle.openingElement.attributes = addTagIdAttribute(\n            tag,\n            existingTitle.openingElement.attributes,\n          )\n        }\n        const conditionalTitle = t.conditionalExpression(\n          tagExpression,\n          createTagElement(\n            tag,\n            [t.jsxExpressionContainer(tagExpression)],\n            existingTitle\n              ? existingTitle.openingElement.attributes\n              : [createTagIdAttribute(tag)],\n          ),\n          t.nullLiteral(),\n        )\n        if (existingTitle?.children?.length) {\n          // If title already exists render as follows\n          // `{title === undefined ? fallbackTitleElement : titleElement}`\n          return t.jsxExpressionContainer(\n            t.conditionalExpression(\n              t.binaryExpression(\n                '===',\n                tagExpression,\n                t.identifier('undefined'),\n              ),\n              existingTitle,\n              conditionalTitle,\n            ),\n          )\n        }\n        return t.jsxExpressionContainer(conditionalTitle)\n      }\n\n      // store the title element\n      let tagElement: t.JSXExpressionContainer | null = null\n\n      const hasTitle = path.get('children').some((childPath) => {\n        if (childPath.node === tagElement) return false\n        if (!childPath.isJSXElement()) return false\n        const name = childPath.get('openingElement').get('name')\n        if (!name.isJSXIdentifier()) return false\n        if (name.node.name !== tag) return false\n        tagElement = getTagElement(childPath.node)\n        childPath.replaceWith(tagElement)\n        return true\n      })\n\n      // create a title element if not already create\n      tagElement = tagElement || getTagElement()\n      if (!hasTitle) {\n        // path.unshiftContainer is not working well :(\n        // path.unshiftContainer('children', titleElement)\n        path.node.children.unshift(tagElement)\n        path.replaceWith(path.node)\n      }\n    },\n  },\n})\n\nexport default plugin\n"],"names":["t"],"mappings":";;;;AAGA,MAAM,QAAA,GAAW,CAAC,KAAA,EAAO,KAAK,CAAA,CAAA;AAY9B,MAAM,gBAAA,GAAmB,CACvB,GACA,EAAA,QAAA,GAAuC,EACvC,EAAA,UAAA,GAAwD,EACrD,KAAA;AACH,EAAM,MAAA,OAAA,GAAUA,UAAE,CAAA,aAAA,CAAc,GAAG,CAAA,CAAA;AACnC,EAAA,OAAOA,UAAE,CAAA,UAAA;AAAA,IACPA,UAAA,CAAE,iBAAkB,CAAA,OAAA,EAAS,UAAU,CAAA;AAAA,IACvCA,UAAA,CAAE,kBAAk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