From 804d3d53628fcb149141488d819c12afbd6f0416 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Tue, 12 Mar 2024 16:58:04 +0100 Subject: [PATCH] Fix polyfilling of `X.prototype` (#207) --- .../src/visitors/usage.ts | 16 ++++++++++++---- .../usage-pure/symbol-prototype/input.js | 1 + .../usage-pure/symbol-prototype/options.json | 10 ++++++++++ .../usage-pure/symbol-prototype/output.js | 2 ++ .../usage-pure/symbol-prototype/input.js | 1 + .../usage-pure/symbol-prototype/options.json | 10 ++++++++++ .../usage-pure/symbol-prototype/output.js | 2 ++ 7 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 packages/babel-plugin-polyfill-corejs2/test/fixtures/usage-pure/symbol-prototype/input.js create mode 100644 packages/babel-plugin-polyfill-corejs2/test/fixtures/usage-pure/symbol-prototype/options.json create mode 100644 packages/babel-plugin-polyfill-corejs2/test/fixtures/usage-pure/symbol-prototype/output.js create mode 100644 packages/babel-plugin-polyfill-corejs3/test/fixtures/usage-pure/symbol-prototype/input.js create mode 100644 packages/babel-plugin-polyfill-corejs3/test/fixtures/usage-pure/symbol-prototype/options.json create mode 100644 packages/babel-plugin-polyfill-corejs3/test/fixtures/usage-pure/symbol-prototype/output.js diff --git a/packages/babel-helper-define-polyfill-provider/src/visitors/usage.ts b/packages/babel-helper-define-polyfill-provider/src/visitors/usage.ts index e711622c..4e5d3a89 100644 --- a/packages/babel-helper-define-polyfill-provider/src/visitors/usage.ts +++ b/packages/babel-helper-define-polyfill-provider/src/visitors/usage.ts @@ -30,19 +30,27 @@ export default (callProvider: CallProvider) => { callProvider({ kind: "global", name }, path); } + function analyzeMemberExpression(path: NodePath) { + const key = resolveKey(path.get("property"), path.node.computed); + return { key, handleAsMemberExpression: !!key && key !== "prototype" }; + } + return { // Symbol(), new Promise ReferencedIdentifier(path: NodePath) { - if (path.parentPath.isMemberExpression({ object: path.node })) { - // Handled in the MemberExpression visitor + const { parentPath } = path; + if ( + parentPath.isMemberExpression({ object: path.node }) && + analyzeMemberExpression(parentPath).handleAsMemberExpression + ) { return; } handleReferencedIdentifier(path); }, MemberExpression(path: NodePath) { - const key = resolveKey(path.get("property"), path.node.computed); - if (!key || key === "prototype") return; + const { key, handleAsMemberExpression } = analyzeMemberExpression(path); + if (!handleAsMemberExpression) return; const object = path.get("object"); let objectIsGlobalIdentifier = object.isIdentifier(); diff --git a/packages/babel-plugin-polyfill-corejs2/test/fixtures/usage-pure/symbol-prototype/input.js b/packages/babel-plugin-polyfill-corejs2/test/fixtures/usage-pure/symbol-prototype/input.js new file mode 100644 index 00000000..86622cf6 --- /dev/null +++ b/packages/babel-plugin-polyfill-corejs2/test/fixtures/usage-pure/symbol-prototype/input.js @@ -0,0 +1 @@ +Symbol.prototype; diff --git a/packages/babel-plugin-polyfill-corejs2/test/fixtures/usage-pure/symbol-prototype/options.json b/packages/babel-plugin-polyfill-corejs2/test/fixtures/usage-pure/symbol-prototype/options.json new file mode 100644 index 00000000..ab5e1c0d --- /dev/null +++ b/packages/babel-plugin-polyfill-corejs2/test/fixtures/usage-pure/symbol-prototype/options.json @@ -0,0 +1,10 @@ +{ + "plugins": [ + [ + "@@/polyfill-corejs2", + { + "method": "usage-pure" + } + ] + ] +} diff --git a/packages/babel-plugin-polyfill-corejs2/test/fixtures/usage-pure/symbol-prototype/output.js b/packages/babel-plugin-polyfill-corejs2/test/fixtures/usage-pure/symbol-prototype/output.js new file mode 100644 index 00000000..ff60e84a --- /dev/null +++ b/packages/babel-plugin-polyfill-corejs2/test/fixtures/usage-pure/symbol-prototype/output.js @@ -0,0 +1,2 @@ +var _Symbol = require("core-js/library/fn/symbol/index.js"); +_Symbol.prototype; diff --git a/packages/babel-plugin-polyfill-corejs3/test/fixtures/usage-pure/symbol-prototype/input.js b/packages/babel-plugin-polyfill-corejs3/test/fixtures/usage-pure/symbol-prototype/input.js new file mode 100644 index 00000000..86622cf6 --- /dev/null +++ b/packages/babel-plugin-polyfill-corejs3/test/fixtures/usage-pure/symbol-prototype/input.js @@ -0,0 +1 @@ +Symbol.prototype; diff --git a/packages/babel-plugin-polyfill-corejs3/test/fixtures/usage-pure/symbol-prototype/options.json b/packages/babel-plugin-polyfill-corejs3/test/fixtures/usage-pure/symbol-prototype/options.json new file mode 100644 index 00000000..75fbdc27 --- /dev/null +++ b/packages/babel-plugin-polyfill-corejs3/test/fixtures/usage-pure/symbol-prototype/options.json @@ -0,0 +1,10 @@ +{ + "plugins": [ + [ + "@@/polyfill-corejs3", + { + "method": "usage-pure" + } + ] + ] +} \ No newline at end of file diff --git a/packages/babel-plugin-polyfill-corejs3/test/fixtures/usage-pure/symbol-prototype/output.js b/packages/babel-plugin-polyfill-corejs3/test/fixtures/usage-pure/symbol-prototype/output.js new file mode 100644 index 00000000..67ae890b --- /dev/null +++ b/packages/babel-plugin-polyfill-corejs3/test/fixtures/usage-pure/symbol-prototype/output.js @@ -0,0 +1,2 @@ +var _Symbol = require("core-js-pure/stable/symbol/index.js"); +_Symbol.prototype;