1 | {"version":3,"names":["_helperPluginUtils","require","_default","exports","default","declare","api","assertVersion","types","t","template","build","left","right","callExpression","memberExpression","identifier","maybeMemoize","node","scope","isStatic","assign","ref","cloneNode","path","isPattern","id","generateUidIdentifierBasedOnNode","push","assignmentExpression","name","visitor","AssignmentExpression","operator","isMemberExpression","member1","member2","object","replaceWith","expression","ast","property","computed","prop","BinaryExpression"],"sources":["../src/index.ts"],"sourcesContent":["import { declare } from \"@babel/helper-plugin-utils\";\nimport type { types as t, Scope } from \"@babel/core\";\n\nexport default declare(api => {\n api.assertVersion(REQUIRED_VERSION(7));\n\n const { types: t, template } = api;\n\n function build(left: t.Expression, right: t.Expression) {\n return t.callExpression(\n t.memberExpression(t.identifier(\"Math\"), t.identifier(\"pow\")),\n [left, right],\n );\n }\n\n function maybeMemoize<T extends t.Expression | t.Super>(\n node: T,\n scope: Scope,\n ) {\n if (scope.isStatic(node)) {\n return { assign: node, ref: t.cloneNode(node) };\n }\n\n if (scope.path.isPattern()) {\n // We cannot inject a temp var in function arguments.\n return null;\n }\n\n const id = scope.generateUidIdentifierBasedOnNode(node);\n scope.push({ id });\n return {\n assign: t.assignmentExpression(\n \"=\",\n t.cloneNode(id),\n // This is not t.Super, because otherwise the .isStatic check above\n // would have returned true.\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion\n node as t.Expression,\n ),\n ref: t.cloneNode(id),\n };\n }\n\n return {\n name: \"transform-exponentiation-operator\",\n\n visitor: {\n AssignmentExpression(path) {\n const { node, scope } = path;\n if (node.operator !== \"**=\") return;\n\n if (t.isMemberExpression(node.left)) {\n let member1: t.Expression;\n let member2: t.Expression;\n\n const object = maybeMemoize(node.left.object, scope);\n if (!object) {\n // We need to inject a temp var, but we are in function parameters\n // and thus cannot. Wrap the expression in an IIFE. It will be\n // eventually requeued and transformed.\n path.replaceWith(template.expression.ast`(() => ${path.node})()`);\n return;\n }\n\n const { property, computed } = node.left;\n\n if (computed) {\n const prop = maybeMemoize(property as t.Expression, scope);\n member1 = t.memberExpression(object.assign, prop.assign, true);\n member2 = t.memberExpression(object.ref, prop.ref, true);\n } else {\n member1 = t.memberExpression(object.assign, property, false);\n member2 = t.memberExpression(\n object.ref,\n t.cloneNode(property),\n false,\n );\n }\n\n path.replaceWith(\n t.assignmentExpression(\"=\", member1, build(member2, node.right)),\n );\n } else {\n path.replaceWith(\n t.assignmentExpression(\n \"=\",\n node.left,\n build(\n // todo: it could be a t.AsExpression\n t.cloneNode(node.left) as t.Identifier,\n node.right,\n ),\n ),\n );\n }\n },\n\n BinaryExpression(path) {\n const { node } = path;\n if (node.operator === \"**\") {\n path.replaceWith(\n build(\n // left can be PrivateName only if operator is `\"in\"`\n node.left as t.Expression,\n node.right,\n ),\n );\n }\n },\n },\n };\n});\n"],"mappings":";;;;;;AAAA,IAAAA,kBAAA,GAAAC,OAAA;AAAqD,IAAAC,QAAA,GAAAC,OAAA,CAAAC,OAAA,GAGtC,IAAAC,0BAAO,EAACC,GAAG,IAAI;EAC5BA,GAAG,CAACC,aAAa,uCAAoB,CAAC;EAEtC,MAAM;IAAEC,KAAK,EAAEC,CAAC;IAAEC;EAAS,CAAC,GAAGJ,GAAG;EAElC,SAASK,KAAKA,CAACC,IAAkB,EAAEC,KAAmB,EAAE;IACtD,OAAOJ,CAAC,CAACK,cAAc,CACrBL,CAAC,CAACM,gBAAgB,CAACN,CAAC,CAACO,UAAU,CAAC,MAAM,CAAC,EAAEP,CAAC,CAACO,UAAU,CAAC,KAAK,CAAC,CAAC,EAC7D,CAACJ,IAAI,EAAEC,KAAK,CACd,CAAC;EACH;EAEA,SAASI,YAAYA,CACnBC,IAAO,EACPC,KAAY,EACZ;IACA,IAAIA,KAAK,CAACC,QAAQ,CAACF,IAAI,CAAC,EAAE;MACxB,OAAO;QAAEG,MAAM,EAAEH,IAAI;QAAEI,GAAG,EAAEb,CAAC,CAACc,SAAS,CAACL,IAAI;MAAE,CAAC;IACjD;IAEA,IAAIC,KAAK,CAACK,IAAI,CAACC,S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