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;
}

View File

@@ -5,8 +5,6 @@ Object.defineProperty(exports, "__esModule", {
});
exports.default = void 0;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var foo = function () {
var _ref2 = _asyncToGenerator(
/*#__PURE__*/
@@ -55,6 +53,10 @@ function _asyncToGenerator(fn) { return function () { var _this = this, _argumen
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
var Foo =
/*#__PURE__*/
function () {