Skip to content

Commit

Permalink
make sure 'computed_props' creates string keys. Closes #1500
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiosantoscode committed Mar 13, 2024
1 parent 03660f8 commit 1cdfba8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lib/compress/index.js
Expand Up @@ -3924,21 +3924,23 @@ function lift_key(self, compressor) {
if (!(self.key instanceof AST_Constant)) return self;
// allow certain acceptable props as not all AST_Constants are true constants
if (self.key instanceof AST_String || self.key instanceof AST_Number) {
if (self.key.value === "__proto__") return self;
if (self.key.value == "constructor"
const key = self.key.value.toString();

if (key === "__proto__") return self;
if (key == "constructor"
&& compressor.parent() instanceof AST_Class) return self;
if (self instanceof AST_ObjectKeyVal) {
self.quote = self.key.quote;
self.key = self.key.value;
self.key = key;
} else if (self instanceof AST_ClassProperty) {
self.quote = self.key.quote;
self.key = make_node(AST_SymbolClassProperty, self.key, {
name: self.key.value
name: key,
});
} else {
self.quote = self.key.quote;
self.key = make_node(AST_SymbolMethod, self.key, {
name: self.key.value
name: key,
});
}
}
Expand Down
14 changes: 14 additions & 0 deletions test/compress/object.js
Expand Up @@ -186,6 +186,20 @@ convert_computed_props_to_regular_ones: {
]
}

convert_computed_props_and_keep_quotes: {
options = {
booleans: true,
computed_props: true,
evaluate: true,
}
format = { quote_keys: true }
input: {
console.log({ [11]: 22, 33: 44 });
}
expect_exact: 'console.log({"11":22,"33":44});'
expect_stdout: "{ '11': 22, '33': 44 }"
}

computed_property_names_evaluated_1: {
options = {
evaluate: true
Expand Down

0 comments on commit 1cdfba8

Please sign in to comment.