add loose option to computed property names

This commit is contained in:
Sebastian McKenzie
2015-01-14 00:35:45 +11:00
parent 8afec8b12a
commit afd3af834d
22 changed files with 168 additions and 14 deletions

View File

@@ -4,26 +4,62 @@ exports.ObjectExpression = function (node, parent, file, scope) {
var hasComputed = false;
var prop;
var key;
var i;
for (i in node.properties) {
for (var i in node.properties) {
hasComputed = t.isProperty(node.properties[i], { computed: true, kind: "init" });
if (hasComputed) break;
}
if (!hasComputed) return;
var initProps = [];
var objId = scope.generateUidBasedOnNode(parent, file);
//
var body = [];
var container = t.functionExpression(null, [], t.blockStatement(body));
container._aliasFunction = true;
//
var callback = spec;
if (file.isLoose("computedPropertyNames")) callback = loose;
var result = callback(node, body, objId, initProps, file);
if (result) return result;
//
body.unshift(t.variableDeclaration("var", [
t.variableDeclarator(objId, t.objectExpression(initProps))
]));
body.push(t.returnStatement(objId));
return t.callExpression(container, []);
};
var loose = function (node, body, objId) {
for (var i in node.properties) {
var prop = node.properties[i];
body.push(t.expressionStatement(
t.assignmentExpression(
"=",
t.memberExpression(objId, prop.key, prop.computed),
prop.value
)
));
}
};
var spec = function (node, body, objId, initProps, file) {
var props = node.properties;
// normalise key
for (i in props) {
for (var i in props) {
prop = props[i];
if (prop.kind !== "init") continue;
@@ -36,7 +72,6 @@ exports.ObjectExpression = function (node, parent, file, scope) {
// add all non-computed properties and `__proto__` properties to the initializer
var initProps = [];
var broken = false;
for (i in props) {
@@ -86,14 +121,4 @@ exports.ObjectExpression = function (node, parent, file, scope) {
return first;
}
}
//
body.unshift(t.variableDeclaration("var", [
t.variableDeclarator(objId, t.objectExpression(initProps))
]));
body.push(t.returnStatement(objId));
return t.callExpression(container, []);
};