{"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/explicit-module-boundary-types */\nimport { ConfigAPI, types as t, NodePath, template } from '@babel/core'\n\nexport interface Attribute {\n  name: string\n  value?: boolean | number | string | null\n  spread?: boolean\n  literal?: boolean\n  position?: 'start' | 'end'\n}\n\nexport interface Options {\n  elements: string[]\n  attributes: Attribute[]\n}\n\nconst positionMethod = {\n  start: 'unshiftContainer',\n  end: 'pushContainer',\n} as const\n\nconst addJSXAttribute = (_: ConfigAPI, opts: Options) => {\n  function getAttributeValue({\n    literal,\n    value,\n  }: {\n    literal?: Attribute['literal']\n    value: Attribute['value']\n  }) {\n    if (typeof value === 'boolean') {\n      return t.jsxExpressionContainer(t.booleanLiteral(value))\n    }\n\n    if (typeof value === 'number') {\n      return t.jsxExpressionContainer(t.numericLiteral(value))\n    }\n\n    if (typeof value === 'string' && literal) {\n      return t.jsxExpressionContainer(\n        (template.ast(value) as t.ExpressionStatement).expression,\n      )\n    }\n\n    if (typeof value === 'string') {\n      return t.stringLiteral(value)\n    }\n\n    return null\n  }\n\n  function getAttribute({ spread, name, value, literal }: Attribute) {\n    if (spread) {\n      return t.jsxSpreadAttribute(t.identifier(name))\n    }\n\n    return t.jsxAttribute(\n      t.jsxIdentifier(name),\n      getAttributeValue({ value, literal }),\n    )\n  }\n\n  return {\n    visitor: {\n      JSXOpeningElement(path: NodePath<t.JSXOpeningElement>) {\n        if (!t.isJSXIdentifier(path.node.name)) return\n        if (!opts.elements.includes(path.node.name.name)) return\n\n        opts.attributes.forEach(\n          ({\n            name,\n            value = null,\n            spread = false,\n            literal = false,\n            position = 'end',\n          }) => {\n            const method = positionMethod[position]\n            const newAttribute = getAttribute({ spread, name, value, literal })\n            const attributes = path.get('attributes')\n\n            const isEqualAttribute = (\n              attribute: NodePath<t.JSXSpreadAttribute | t.JSXAttribute>,\n            ) => {\n              if (spread)\n                return (\n                  attribute.isJSXSpreadAttribute() &&\n                  attribute.get('argument').isIdentifier({ name })\n                )\n              return (\n                attribute.isJSXAttribute() &&\n                attribute.get('name').isJSXIdentifier({ name })\n              )\n            }\n\n            const replaced = attributes.some((attribute) => {\n              if (!isEqualAttribute(attribute)) return false\n              attribute.replaceWith(newAttribute)\n              return true\n            })\n\n            if (!replaced) {\n              path[method]('attributes', newAttribute)\n            }\n          },\n        )\n      },\n    },\n  }\n}\n\nexport default addJSXAttribute\n"],"names":["t","template"],"mappings":";;;;AAgBA,MAAM,cAAiB,GAAA;AAAA,EACrB,KAAO,EAAA,kBAAA;AAAA,EACP,GAAK,EAAA,eAAA;AACP,CAAA,CAAA;AAEM,MAAA,eAAA,GAAkB,CAAC,CAAA,EAAc,IAAkB,KAAA;AACvD,EAAA,SAAS,iBAAkB,CAAA;AAAA,IACzB,OAAA;AAAA,IACA,KAAA;AAAA,GAIC,EAAA;AACD,IAAI,IAAA,OAAO,UAAU,SAAW,EAAA;AAC9B,MAAA,OAAOA,UAAE,CAAA,sBAAA,CAAuBA,UAAE,CAAA,cAAA,CAAe,KAAK,CAAC,CAAA,CAAA;AAAA,KACzD;AAEA,IAAI,IAAA,OAAO,UAAU,QAAU,EAAA;AAC7B,MAAA,OAAOA,UAAE,CAAA,sBAAA,CAAuBA,UAAE,CAAA,cAAA,CAAe,KAAK,CAAC,CAAA,CAAA;AAAA,KACzD;AAEA,IAAI,IAAA,OAAO,KAAU,KAAA,QAAA,IAAY,OAAS,EAAA;AACxC,MAAA,OAAOA,UAAE,CAAA,sBAAA;AAAA,QACNC,aAAA,CAAS,GAAI,CAAA,KAAK,CAA4B,CAAA,UAAA;AAAA,OACjD,CAAA;AAAA,KACF;AAEA,IAAI,IAAA,OAAO,UAAU,QAAU,EAAA;AAC7B,MAAO,OAAAD,UAAA,CAAE,cAAc,KAAK,CAAA,CAAA;AAAA,KAC9B;AAEA,IAAO,OAAA,IAAA,CAAA;AAAA,GACT;AAEA,EAAA,SAAS,aAAa,EAAE,MAAA,EAAQ,IAAM,EAAA,KAAA,E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