Represent helpers as simple modules.

This commit is contained in:
Logan Smyth
2017-05-05 01:01:17 -07:00
parent 0c5fae2faa
commit 158e9fbfd7
26 changed files with 583 additions and 364 deletions

View File

@@ -29,7 +29,18 @@ function buildGlobal(namespace, builder) {
);
const tree = t.program([
t.expressionStatement(
t.callExpression(container, [helpers.get("selfGlobal")]),
t.callExpression(container, [
// typeof global === "undefined" ? self : global
t.conditionalExpression(
t.binaryExpression(
"===",
t.unaryExpression("typeof", t.identifier("global")),
t.stringLiteral("undefined"),
),
t.identifier("self"),
t.identifier("global"),
),
]),
),
]);
@@ -131,16 +142,12 @@ function buildHelpers(body, namespace, whitelist) {
helpers.list.forEach(function(name) {
if (whitelist && whitelist.indexOf(name) < 0) return;
const key = t.identifier(name);
body.push(
t.expressionStatement(
t.assignmentExpression(
"=",
t.memberExpression(namespace, key),
helpers.get(name),
),
),
const { nodes } = helpers.get(
name,
t.memberExpression(namespace, t.identifier(name)),
);
body.push(...nodes);
});
}
export default function(

View File

@@ -241,24 +241,18 @@ export default class File extends Store {
return t.memberExpression(runtime, t.identifier(name));
}
const ref = getHelper(name);
const ownBindingNames = Object.keys(this.scope.getAllBindings());
const uid = (this.declarations[name] = this.scope.generateUidIdentifier(
name,
));
if (t.isFunctionExpression(ref) && !ref.id) {
ref.body._compact = true;
ref.id = uid;
ref.type = "FunctionDeclaration";
this.path.unshiftContainer("body", ref);
} else {
ref._compact = true;
this.scope.push({
id: uid,
init: ref,
unique: true,
});
}
const { nodes } = getHelper(name, uid, ownBindingNames);
nodes.forEach(node => {
node._compact = true;
});
this.path.unshiftContainer("body", nodes);
return uid;
}