support computed property name in object literal (#4268)

This commit is contained in:
Alex Lam S.L
2020-11-08 15:38:32 +00:00
committed by GitHub
parent 810cd40356
commit 91fc1c82b5
10 changed files with 166 additions and 139 deletions

View File

@@ -699,6 +699,7 @@ function OutputStream(options) {
// (false, true) ? (a = 10, b = 20) : (c = 30)
// ==> 20 (side effect, set a := 10 and b := 20)
|| p instanceof AST_Conditional
// { [(1, 2)]: 3 }[2] ==> 3
// { foo: (1, 2) }.foo ==> 2
|| p instanceof AST_ObjectProperty
// (1, {foo:2}).foo or (1, {foo:2})["foo"] ==> 2
@@ -1289,25 +1290,33 @@ function OutputStream(options) {
else print_braced_empty(this, output);
});
function print_property_name(key, quote, output) {
if (output.option("quote_keys")) {
function print_property_key(self, output) {
var key = self.key;
if (key instanceof AST_Node) {
output.with_square(function() {
key.print(output);
});
} else if (output.option("quote_keys")) {
output.print_string(key);
} else if ("" + +key == key && key >= 0) {
output.print(make_num(key));
} else if (RESERVED_WORDS[key] ? !output.option("ie8") : is_identifier_string(key)) {
if (quote && output.option("keep_quoted_props")) {
output.print_string(key, quote);
} else {
output.print_name(key);
}
} else {
output.print_string(key, quote);
var quote = self.start && self.start.quote;
if (RESERVED_WORDS[key] ? !output.option("ie8") : is_identifier_string(key)) {
if (quote && output.option("keep_quoted_props")) {
output.print_string(key, quote);
} else {
output.print_name(key);
}
} else {
output.print_string(key, quote);
}
}
}
DEFPRINT(AST_ObjectKeyVal, function(output) {
var self = this;
print_property_name(self.key, self.quote, output);
print_property_key(self, output);
output.colon();
self.value.print(output);
});
@@ -1316,7 +1325,7 @@ function OutputStream(options) {
var self = this;
output.print(type);
output.space();
print_property_name(self.key.name, self.quote, output);
print_property_key(self, output);
self.value._codegen(output, true);
};
}
@@ -1488,14 +1497,7 @@ function OutputStream(options) {
output.add_mapping(this.start);
});
DEFMAP([
AST_ObjectGetter,
AST_ObjectSetter,
], function(output) {
output.add_mapping(this.start, this.key.name);
});
DEFMAP([ AST_ObjectProperty ], function(output) {
output.add_mapping(this.start, this.key);
if (typeof this.key == "string") output.add_mapping(this.start, this.key);
});
})();