Fix output for arrays containing undefined values.
1b6bcca7 was a first attempt at this. That commit made Uglify stop replacing
holes with undefined, but instead it started replacing undefined with
holes. This is slightly problematic, because there is a difference between a
hole and an undefined value. More problematically, it changed [1,undefined] to
[1,] which generally doesn't even parse as a hole (just as a trailing comma), so
it didn't even preserve the length of the array!
Instead, parse holes as their own special AST node which prints invisibly.
This commit is contained in:
committed by
Mihai Bazon
parent
605f330e69
commit
1529ab965a
@@ -863,6 +863,11 @@ var AST_Undefined = DEFNODE("Undefined", null, {
|
|||||||
value: (function(){}())
|
value: (function(){}())
|
||||||
}, AST_Atom);
|
}, AST_Atom);
|
||||||
|
|
||||||
|
var AST_Hole = DEFNODE("Hole", null, {
|
||||||
|
$documentation: "A hole in an array",
|
||||||
|
value: (function(){}())
|
||||||
|
}, AST_Atom);
|
||||||
|
|
||||||
var AST_Infinity = DEFNODE("Infinity", null, {
|
var AST_Infinity = DEFNODE("Infinity", null, {
|
||||||
$documentation: "The `Infinity` value",
|
$documentation: "The `Infinity` value",
|
||||||
value: 1/0
|
value: 1/0
|
||||||
|
|||||||
@@ -984,8 +984,7 @@ function OutputStream(options) {
|
|||||||
if (len > 0) output.space();
|
if (len > 0) output.space();
|
||||||
a.forEach(function(exp, i){
|
a.forEach(function(exp, i){
|
||||||
if (i) output.comma();
|
if (i) output.comma();
|
||||||
if (!(exp instanceof AST_Undefined))
|
exp.print(output);
|
||||||
exp.print(output);
|
|
||||||
});
|
});
|
||||||
if (len > 0) output.space();
|
if (len > 0) output.space();
|
||||||
});
|
});
|
||||||
@@ -1036,6 +1035,9 @@ function OutputStream(options) {
|
|||||||
DEFPRINT(AST_Undefined, function(self, output){
|
DEFPRINT(AST_Undefined, function(self, output){
|
||||||
output.print("void 0");
|
output.print("void 0");
|
||||||
});
|
});
|
||||||
|
DEFPRINT(AST_Hole, function(self, output){
|
||||||
|
output.print("");
|
||||||
|
});
|
||||||
DEFPRINT(AST_Infinity, function(self, output){
|
DEFPRINT(AST_Infinity, function(self, output){
|
||||||
output.print("1/0");
|
output.print("1/0");
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1131,7 +1131,7 @@ function parse($TEXT, options) {
|
|||||||
if (first) first = false; else expect(",");
|
if (first) first = false; else expect(",");
|
||||||
if (allow_trailing_comma && is("punc", closing)) break;
|
if (allow_trailing_comma && is("punc", closing)) break;
|
||||||
if (is("punc", ",") && allow_empty) {
|
if (is("punc", ",") && allow_empty) {
|
||||||
a.push(new AST_Undefined({ start: S.token, end: S.token }));
|
a.push(new AST_Hole({ start: S.token, end: S.token }));
|
||||||
} else {
|
} else {
|
||||||
a.push(expression(false));
|
a.push(expression(false));
|
||||||
}
|
}
|
||||||
|
|||||||
12
test/compress/arrays.js
Normal file
12
test/compress/arrays.js
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
holes_and_undefined: {
|
||||||
|
input: {
|
||||||
|
x = [1, 2, undefined];
|
||||||
|
y = [1, , 2, ];
|
||||||
|
z = [1, undefined, 3];
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
x=[1,2,void 0];
|
||||||
|
y=[1,,2];
|
||||||
|
z=[1,void 0,3];
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user