Add a 'whitelist' option for the external-helpers plugin to mirror the helper builder. (#8531)

This commit is contained in:
Logan Smyth 2018-08-24 15:10:46 -07:00 committed by GitHub
parent cada040bec
commit 595240f071
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 49 additions and 1 deletions

View File

@ -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),

View File

@ -0,0 +1,3 @@
class Foo {
method(){}
}

View File

@ -0,0 +1,8 @@
{
"plugins": [
["external-helpers", {
"whitelist": ["createClass"]
}],
"transform-classes"
]
}

View File

@ -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;
}();

View File

@ -0,0 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";
runner(__dirname);