diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2020-11-08 15:38:32 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-08 23:38:32 +0800 |
commit | 91fc1c82b56986df51ad1450c18718bc585deca9 (patch) | |
tree | a534652eea79949356bee8a877b71a49a5b18e82 /lib/output.js | |
parent | 810cd40356f6fa1ddbc9c97a0df95bd1d94a2720 (diff) | |
download | tracifyjs-91fc1c82b56986df51ad1450c18718bc585deca9.tar.gz tracifyjs-91fc1c82b56986df51ad1450c18718bc585deca9.zip |
support computed property name in object literal (#4268)
Diffstat (limited to 'lib/output.js')
-rw-r--r-- | lib/output.js | 38 |
1 files changed, 20 insertions, 18 deletions
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); }); })(); |