Skip to content

Commit

Permalink
Don't mangle computed properties with keep_quoted (#1058)
Browse files Browse the repository at this point in the history
* Preserve computed attributes when mangling with keep_quoted

There were various failures when using computed properties with property mangling enabled with `keep_quoted`:

```js
// keep_quoted: true

let prop = '_foo';
let o = {
  [prop]: 'bar'
};
console.log(o[id('_foo')]);

// - - -

let prop = '_foo';
let o = {
  [prop]() { return 'bar' }
};
console.log(o[id('_foo')]());

// - - -

let prop = '_foo';
let o = {
  get [prop]() { return 'bar' }
};
console.log(o[id('_foo')]);
```

```js
// keep_quoted: 'strict'

let prop = '_foo';
let o = {
  [prop]: 'bar'
};
console.log(o[id('_foo')]);
```

* Add tests for strict/loose keep_quotes actually reserving names

* Unskip tests
  • Loading branch information
jridgewell committed Sep 11, 2021
1 parent 748e3a6 commit 3409eb6
Show file tree
Hide file tree
Showing 7 changed files with 1,680 additions and 15 deletions.
3 changes: 3 additions & 0 deletions lib/compress/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4485,12 +4485,15 @@ function lift_key(self, compressor) {
if (self.key.value == "constructor"
&& compressor.parent() instanceof AST_Class) return self;
if (self instanceof AST_ObjectKeyVal) {
self.quote = self.key.quote;
self.key = self.key.value;
} else if (self instanceof AST_ClassProperty) {
self.quote = self.key.quote;
self.key = make_node(AST_SymbolClassProperty, self.key, {
name: self.key.value
});
} else {
self.quote = self.key.quote;
self.key = make_node(AST_SymbolMethod, self.key, {
name: self.key.value
});
Expand Down
20 changes: 9 additions & 11 deletions lib/propmangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ function mangle_properties(ast, options) {
var names_to_mangle = new Set();
var unmangleable = new Set();

var keep_quoted_strict = options.keep_quoted === "strict";
var keep_quoted = !!options.keep_quoted;

// step 1: find candidates to mangle
ast.walk(new TreeWalker(function(node) {
Expand All @@ -231,13 +231,12 @@ function mangle_properties(ast, options) {
) {
// handled by mangle_private_properties
} else if (node instanceof AST_ObjectKeyVal) {
if (typeof node.key == "string" &&
(!keep_quoted_strict || !node.quote)) {
if (typeof node.key == "string" && (!keep_quoted || !node.quote)) {
add(node.key);
}
} else if (node instanceof AST_ObjectProperty) {
// setter or getter, since KeyVal is handled above
if (!keep_quoted_strict || !node.key.end.quote) {
if (!keep_quoted || !node.quote) {
add(node.key.name);
}
} else if (node instanceof AST_Dot) {
Expand All @@ -250,11 +249,11 @@ function mangle_properties(ast, options) {
declared = !(root.thedef && root.thedef.undeclared);
}
if (declared &&
(!keep_quoted_strict || !node.quote)) {
(!keep_quoted || !node.quote)) {
add(node.property);
}
} else if (node instanceof AST_Sub) {
if (!keep_quoted_strict) {
if (!keep_quoted) {
addStrings(node.property, add);
}
} else if (node instanceof AST_Call
Expand All @@ -276,20 +275,19 @@ function mangle_properties(ast, options) {
) {
// handled by mangle_private_properties
} else if (node instanceof AST_ObjectKeyVal) {
if (typeof node.key == "string" &&
(!keep_quoted_strict || !node.quote)) {
if (typeof node.key == "string" && (!keep_quoted || !node.quote)) {
node.key = mangle(node.key);
}
} else if (node instanceof AST_ObjectProperty) {
// setter, getter, method or class field
if (!keep_quoted_strict || !node.key.end.quote) {
if (!keep_quoted || !node.quote) {
node.key.name = mangle(node.key.name);
}
} else if (node instanceof AST_Dot) {
if (!keep_quoted_strict || !node.quote) {
if (!keep_quoted || !node.quote) {
node.property = mangle(node.property);
}
} else if (!options.keep_quoted && node instanceof AST_Sub) {
} else if (!keep_quoted && node instanceof AST_Sub) {
node.property = mangleStrings(node.property);
} else if (node instanceof AST_Call
&& node.expression.print_to_string() == "Object.defineProperty") {
Expand Down

0 comments on commit 3409eb6

Please sign in to comment.