From 91fc1c82b56986df51ad1450c18718bc585deca9 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sun, 8 Nov 2020 15:38:32 +0000 Subject: support computed property name in object literal (#4268) --- lib/output.js | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) (limited to 'lib/output.js') diff --git a/lib/output.js b/lib/output.js index ef1d41f6..884cafc3 100644 --- a/lib/output.js +++ b/lib/output.js @@ -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 { + 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_name(key); + output.print_string(key, quote); } - } 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); }); })(); -- cgit v1.2.3