1 | {"version":3,"names":["_helperPluginUtils","require","_core","_default","exports","default","declare","api","assertVersion","name","visitor","MetaProperty","path","meta","get","property","scope","isIdentifier","func","findParent","isClass","isFunction","isArrowFunctionExpression","isClassMethod","kind","buildCodeFrameError","node","t","isMethod","replaceWith","buildUndefinedNode","constructor","memberExpression","thisExpression","identifier","id","generateUidIdentifier","parentPath","hasOwnBinding","bindingIdentifierEquals","rename","parent","conditionalExpression","binaryExpression","cloneNode"],"sources":["../src/index.ts"],"sourcesContent":["import { declare } from \"@babel/helper-plugin-utils\";\nimport { types as t } from \"@babel/core\";\nimport type { NodePath } from \"@babel/traverse\";\n\nexport default declare(api => {\n api.assertVersion(\n process.env.BABEL_8_BREAKING && process.env.IS_PUBLISH\n ? PACKAGE_JSON.version\n : 7,\n );\n\n return {\n name: \"transform-new-target\",\n\n visitor: {\n MetaProperty(path) {\n const meta = path.get(\"meta\");\n const property = path.get(\"property\");\n const { scope } = path;\n\n if (\n meta.isIdentifier({ name: \"new\" }) &&\n property.isIdentifier({ name: \"target\" })\n ) {\n const func = path.findParent(path => {\n if (path.isClass()) return true;\n if (path.isFunction() && !path.isArrowFunctionExpression()) {\n if (path.isClassMethod({ kind: \"constructor\" })) {\n return false;\n }\n\n return true;\n }\n return false;\n }) as NodePath<\n | t.FunctionDeclaration\n | t.FunctionExpression\n | t.Class\n | t.ClassMethod\n | t.ClassPrivateMethod\n >;\n\n if (!func) {\n throw path.buildCodeFrameError(\n \"new.target must be under a (non-arrow) function or a class.\",\n );\n }\n\n const { node } = func;\n if (t.isMethod(node)) {\n path.replaceWith(scope.buildUndefinedNode());\n return;\n }\n\n const constructor = t.memberExpression(\n t.thisExpression(),\n t.identifier(\"constructor\"),\n );\n\n if (func.isClass()) {\n path.replaceWith(constructor);\n return;\n }\n\n if (!node.id) {\n node.id = scope.generateUidIdentifier(\"target\");\n } else {\n // packages/babel-helper-create-class-features-plugin/src/fields.ts#L192 unshadow\n let scope = path.scope;\n const name = node.id.name;\n while (scope !== func.parentPath.scope) {\n if (\n scope.hasOwnBinding(name) &&\n !scope.bindingIdentifierEquals(name, node.id)\n ) {\n scope.rename(name);\n }\n scope = scope.parent;\n }\n }\n\n path.replaceWith(\n t.conditionalExpression(\n t.binaryExpression(\n \"instanceof\",\n t.thisExpression(),\n t.cloneNode(node.id),\n ),\n constructor,\n scope.buildUndefinedNode(),\n ),\n );\n }\n },\n },\n };\n});\n"],"mappings":";;;;;;AAAA,IAAAA,kBAAA,GAAAC,OAAA;AACA,IAAAC,KAAA,GAAAD,OAAA;AAAyC,IAAAE,QAAA,GAAAC,OAAA,CAAAC,OAAA,GAG1B,IAAAC,0BAAO,EAACC,GAAG,IAAI;EAC5BA,GAAG,CAACC,aAAa,CAGX,CACN,CAAC;EAED,OAAO;IACLC,IAAI,EAAE,sBAAsB;IAE5BC,OAAO,EAAE;MACPC,YAAYA,CAACC,IAAI,EAAE;QACjB,MAAMC,IAAI,GAAGD,IAAI,CAACE,GAAG,CAAC,MAAM,CAAC;QAC7B,MAAMC,QAAQ,GAAGH,IAAI,CAACE,GAAG,CAAC,UAAU,CAAC;QACrC,MAAM;UAAEE;QAAM,CAAC,GAAGJ,IAAI;QAEtB,IACEC,IAAI,CAACI,YAAY,CAAC;UAAER,IAAI,EAAE;QAAM,CAAC,CAAC,IAClCM,QAAQ,CAACE,YAAY,CAAC;UAAER,IAAI,EAAE;QAAS,CAAC,CAAC,EACzC;UACA,MAAMS,IAAI,GAAGN,IAAI,CAACO,UAAU,CAACP,IAAI,IAAI;YACnC,IAAIA,IAAI,CAACQ,OAAO,CAAC,CAAC,EAAE,OAAO,IAAI;YAC/B,IAAIR,IAAI,CAACS,UAAU,CAAC,CAAC,IAAI,CAACT,IAAI,CAACU,yBAAyB,CAAC,CAAC,EAAE;cAC1D,IAAIV,IAAI,CAACW,aAAa,CAAC;gBAAEC,IAAI,EAAE;cAAc,CAAC,CAAC,EAAE;gBAC/C,OAAO,KAAK;cACd;cAEA,OAAO,IAAI;YACb;YACA,O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