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, []);
};

View File

@ -0,0 +1,3 @@
foo({
[bar]: "foobar"
});

View File

@ -0,0 +1,8 @@
"use strict";
foo((function () {
var _foo = {};
_foo[bar] = "foobar";
return _foo;
})());

View File

@ -0,0 +1,3 @@
foo = {
[bar]: "foobar"
};

View File

@ -0,0 +1,8 @@
"use strict";
foo = (function () {
var _foo = {};
_foo[bar] = "foobar";
return _foo;
})();

View File

@ -0,0 +1,3 @@
var foo = {
[Symbol.iterator]: "foobar"
};

View File

@ -0,0 +1,8 @@
"use strict";
var foo = (function () {
var _foo = {};
_foo[Symbol.iterator] = "foobar";
return _foo;
})();

View File

@ -0,0 +1,5 @@
var obj = {
[foobar]() {
return "foobar";
}
};

View File

@ -0,0 +1,11 @@
"use strict";
var obj = (function () {
var _obj = {};
_obj[foobar] = function () {
return "foobar";
};
return _obj;
})();

View File

@ -0,0 +1,6 @@
var obj = {
["x" + foo]: "heh",
["y" + bar]: "noo",
foo: "foo",
bar: "bar"
};

View File

@ -0,0 +1,11 @@
"use strict";
var obj = (function () {
var _obj = {};
_obj["x" + foo] = "heh";
_obj["y" + bar] = "noo";
_obj.foo = "foo";
_obj.bar = "bar";
return _obj;
})();

View File

@ -0,0 +1,4 @@
var obj = {
["x" + foo]: "heh",
["y" + bar]: "noo"
};

View File

@ -0,0 +1,9 @@
"use strict";
var obj = (function () {
var _obj = {};
_obj["x" + foo] = "heh";
_obj["y" + bar] = "noo";
return _obj;
})();

View File

@ -0,0 +1,3 @@
{
"loose": ["computedPropertyNames"]
}

View File

@ -0,0 +1,3 @@
var obj = {
["x" + foo]: "heh"
};

View File

@ -0,0 +1,8 @@
"use strict";
var obj = (function () {
var _obj = {};
_obj["x" + foo] = "heh";
return _obj;
})();

View File

@ -0,0 +1,3 @@
var obj = {
["x" + this.foo]: "heh"
};

View File

@ -0,0 +1,9 @@
"use strict";
var _this = this;
var obj = (function () {
var _obj = {};
_obj["x" + _this.foo] = "heh";
return _obj;
})();

View File

@ -0,0 +1,4 @@
var obj = {
first: "first",
["second"]: "second",
};

View File

@ -0,0 +1,9 @@
"use strict";
var obj = (function () {
var _obj = {};
_obj.first = "first";
_obj.second = "second";
return _obj;
})();

View File

@ -0,0 +1,3 @@
var foo = {
[bar]: "foobar"
};

View File

@ -0,0 +1,8 @@
"use strict";
var foo = (function () {
var _foo = {};
_foo[bar] = "foobar";
return _foo;
})();