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