Skip to content

Commit

Permalink
Do not collapse_vars in the face of classes. Fixes #627
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiosantoscode committed Mar 31, 2020
1 parent b80c74e commit e376396
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 155 deletions.
17 changes: 2 additions & 15 deletions lib/compress/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,7 @@ function tighten_body(statements, compressor) {
|| node instanceof AST_With
|| node instanceof AST_Yield
|| node instanceof AST_Export
|| node instanceof AST_Class
|| parent instanceof AST_For && node !== parent.init
|| !replace_all
&& (
Expand Down Expand Up @@ -1507,21 +1508,7 @@ function tighten_body(statements, compressor) {

function handle_custom_scan_order(node) {
// Skip (non-executed) functions
if (node instanceof AST_Lambda) return node;

// Skip the bits of classes that aren't executed immediately
if (node instanceof AST_Class) {
if (node.extends) node.extends = node.extends.transform(scanner);
for (const prop of node.properties) {
if (prop.computed_key()) {
prop.key = prop.key.transform(scanner);
}
if (prop.static && prop.value) {
prop.value = prop.value.transform(scanner);
}
}
return node;
}
if (node instanceof AST_Scope) return node;

// Scan case expressions first in a switch statement
if (node instanceof AST_Switch) {
Expand Down
149 changes: 11 additions & 138 deletions test/compress/collapse_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -6007,147 +6007,20 @@ noinline_annotation: {
}
}

class_extends: {
ignore_class: {
options = {
collapse_vars: true,
toplevel: true
}
input: {
var A;
A = class {
static which = 'A'
};
class B extends A { static which = 'B' };
console.log(B.which, A.which);
}
expect: {
var A;
class B extends (A = class { static which = 'A' }) {
static which = 'B'
};
console.log(B.which, A.which);
}
expect_stdout: "B A"
node_version: ">=12"
}

class_statics: {
options = {
collapse_vars: true,
toplevel: true
}
input: {
var A;
A = class {
static which = 'A'
};
class B {
static which = 'B'
static A = A
};
console.log(B.which, B.A.which, A.which);
}
expect: {
var A;
class B {
static which = 'B'
static A = (A = class { static which = 'A' })
};
console.log(B.which, B.A.which, A.which);
}
expect_stdout: "B A A"
node_version: ">=12"
}

class_computed_prop_defs: {
options = {
collapse_vars: true,
defaults: true,
toplevel: true
}
input: {
var A;
A = class {
static which = 'A'
};
class B {
static which = 'B'
static [A.which] = A
};
console.log(B.which, B.A.which, A.which);
global.leak = x => class dummy { get pass() { return x } }
global.module = {};
(function () {
const SuperClass = leak("PASS")
class TheClass extends SuperClass {}
module.exports = TheClass
}())
console.log(new module.exports().pass)
}
expect: {
var A;
class B {
static which = 'B'
static [
(A = class {
static which = 'A'
}).which
] = A
};
console.log(B.which, B.A.which, A.which);
}
expect_stdout: "B A A"
node_version: ">=12"
}

class_computed_prop_defs_2: {
options = {
collapse_vars: true,
toplevel: true
}
input: {
var A;
A = class {
static which = 'A';
};
class B {
static which = 'B';
[A.which] = A;
};
console.log(B.which, new B().A.which, A.which);
}
expect: {
var A;
class B {
static which = 'B';
[(A = class {
static which = 'A'
}).which] = A;
};
console.log(B.which, new B().A.which, A.which);
}
expect_stdout: "B A A"
node_version: ">=12"
}

class_regular_prop: {
options = {
collapse_vars: true,
toplevel: true
}
input: {
var A;
A = class {
static which = 'A'
};
class B {
static which = 'B'
regular_prop = A
};
console.log(B.which, new B().regular_prop.which, A.which);
}
expect: {
var A;
A = class {
static which = 'A'
};
class B {
static which = 'B'
regular_prop = A
};
console.log(B.which, new B().regular_prop.which, A.which);
}
expect_stdout: "B A A"
node_version: ">=12"
expect_stdout: "PASS"
}
4 changes: 2 additions & 2 deletions test/compress/harmony.js
Original file line number Diff line number Diff line change
Expand Up @@ -1776,8 +1776,8 @@ issue_3061: {
}
expect: {
console.log(new class extends(function(base) {
return class extends Error {};
}()){}() instanceof Error);
return class extends base {};
}(Error)){}() instanceof Error);
}
expect_stdout: "true"
}
Expand Down

0 comments on commit e376396

Please sign in to comment.