add access to helpers used by the current file and allow a helper whitelist to be passed to buildHelpers - closes #898

This commit is contained in:
Sebastian McKenzie 2015-02-26 08:59:07 +11:00
parent 372c06eb80
commit 0a0931dc2e
3 changed files with 19 additions and 8 deletions

View File

@ -3,7 +3,7 @@ var generator = require("./generation");
var util = require("./util");
var t = require("./types");
module.exports = function () {
module.exports = function (whitelist) {
var namespace = t.identifier("babelHelpers");
var body = [];
@ -17,7 +17,7 @@ module.exports = function () {
)
]));
buildHelpers(body, namespace);
buildHelpers(body, namespace, whitelist);
return generator(tree).code;
};

View File

@ -3,9 +3,11 @@ var util = require("./util");
var each = require("lodash/collection/each");
var t = require("./types");
module.exports = function (body, namespace) {
module.exports = function (body, namespace, whitelist = []) {
each(File.helpers, function (name) {
var key = t.identifier(t.toIdentifier(name));
if (whitelist.length && whitelist.indexOf(key) >= 0) return;
body.push(t.expressionStatement(
t.assignmentExpression("=", t.memberExpression(namespace, key), util.template(name))
));

View File

@ -22,12 +22,13 @@ function File(opts) {
this.dynamicImported = [];
this.dynamicImports = [];
this.dynamicData = {};
this.data = {};
this.usedHelpers = {};
this.dynamicData = {};
this.data = {};
this.lastStatements = [];
this.opts = this.normalizeOptions(opts);
this.ast = {};
this.lastStatements = [];
this.opts = this.normalizeOptions(opts);
this.ast = {};
this.buildTransformers();
}
@ -85,6 +86,7 @@ File.validOptions = [
"externalHelpers",
"auxiliaryComment",
"compact",
"returnUsedHelpers",
"resolveModuleSource",
"moduleId",
@ -111,6 +113,7 @@ File.prototype.normalizeOptions = function (opts) {
defaults(opts, {
keepModuleIdExtensions: false,
resolveModuleSource: null,
returnUsedHelpers: false,
externalHelpers: false,
auxilaryComment: "",
experimental: false,
@ -331,6 +334,8 @@ File.prototype.addHelper = function (name) {
var declar = program._declarations && program._declarations[name];
if (declar) return declar.id;
this.usedHelpers[name] = true;
var runtime = this.get("helpersNamespace");
if (runtime) {
name = t.identifier(t.toIdentifier(name));
@ -448,6 +453,10 @@ File.prototype.generate = function () {
ast: null
};
if (this.opts.returnUsedHelpers) {
result.usedHelpers = Object.keys(this.usedHelpers);
}
if (opts.ast) result.ast = ast;
if (!opts.code) return result;