computed properties

This commit is contained in:
Fábio Santos
2015-09-07 22:46:07 +01:00
parent b14496c742
commit b31918bbf0
5 changed files with 42 additions and 0 deletions

View File

@@ -921,6 +921,16 @@ var AST_ObjectKeyVal = DEFNODE("ObjectKeyVal", "quote", {
} }
}, AST_ObjectProperty); }, AST_ObjectProperty);
var AST_ObjectComputedKeyVal = DEFNODE("ObjectComputedKeyVal", null, {
$documentation: "An object property whose key is computed. Like `[Symbol.iterator]: function...` or `[routes.homepage]: renderHomepage`",
_walk: function(visitor) {
return visitor._visit(this, function(){
this.key._walk(visitor);
this.value._walk(visitor);
});
}
}, AST_ObjectProperty);
var AST_ObjectSymbol = DEFNODE("ObjectSymbol", "symbol", { var AST_ObjectSymbol = DEFNODE("ObjectSymbol", "symbol", {
$propdoc: { $propdoc: {
symbol: "[AST_SymbolRef] what symbol it is" symbol: "[AST_SymbolRef] what symbol it is"

View File

@@ -952,6 +952,9 @@ merge(Compressor.prototype, {
return false; return false;
}); });
def(AST_ObjectProperty, function(compressor){ def(AST_ObjectProperty, function(compressor){
if (this instanceof AST_ObjectComputedKeyVal &&
this.key.has_side_effects(compressor))
return true;
return this.value.has_side_effects(compressor); return this.value.has_side_effects(compressor);
}); });
def(AST_Array, function(compressor){ def(AST_Array, function(compressor){

View File

@@ -1203,6 +1203,13 @@ function OutputStream(options) {
self.key.print(output); self.key.print(output);
self.value._do_print(output, true); self.value._do_print(output, true);
}); });
DEFPRINT(AST_ObjectComputedKeyVal, function(self, output) {
output.print("[");
self.key.print(output);
output.print("]:");
output.space();
self.value.print(output);
});
DEFPRINT(AST_Symbol, function(self, output){ DEFPRINT(AST_Symbol, function(self, output){
var def = self.definition(); var def = self.definition();
output.print_name(def ? def.mangled_name || def.name : self.name); output.print_name(def ? def.mangled_name || def.name : self.name);

View File

@@ -1466,6 +1466,15 @@ function parse($TEXT, options) {
} }
} }
if (type == "punc" && start.value == "[") {
expect(":");
a.push(new AST_ObjectComputedKeyVal({
key: name,
value: expression(false)
}));
continue;
}
if (!is("punc", ":")) { if (!is("punc", ":")) {
// It's one of those object destructurings, the value is its own name // It's one of those object destructurings, the value is its own name
a.push(new AST_ObjectSymbol({ a.push(new AST_ObjectSymbol({
@@ -1495,6 +1504,12 @@ function parse($TEXT, options) {
var tmp = S.token; var tmp = S.token;
next(); next();
switch (tmp.type) { switch (tmp.type) {
case "punc":
if (tmp.value === "[") {
var ex = expression(false);
expect("]");
return ex;
} else unexpected();
case "num": case "num":
case "string": case "string":
case "name": case "name":

View File

@@ -36,6 +36,13 @@ regression_arrow_functions_and_hoist: {
expect_exact: "a=>b;" expect_exact: "a=>b;"
} }
computed_property_names: {
input: {
obj({ ["x" + "x"]: 6 });
}
expect_exact: "obj({[\"x\"+\"x\"]:6});"
}
typeof_arrow_functions: { typeof_arrow_functions: {
options = { options = {
evaluate: true evaluate: true