Skip to content

Commit

Permalink
avoid optimizations inside computed keys. Closes #1514. Closes #272
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiosantoscode committed Apr 1, 2024
1 parent 1b5cbc2 commit 3f912af
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 5 deletions.
11 changes: 11 additions & 0 deletions lib/compress/index.js
Expand Up @@ -401,6 +401,17 @@ class Compressor extends TreeWalker {
}
}

in_computed_key() {
if (!this.option("evaluate")) return false;
var self = this.self();
for (var i = 0, p; p = this.parent(i); i++) {
if (p instanceof AST_ObjectProperty && p.key === self) {
return true;
}
}
return false;
}

get_toplevel() {
return this._toplevel;
}
Expand Down
4 changes: 4 additions & 0 deletions lib/compress/inline.js
Expand Up @@ -167,6 +167,8 @@ function is_const_symbol_short_than_init_value(def, fixed_value) {
}

export function inline_into_symbolref(self, compressor) {
if (compressor.in_computed_key()) return self;

const parent = compressor.parent();
const def = self.definition();
const nearest_scope = compressor.find_scope();
Expand Down Expand Up @@ -316,6 +318,8 @@ export function inline_into_symbolref(self, compressor) {
}

export function inline_into_call(self, compressor) {
if (compressor.in_computed_key()) return self;

var exp = self.expression;
var fn = exp;
var simple_args = self.args.every((arg) => !(arg instanceof AST_Expansion));
Expand Down
4 changes: 3 additions & 1 deletion lib/compress/tighten-body.js
Expand Up @@ -82,6 +82,7 @@ import {
AST_Number,
AST_Object,
AST_ObjectKeyVal,
AST_ObjectProperty,
AST_PropAccess,
AST_RegExp,
AST_Return,
Expand Down Expand Up @@ -116,7 +117,7 @@ import {
walk,
walk_abort,

_NOINLINE
_NOINLINE,
} from "../ast.js";
import {
make_node,
Expand Down Expand Up @@ -328,6 +329,7 @@ export function tighten_body(statements, compressor) {
|| node instanceof AST_SymbolRef
&& parent instanceof AST_Call
&& has_annotation(parent, _NOINLINE)
|| node instanceof AST_ObjectProperty && node.key instanceof AST_Node
) {
abort = true;
return node;
Expand Down
3 changes: 2 additions & 1 deletion test/compress/collapse_vars.js
Expand Up @@ -1348,9 +1348,10 @@ collapse_vars_object: {
}
expect: {
function f0(x, y) {
var z = x + y;
return {
get b() { return 7; },
r: x + y
r: z,
};
}
function f1(x, y) {
Expand Down
8 changes: 5 additions & 3 deletions test/compress/harmony.js
Expand Up @@ -979,9 +979,11 @@ issue_2349b: {
}, "blah"));
}
expect: {
console.log([ {
blah: 42
} ].map(({["blah"]: l}) => l));
console.log((
a = "blah",
[{ blah: 42 }].map(({[a]: l}) => l)
));
var a;
}
expect_stdout: [
"[ 42 ]",
Expand Down
40 changes: 40 additions & 0 deletions test/compress/properties.js
Expand Up @@ -1352,6 +1352,46 @@ computed_property: {
]
}

skip_computed_properties: {
options = {
evaluate: true,
inline: true,
}
input: {
export class A {
[(() => class {})()] = 1;
}
}
expect: {
export class A {
[(() => class {})()] = 1;
}
}
}

skip_computed_properties_2: {
options = {
toplevel: true,
evaluate: true,
inline: true,
reduce_vars: true,
unused: true,
collapse_vars: true,
}
input: {
function f(o) {
return {[o.key]: o};
}
var obj = {key: 'xyz'.slice(1, -1)};
console.log(f(obj));
}
expect: {
var obj = {key: 'xyz'.slice(1, -1)};
console.log((o=obj, {[o.key]: o}));
var o;
}
}

new_this: {
options = {
properties: true,
Expand Down

0 comments on commit 3f912af

Please sign in to comment.