Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert void 0 to a new var #2912

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ function Compressor(options, false_by_default) {
unsafe_regexp : false,
unsafe_undefined: false,
unused : !false_by_default,
// TODO Is "void" a good name? "void_to_var"?
void : false,
warnings : false,
}, true);
var global_defs = this.options["global_defs"];
Expand Down Expand Up @@ -199,6 +201,9 @@ merge(Compressor.prototype, {
if (this.option("expression")) {
node.process_expression(false);
}
if (this.option("void")) {
node.process_void();
}
return node;
},
info: function() {
Expand Down Expand Up @@ -315,6 +320,75 @@ merge(Compressor.prototype, {
self.transform(tt);
});

AST_Scope.DEFMETHOD("process_void", function (compressor) {
var lambdas_with_var = [];
var tw = new TreeWalker(function(node) {
if (node instanceof AST_Var) {
// TODO Find parent efficiently
// TODO Should AST_Scope be used instead?
var lambda = tw.find_parent(AST_Lambda);
if (lambda && !lambda._first_var) {
lambda._first_var = node;
lambdas_with_var.push(lambda);
}
} else if (node instanceof AST_UnaryPrefix
&& node.operator == "void"
&& node.expression instanceof AST_Number) {
// TODO Find parent efficiently
// TODO Should AST_Scope be used instead?
var lambda = tw.find_parent(AST_Lambda);
if (lambda) {
if (!lambda._void_uses) lambda._void_uses = [];
// TODO Simpler way?
node._parent = tw.parent();
lambda._void_uses.push(node);
}
} else if (node instanceof AST_Catch
&& node.argname.name.indexOf("undefined") == 0) {
// TODO Is there a cleaner way to do this?
return true;
}
});

this.walk(tw);

// TODO Is there a better way to replace a node?
var replacer = new TreeTransformer(function(node) {
if (node._replacement) {
return node._replacement;
}
});

lambdas_with_var.forEach(function(lambda) {
if (lambda._processed_void || !lambda._void_uses) return;

// TODO This is similar to make_sym() below. Should they be combined?
var new_var = make_node(AST_SymbolVar, null, {
name: lambda.make_var_name("undefined"),
scope: lambda,
});

var def = lambda.def_variable(new_var);
lambda.enclosed.push(def);

var new_var_def = make_node(AST_VarDef, lambda._first_var, {
name: new_var,
value: null,
});

lambda._first_var.definitions.push(new_var_def);


lambda._void_uses.forEach(function(void_use) {
// TODO Is there a better way to replace a node?
void_use._replacement = make_node(AST_SymbolRef, void_use, new_var);
void_use._parent.transform(replacer);
});

lambda._processed_void = true;
});
});

(function(def){
def(AST_Node, noop);

Expand Down
125 changes: 125 additions & 0 deletions test/compress/void.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
void_1: {
options = {
void: true,
}
input: {
var a = 0;
x = void 0;
if (void 0 === b)
c = void 0;

function f1() {
var a = 1;
console.log(void 0);
}

function f2(undefined) {
var a = 2;
console.log(void 0);
}

function f3() {
var undefined = 3;
console.log(void 0);
}

function f4() {
console.log(void 0);
for (var a = 4;;);
var b = 4;

function f5() {
var c = 5;
var d = 5;
console.log(void 0);
}
}

function f6() {
try {
var a = 6;
console.log(void 0);
} catch (e) {
console.log(void 0);
}
}
}
expect: {
var a = 0;
x = void 0;
if (void 0 === b)
c = void 0;

function f1() {
var a = 1, undefined;
console.log(undefined)
}

function f2(undefined) {
var a = 2, undefined$0;
console.log(undefined$0)
}

function f3() {
var undefined = 3, undefined$0;
console.log(undefined$0)
}

function f4() {
console.log(undefined);
for (var a = 4, undefined;;);
var b = 4;

function f5() {
var c = 5, undefined;
var d = 5;
console.log(undefined)
}
}

function f6() {
try {
var a = 6, undefined;
console.log(undefined)
} catch (e) {
console.log(undefined)
}
}
}
}

void_2: {
options = {
void: true,
}
input: {
f();
function f() {
var a = 1;
console.log(void 0);

try {
throw "FAIL";
} catch (undefined) {
console.log(void 0);
}
}
}
expect: {
f();
function f() {
var a = 1, undefined;
console.log(undefined);
try {
throw "FAIL"
} catch (undefined) {
console.log(void 0);
}
}

}
expect_stdout: [
"undefined",
"undefined",
]
}