From 595240f071ad358d952bf7a45db1cf8605d9fcfe Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Fri, 24 Aug 2018 15:10:46 -0700 Subject: [PATCH] Add a 'whitelist' option for the external-helpers plugin to mirror the helper builder. (#8531) --- .../src/index.js | 19 ++++++++++++++++++- .../test/fixtures/opts/whitelist/input.js | 3 +++ .../test/fixtures/opts/whitelist/options.json | 8 ++++++++ .../test/fixtures/opts/whitelist/output.js | 17 +++++++++++++++++ .../test/index.js | 3 +++ 5 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 packages/babel-plugin-external-helpers/test/fixtures/opts/whitelist/input.js create mode 100644 packages/babel-plugin-external-helpers/test/fixtures/opts/whitelist/options.json create mode 100644 packages/babel-plugin-external-helpers/test/fixtures/opts/whitelist/output.js create mode 100644 packages/babel-plugin-external-helpers/test/index.js diff --git a/packages/babel-plugin-external-helpers/src/index.js b/packages/babel-plugin-external-helpers/src/index.js index ef08966b2c..089986cd24 100644 --- a/packages/babel-plugin-external-helpers/src/index.js +++ b/packages/babel-plugin-external-helpers/src/index.js @@ -4,7 +4,18 @@ import { types as t } from "@babel/core"; export default declare((api, options) => { api.assertVersion(7); - const { helperVersion = "7.0.0-beta.0" } = options; + const { helperVersion = "7.0.0-beta.0", whitelist = false } = options; + + if ( + whitelist !== false && + (!Array.isArray(whitelist) || whitelist.some(w => typeof w !== "string")) + ) { + throw new Error( + ".whitelist must be undefined, false, or an array of strings", + ); + } + + const helperWhitelist = whitelist ? new Set(whitelist) : null; return { pre(file) { @@ -19,6 +30,12 @@ export default declare((api, options) => { return; } + // babelCore.buildExternalHelpers() allows a whitelist of helpers that + // will be inserted into the external helpers list. That same whitelist + // should be passed into the plugin here in that case, so that we can + // avoid referencing 'babelHelpers.XX' when the helper does not exist. + if (helperWhitelist && !helperWhitelist.has(name)) return; + return t.memberExpression( t.identifier("babelHelpers"), t.identifier(name), diff --git a/packages/babel-plugin-external-helpers/test/fixtures/opts/whitelist/input.js b/packages/babel-plugin-external-helpers/test/fixtures/opts/whitelist/input.js new file mode 100644 index 0000000000..e7d671375f --- /dev/null +++ b/packages/babel-plugin-external-helpers/test/fixtures/opts/whitelist/input.js @@ -0,0 +1,3 @@ +class Foo { + method(){} +} diff --git a/packages/babel-plugin-external-helpers/test/fixtures/opts/whitelist/options.json b/packages/babel-plugin-external-helpers/test/fixtures/opts/whitelist/options.json new file mode 100644 index 0000000000..b47c0d3f87 --- /dev/null +++ b/packages/babel-plugin-external-helpers/test/fixtures/opts/whitelist/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + ["external-helpers", { + "whitelist": ["createClass"] + }], + "transform-classes" + ] +} diff --git a/packages/babel-plugin-external-helpers/test/fixtures/opts/whitelist/output.js b/packages/babel-plugin-external-helpers/test/fixtures/opts/whitelist/output.js new file mode 100644 index 0000000000..34a0d30fb1 --- /dev/null +++ b/packages/babel-plugin-external-helpers/test/fixtures/opts/whitelist/output.js @@ -0,0 +1,17 @@ +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } + +let Foo = +/*#__PURE__*/ +function () { + "use strict"; + + function Foo() { + _classCallCheck(this, Foo); + } + + babelHelpers.createClass(Foo, [{ + key: "method", + value: function method() {} + }]); + return Foo; +}(); diff --git a/packages/babel-plugin-external-helpers/test/index.js b/packages/babel-plugin-external-helpers/test/index.js new file mode 100644 index 0000000000..1b534b8fc6 --- /dev/null +++ b/packages/babel-plugin-external-helpers/test/index.js @@ -0,0 +1,3 @@ +import runner from "@babel/helper-plugin-test-runner"; + +runner(__dirname);