{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/explicit-module-boundary-types */\nimport { types as t, NodePath, ConfigAPI } from '@babel/core'\n\nconst elements = ['svg', 'Svg']\n\nexport interface Options {\n  width: number | string\n  height: number | string\n}\n\nconst getValue = (raw: undefined | number | string) => {\n  if (raw === undefined) return t.stringLiteral('1em')\n  switch (typeof raw) {\n    case 'number':\n      return t.jsxExpressionContainer(t.numericLiteral(raw))\n    case 'string':\n      return t.stringLiteral(raw)\n    default:\n      return t.stringLiteral('1em')\n  }\n}\n\nconst plugin = (_: ConfigAPI, opts: Options) => ({\n  visitor: {\n    JSXOpeningElement(path: NodePath<t.JSXOpeningElement>) {\n      if (\n        !elements.some((element) =>\n          path.get('name').isJSXIdentifier({ name: element }),\n        )\n      )\n        return\n\n      const values = {\n        width: getValue(opts.width),\n        height: getValue(opts.height),\n      }\n      const requiredAttributes = Object.keys(values)\n\n      path.get('attributes').forEach((attributePath) => {\n        if (!attributePath.isJSXAttribute()) return\n        const namePath = attributePath.get('name')\n        if (!namePath.isJSXIdentifier()) return\n        const index = requiredAttributes.indexOf(namePath.node.name)\n\n        if (index === -1) return\n\n        const valuePath = attributePath.get('value')\n        valuePath.replaceWith(values[namePath.node.name as 'width' | 'height'])\n        requiredAttributes.splice(index, 1)\n      })\n\n      path.pushContainer(\n        'attributes',\n        requiredAttributes.map((attr) =>\n          t.jsxAttribute(\n            t.jsxIdentifier(attr),\n            values[attr as 'width' | 'height'],\n          ),\n        ),\n      )\n    },\n  },\n})\n\nexport default plugin\n"],"names":["t"],"mappings":";;;;AAGA,MAAM,QAAA,GAAW,CAAC,KAAA,EAAO,KAAK,CAAA,CAAA;AAO9B,MAAM,QAAA,GAAW,CAAC,GAAqC,KAAA;AACrD,EAAA,IAAI,GAAQ,KAAA,KAAA,CAAA;AAAW,IAAO,OAAAA,UAAA,CAAE,cAAc,KAAK,CAAA,CAAA;AACnD,EAAA,QAAQ,OAAO,GAAK;A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