{"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\ninterface State {\n  replacedComponents: Set<string>\n  unsupportedComponents: Set<string>\n}\n\nconst elementToComponent: { [key: string]: string } = {\n  svg: 'Svg',\n  circle: 'Circle',\n  clipPath: 'ClipPath',\n  ellipse: 'Ellipse',\n  g: 'G',\n  linearGradient: 'LinearGradient',\n  radialGradient: 'RadialGradient',\n  line: 'Line',\n  path: 'Path',\n  pattern: 'Pattern',\n  polygon: 'Polygon',\n  polyline: 'Polyline',\n  rect: 'Rect',\n  symbol: 'Symbol',\n  text: 'Text',\n  textPath: 'TextPath',\n  tspan: 'TSpan',\n  use: 'Use',\n  defs: 'Defs',\n  stop: 'Stop',\n  mask: 'Mask',\n  image: 'Image',\n  foreignObject: 'ForeignObject',\n}\n\nconst plugin = () => {\n  function replaceElement(path: NodePath<t.JSXElement>, state: State) {\n    const namePath = path.get('openingElement').get('name')\n    if (!namePath.isJSXIdentifier()) return\n    const { name } = namePath.node\n\n    // Replace element by react-native-svg components\n    const component = elementToComponent[name]\n\n    if (component) {\n      namePath.replaceWith(t.jsxIdentifier(component))\n      if (path.has('closingElement')) {\n        const closingNamePath = path\n          .get('closingElement')\n          .get('name') as NodePath<t.JSXIdentifier>\n        closingNamePath.replaceWith(t.jsxIdentifier(component))\n      }\n      state.replacedComponents.add(component)\n      return\n    }\n\n    // Remove element if not supported\n    state.unsupportedComponents.add(name)\n    path.remove()\n  }\n\n  const svgElementVisitor = {\n    JSXElement(path: NodePath<t.JSXElement>, state: State) {\n      if (\n        !path.get('openingElement').get('name').isJSXIdentifier({ name: 'svg' })\n      ) {\n        return\n      }\n\n      replaceElement(path, state)\n      path.traverse(jsxElementVisitor, state)\n    },\n  }\n\n  const jsxElementVisitor = {\n    JSXElement(path: NodePath<t.JSXElement>, state: State) {\n      replaceElement(path, state)\n    },\n  }\n\n  const importDeclarationVisitor = {\n    ImportDeclaration(path: NodePath<t.ImportDeclaration>, state: State) {\n      if (\n        path.get('source').isStringLiteral({ value: 'react-native-svg' }) &&\n        !path.get('importKind').hasNode()\n      ) {\n        state.replacedComponents.forEach((component) => {\n          if (\n            path\n              .get('specifiers')\n              .some((specifier) =>\n                specifier.get('local').isIdentifier({ name: component }),\n              )\n          ) {\n            return\n          }\n\n          path.pushContainer(\n            'specifiers',\n            t.importSpecifier(t.identifier(component), t.identifier(component)),\n          )\n        })\n      } else if (path.get('source').isStringLiteral({ value: 'expo' })) {\n        path.pushContainer(\n          'specifiers',\n          t.importSpecifier(t.identifier('Svg'), t.identifier('Svg')),\n        )\n      } else {\n        return\n      }\n\n      if (state.unsupportedComponents.size && !path.has('trailingComments')) {\n        const componentList = [...state.unsupportedComponents].join(', ')\n        path.addComment(\n          'trailing',\n          ` SVGR has dropped some elements not supported by react-native-svg: ${componentList} `,\n        )\n      }\n    },\n  }\n\n  return {\n    visitor: {\n      Program(path: NodePath<t.Program>, state: Partial<State>) {\n        state.replacedComponents = new Set()\n        state.unsupportedComponents = new Set()\n\n        path.traverse(svgElementVisitor, state as State)\n        path.traverse(importDeclarationVisitor, state as State)\n      },\n    },\n  }\n}\n\nexport default plugin\n"],"names":["t"],"mappings":";;;;AAQA,MAAM,kBAAgD,GAAA;AAAA,EACpD,GAAK,EAAA,KAAA;AAAA,EACL,MAAQ,EAAA,QAAA;AAAA,EACR,QAAU,EAAA,UAAA;AAAA,EACV,OAAS,EAAA,SAAA;AAAA,EACT,CAAG,EAAA,GAAA;AAAA,EACH,cAAgB,EAAA,gBAAA;AAAA,EAChB,cAAgB,EAAA,gBAAA;AAAA,EAChB,IAAM,EAAA,MAAA;AAAA,EACN,IAAM,EAAA,MAAA;AAAA,EACN,OAAS,EAAA,SAAA;AAAA,EACT,OAAS,EAAA,SAAA;AAAA,EACT,QAAU,EAAA,UAAA;AAAA,EACV,IAAM,EAAA,MAAA;AAAA,EACN,MAAQ,EAAA,QAAA;AAAA,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