From e096710c86e1c0f5a2f0f4d2b6b0b5d4a62bd51a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Thu, 23 Jan 2020 15:23:02 +0100 Subject: [PATCH 1/2] Avoid compiling the typeof helper with itself --- packages/babel-helpers/src/helpers.js | 2 + .../package.json | 6 ++- .../src/index.js | 17 +++++- .../test/helper.spec.js | 52 +++++++++++++++++++ 4 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 packages/babel-plugin-transform-typeof-symbol/test/helper.spec.js diff --git a/packages/babel-helpers/src/helpers.js b/packages/babel-helpers/src/helpers.js index 9212dfb2c28c..90a0bca64ab5 100644 --- a/packages/babel-helpers/src/helpers.js +++ b/packages/babel-helpers/src/helpers.js @@ -12,6 +12,8 @@ const helper = (minVersion: string) => tpl => ({ helpers.typeof = helper("7.0.0-beta.0")` export default function _typeof(obj) { + "@babel/helpers - typeof"; + if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function (obj) { return typeof obj; }; } else { diff --git a/packages/babel-plugin-transform-typeof-symbol/package.json b/packages/babel-plugin-transform-typeof-symbol/package.json index f89e3c893775..59f2b5721cb4 100644 --- a/packages/babel-plugin-transform-typeof-symbol/package.json +++ b/packages/babel-plugin-transform-typeof-symbol/package.json @@ -19,6 +19,10 @@ }, "devDependencies": { "@babel/core": "^7.8.3", - "@babel/helper-plugin-test-runner": "^7.8.3" + "@babel/helper-plugin-test-runner": "^7.8.3", + "@babel/runtime": "^7.8.3", + "@babel/runtime-corejs2": "^7.8.3", + "@babel/runtime-corejs3": "^7.8.3", + "resolve": "^1.15.0" } } diff --git a/packages/babel-plugin-transform-typeof-symbol/src/index.js b/packages/babel-plugin-transform-typeof-symbol/src/index.js index b969f216d7c8..1faea61b4b55 100644 --- a/packages/babel-plugin-transform-typeof-symbol/src/index.js +++ b/packages/babel-plugin-transform-typeof-symbol/src/index.js @@ -36,8 +36,23 @@ export default declare(api => { } } + let isUnderHelper = path.findParent(path => { + if (path.isFunction()) { + return ( + path.get("body.directives.0")?.node.value.value === + "@babel/helpers - typeof" + ); + } + }); + + if (isUnderHelper) return; + const helper = this.addHelper("typeof"); - const isUnderHelper = path.findParent(path => { + + // TODO: This is needed for backward compatibility with + // @babel/helpers <= 7.8.3. + // Remove in Babel 8 + isUnderHelper = path.findParent(path => { return ( (path.isVariableDeclarator() && path.node.id === helper) || (path.isFunctionDeclaration() && diff --git a/packages/babel-plugin-transform-typeof-symbol/test/helper.spec.js b/packages/babel-plugin-transform-typeof-symbol/test/helper.spec.js new file mode 100644 index 000000000000..9a89884a3c5f --- /dev/null +++ b/packages/babel-plugin-transform-typeof-symbol/test/helper.spec.js @@ -0,0 +1,52 @@ +import * as babel from "@babel/core"; +import resolvePath from "resolve"; +import fs from "fs"; + +import transformTypeofSymbol from ".."; + +const resolve = path => + new Promise((resolve, reject) => + resolvePath(path, (err, path) => (err ? reject(err) : resolve(path))), + ); +const readFile = path => + new Promise((resolve, reject) => + fs.readFile(path, "utf8", (err, contents) => { + if (err) reject(err); + else resolve(contents); + }), + ); + +describe("@babel/plugin-transform-typeof-symbol", () => { + test.each` + runtime | type + ${"@babel/runtime"} | ${"esm"} + ${"@babel/runtime"} | ${"cjs"} + ${"@babel/runtime-corejs2"} | ${"esm"} + ${"@babel/runtime-corejs2"} | ${"cjs"} + ${"@babel/runtime-corejs3"} | ${"esm"} + ${"@babel/runtime-corejs3"} | ${"cjs"} + `( + "shouldn't transpile the $type $runtime helper", + async ({ type, runtime }) => { + const path = await resolve( + `${runtime}/helpers${type === "esm" ? "/esm/" : "/"}typeof`, + ); + const src = await readFile(path); + + const ast = babel.parseSync(src, { + configFile: false, + sourceType: type === "esm" ? "module" : "script", + }); + + const withPlugin = babel.transformFromAstSync(ast, src, { + configFile: false, + plugins: [transformTypeofSymbol], + }); + const withoutPlugin = babel.transformFromAstSync(ast, src, { + configFile: false, + }); + + expect(withPlugin.code).toBe(withoutPlugin.code); + }, + ); +}); From 27b1fede06e7b95d1db6528f7ead4ae9b0743586 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Thu, 23 Jan 2020 15:24:39 +0100 Subject: [PATCH 2/2] Update fixtures --- .../test/fixtures/regression/6154/output.js | 2 +- .../test/fixtures/legacy-regression/7030/output.js | 2 +- .../test/fixtures/regression/T7537/output.js | 2 +- .../test/fixtures/regression/6057-expanded/output.js | 2 +- .../test/fixtures/symbols/builtin-global/output.mjs | 2 +- .../test/fixtures/symbols/default-export/output.mjs | 2 +- .../test/fixtures/symbols/shadow/output.js | 2 +- .../auto-esm-unsupported-import-unsupported/output.js | 2 +- .../test/fixtures/dynamic-import/modules-amd/output.js | 2 +- .../test/fixtures/dynamic-import/modules-cjs/output.js | 2 +- .../test/fixtures/plugins-integration/issue-10662/output.js | 2 +- .../test/fixtures/plugins-integration/issue-7527/output.js | 2 +- .../preset-options/loose-with-typeof-symbol-includes/output.js | 2 +- 13 files changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/regression/6154/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/regression/6154/output.js index 6c8d4cd1a2f5..6722f24945c4 100644 --- a/packages/babel-plugin-proposal-class-properties/test/fixtures/regression/6154/output.js +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/regression/6154/output.js @@ -1,4 +1,4 @@ -function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } +function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/legacy-regression/7030/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/legacy-regression/7030/output.js index 3ecd37c2f8d2..7f0fec1864c4 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/legacy-regression/7030/output.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/legacy-regression/7030/output.js @@ -1,4 +1,4 @@ -function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } +function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } diff --git a/packages/babel-plugin-transform-classes/test/fixtures/regression/T7537/output.js b/packages/babel-plugin-transform-classes/test/fixtures/regression/T7537/output.js index 5e43e6dd208d..15585d8018b9 100644 --- a/packages/babel-plugin-transform-classes/test/fixtures/regression/T7537/output.js +++ b/packages/babel-plugin-transform-classes/test/fixtures/regression/T7537/output.js @@ -1,4 +1,4 @@ -function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } +function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } diff --git a/packages/babel-plugin-transform-parameters/test/fixtures/regression/6057-expanded/output.js b/packages/babel-plugin-transform-parameters/test/fixtures/regression/6057-expanded/output.js index 451673e683d4..b0e3dd12aa14 100644 --- a/packages/babel-plugin-transform-parameters/test/fixtures/regression/6057-expanded/output.js +++ b/packages/babel-plugin-transform-parameters/test/fixtures/regression/6057-expanded/output.js @@ -9,7 +9,7 @@ var _args = _interopRequireDefault(require("utils/url/args")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } -function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } +function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } diff --git a/packages/babel-plugin-transform-typeof-symbol/test/fixtures/symbols/builtin-global/output.mjs b/packages/babel-plugin-transform-typeof-symbol/test/fixtures/symbols/builtin-global/output.mjs index 8b1124c31ba1..35e7d5e1f7bf 100644 --- a/packages/babel-plugin-transform-typeof-symbol/test/fixtures/symbols/builtin-global/output.mjs +++ b/packages/babel-plugin-transform-typeof-symbol/test/fixtures/symbols/builtin-global/output.mjs @@ -1,3 +1,3 @@ -function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function (obj) { return typeof obj; }; } else { _typeof = function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } +function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function (obj) { return typeof obj; }; } else { _typeof = function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } (typeof Reflect === "undefined" ? "undefined" : _typeof(Reflect)) === "object"; diff --git a/packages/babel-plugin-transform-typeof-symbol/test/fixtures/symbols/default-export/output.mjs b/packages/babel-plugin-transform-typeof-symbol/test/fixtures/symbols/default-export/output.mjs index fd697ccdc932..5db84a8c4952 100644 --- a/packages/babel-plugin-transform-typeof-symbol/test/fixtures/symbols/default-export/output.mjs +++ b/packages/babel-plugin-transform-typeof-symbol/test/fixtures/symbols/default-export/output.mjs @@ -1,4 +1,4 @@ -function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function (obj) { return typeof obj; }; } else { _typeof = function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } +function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function (obj) { return typeof obj; }; } else { _typeof = function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } export default function () { _typeof({}) === "object"; diff --git a/packages/babel-plugin-transform-typeof-symbol/test/fixtures/symbols/shadow/output.js b/packages/babel-plugin-transform-typeof-symbol/test/fixtures/symbols/shadow/output.js index fc5cee7b35ab..05b5e743592f 100644 --- a/packages/babel-plugin-transform-typeof-symbol/test/fixtures/symbols/shadow/output.js +++ b/packages/babel-plugin-transform-typeof-symbol/test/fixtures/symbols/shadow/output.js @@ -1,4 +1,4 @@ -function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function (obj) { return typeof obj; }; } else { _typeof = function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } +function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function (obj) { return typeof obj; }; } else { _typeof = function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } var _Symbol = foo(); diff --git a/packages/babel-preset-env/test/fixtures/dynamic-import/auto-esm-unsupported-import-unsupported/output.js b/packages/babel-preset-env/test/fixtures/dynamic-import/auto-esm-unsupported-import-unsupported/output.js index a7f2edabb858..1fbdb0891dc5 100644 --- a/packages/babel-preset-env/test/fixtures/dynamic-import/auto-esm-unsupported-import-unsupported/output.js +++ b/packages/babel-preset-env/test/fixtures/dynamic-import/auto-esm-unsupported-import-unsupported/output.js @@ -1,6 +1,6 @@ "use strict"; -function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } +function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } diff --git a/packages/babel-preset-env/test/fixtures/dynamic-import/modules-amd/output.js b/packages/babel-preset-env/test/fixtures/dynamic-import/modules-amd/output.js index f1ca949bbcb1..3625cc4157ca 100644 --- a/packages/babel-preset-env/test/fixtures/dynamic-import/modules-amd/output.js +++ b/packages/babel-preset-env/test/fixtures/dynamic-import/modules-amd/output.js @@ -1,5 +1,5 @@ define(["require"], function (_require) { - function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } + function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } diff --git a/packages/babel-preset-env/test/fixtures/dynamic-import/modules-cjs/output.js b/packages/babel-preset-env/test/fixtures/dynamic-import/modules-cjs/output.js index a7f2edabb858..1fbdb0891dc5 100644 --- a/packages/babel-preset-env/test/fixtures/dynamic-import/modules-cjs/output.js +++ b/packages/babel-preset-env/test/fixtures/dynamic-import/modules-cjs/output.js @@ -1,6 +1,6 @@ "use strict"; -function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } +function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } diff --git a/packages/babel-preset-env/test/fixtures/plugins-integration/issue-10662/output.js b/packages/babel-preset-env/test/fixtures/plugins-integration/issue-10662/output.js index e13f6d324d73..8f9250faa0a2 100644 --- a/packages/babel-preset-env/test/fixtures/plugins-integration/issue-10662/output.js +++ b/packages/babel-preset-env/test/fixtures/plugins-integration/issue-10662/output.js @@ -13,7 +13,7 @@ })(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function () { "use strict"; - function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } + function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } var globalThis = {}; diff --git a/packages/babel-preset-env/test/fixtures/plugins-integration/issue-7527/output.js b/packages/babel-preset-env/test/fixtures/plugins-integration/issue-7527/output.js index d0111f1a27ab..e5987724ed71 100644 --- a/packages/babel-preset-env/test/fixtures/plugins-integration/issue-7527/output.js +++ b/packages/babel-preset-env/test/fixtures/plugins-integration/issue-7527/output.js @@ -1,6 +1,6 @@ "use strict"; -function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } +function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } diff --git a/packages/babel-preset-env/test/fixtures/preset-options/loose-with-typeof-symbol-includes/output.js b/packages/babel-preset-env/test/fixtures/preset-options/loose-with-typeof-symbol-includes/output.js index 47a605e27410..80d9f2721b48 100644 --- a/packages/babel-preset-env/test/fixtures/preset-options/loose-with-typeof-symbol-includes/output.js +++ b/packages/babel-preset-env/test/fixtures/preset-options/loose-with-typeof-symbol-includes/output.js @@ -1,5 +1,5 @@ "use strict"; -function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } +function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } _typeof(Symbol());