add loose option to computed property names
This commit is contained in:
parent
8afec8b12a
commit
afd3af834d
@ -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, []);
|
||||
};
|
||||
|
||||
3
test/fixtures/transformation/es6-computed-property-names-loose/argument/actual.js
vendored
Normal file
3
test/fixtures/transformation/es6-computed-property-names-loose/argument/actual.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
foo({
|
||||
[bar]: "foobar"
|
||||
});
|
||||
8
test/fixtures/transformation/es6-computed-property-names-loose/argument/expected.js
vendored
Normal file
8
test/fixtures/transformation/es6-computed-property-names-loose/argument/expected.js
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
"use strict";
|
||||
|
||||
foo((function () {
|
||||
var _foo = {};
|
||||
|
||||
_foo[bar] = "foobar";
|
||||
return _foo;
|
||||
})());
|
||||
3
test/fixtures/transformation/es6-computed-property-names-loose/assignment/actual.js
vendored
Normal file
3
test/fixtures/transformation/es6-computed-property-names-loose/assignment/actual.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
foo = {
|
||||
[bar]: "foobar"
|
||||
};
|
||||
8
test/fixtures/transformation/es6-computed-property-names-loose/assignment/expected.js
vendored
Normal file
8
test/fixtures/transformation/es6-computed-property-names-loose/assignment/expected.js
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
"use strict";
|
||||
|
||||
foo = (function () {
|
||||
var _foo = {};
|
||||
|
||||
_foo[bar] = "foobar";
|
||||
return _foo;
|
||||
})();
|
||||
3
test/fixtures/transformation/es6-computed-property-names-loose/ignore-symbol/actual.js
vendored
Normal file
3
test/fixtures/transformation/es6-computed-property-names-loose/ignore-symbol/actual.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
var foo = {
|
||||
[Symbol.iterator]: "foobar"
|
||||
};
|
||||
8
test/fixtures/transformation/es6-computed-property-names-loose/ignore-symbol/expected.js
vendored
Normal file
8
test/fixtures/transformation/es6-computed-property-names-loose/ignore-symbol/expected.js
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
"use strict";
|
||||
|
||||
var foo = (function () {
|
||||
var _foo = {};
|
||||
|
||||
_foo[Symbol.iterator] = "foobar";
|
||||
return _foo;
|
||||
})();
|
||||
5
test/fixtures/transformation/es6-computed-property-names-loose/method/actual.js
vendored
Normal file
5
test/fixtures/transformation/es6-computed-property-names-loose/method/actual.js
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
var obj = {
|
||||
[foobar]() {
|
||||
return "foobar";
|
||||
}
|
||||
};
|
||||
11
test/fixtures/transformation/es6-computed-property-names-loose/method/expected.js
vendored
Normal file
11
test/fixtures/transformation/es6-computed-property-names-loose/method/expected.js
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
"use strict";
|
||||
|
||||
var obj = (function () {
|
||||
var _obj = {};
|
||||
|
||||
_obj[foobar] = function () {
|
||||
return "foobar";
|
||||
};
|
||||
|
||||
return _obj;
|
||||
})();
|
||||
6
test/fixtures/transformation/es6-computed-property-names-loose/mixed/actual.js
vendored
Normal file
6
test/fixtures/transformation/es6-computed-property-names-loose/mixed/actual.js
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
var obj = {
|
||||
["x" + foo]: "heh",
|
||||
["y" + bar]: "noo",
|
||||
foo: "foo",
|
||||
bar: "bar"
|
||||
};
|
||||
11
test/fixtures/transformation/es6-computed-property-names-loose/mixed/expected.js
vendored
Normal file
11
test/fixtures/transformation/es6-computed-property-names-loose/mixed/expected.js
vendored
Normal 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;
|
||||
})();
|
||||
4
test/fixtures/transformation/es6-computed-property-names-loose/multiple/actual.js
vendored
Normal file
4
test/fixtures/transformation/es6-computed-property-names-loose/multiple/actual.js
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
var obj = {
|
||||
["x" + foo]: "heh",
|
||||
["y" + bar]: "noo"
|
||||
};
|
||||
9
test/fixtures/transformation/es6-computed-property-names-loose/multiple/expected.js
vendored
Normal file
9
test/fixtures/transformation/es6-computed-property-names-loose/multiple/expected.js
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
"use strict";
|
||||
|
||||
var obj = (function () {
|
||||
var _obj = {};
|
||||
|
||||
_obj["x" + foo] = "heh";
|
||||
_obj["y" + bar] = "noo";
|
||||
return _obj;
|
||||
})();
|
||||
3
test/fixtures/transformation/es6-computed-property-names-loose/options.json
vendored
Normal file
3
test/fixtures/transformation/es6-computed-property-names-loose/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"loose": ["computedPropertyNames"]
|
||||
}
|
||||
3
test/fixtures/transformation/es6-computed-property-names-loose/single/actual.js
vendored
Normal file
3
test/fixtures/transformation/es6-computed-property-names-loose/single/actual.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
var obj = {
|
||||
["x" + foo]: "heh"
|
||||
};
|
||||
8
test/fixtures/transformation/es6-computed-property-names-loose/single/expected.js
vendored
Normal file
8
test/fixtures/transformation/es6-computed-property-names-loose/single/expected.js
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
"use strict";
|
||||
|
||||
var obj = (function () {
|
||||
var _obj = {};
|
||||
|
||||
_obj["x" + foo] = "heh";
|
||||
return _obj;
|
||||
})();
|
||||
3
test/fixtures/transformation/es6-computed-property-names-loose/this/actual.js
vendored
Normal file
3
test/fixtures/transformation/es6-computed-property-names-loose/this/actual.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
var obj = {
|
||||
["x" + this.foo]: "heh"
|
||||
};
|
||||
9
test/fixtures/transformation/es6-computed-property-names-loose/this/expected.js
vendored
Normal file
9
test/fixtures/transformation/es6-computed-property-names-loose/this/expected.js
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
"use strict";
|
||||
|
||||
var _this = this;
|
||||
var obj = (function () {
|
||||
var _obj = {};
|
||||
|
||||
_obj["x" + _this.foo] = "heh";
|
||||
return _obj;
|
||||
})();
|
||||
4
test/fixtures/transformation/es6-computed-property-names-loose/two/actual.js
vendored
Normal file
4
test/fixtures/transformation/es6-computed-property-names-loose/two/actual.js
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
var obj = {
|
||||
first: "first",
|
||||
["second"]: "second",
|
||||
};
|
||||
9
test/fixtures/transformation/es6-computed-property-names-loose/two/expected.js
vendored
Normal file
9
test/fixtures/transformation/es6-computed-property-names-loose/two/expected.js
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
"use strict";
|
||||
|
||||
var obj = (function () {
|
||||
var _obj = {};
|
||||
|
||||
_obj.first = "first";
|
||||
_obj.second = "second";
|
||||
return _obj;
|
||||
})();
|
||||
3
test/fixtures/transformation/es6-computed-property-names-loose/variable/actual.js
vendored
Normal file
3
test/fixtures/transformation/es6-computed-property-names-loose/variable/actual.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
var foo = {
|
||||
[bar]: "foobar"
|
||||
};
|
||||
8
test/fixtures/transformation/es6-computed-property-names-loose/variable/expected.js
vendored
Normal file
8
test/fixtures/transformation/es6-computed-property-names-loose/variable/expected.js
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
"use strict";
|
||||
|
||||
var foo = (function () {
|
||||
var _foo = {};
|
||||
|
||||
_foo[bar] = "foobar";
|
||||
return _foo;
|
||||
})();
|
||||
Loading…
x
Reference in New Issue
Block a user