Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
491f16c766 | ||
|
|
a30092e20f | ||
|
|
b1abe92e1a | ||
|
|
b454ce667e | ||
|
|
32283a0def | ||
|
|
ac51d4c5a0 | ||
|
|
0432a7abb9 | ||
|
|
f3a1694a41 | ||
|
|
2e0dc97003 | ||
|
|
701035621d | ||
|
|
79334dda10 | ||
|
|
e918748d88 |
8
.github/ISSUE_TEMPLATE.md
vendored
8
.github/ISSUE_TEMPLATE.md
vendored
@@ -1,7 +1,9 @@
|
||||
- Bug report or feature request?
|
||||
- Bug report or feature request? <!-- Note: sub-optimal but correct code is not a bug -->
|
||||
- `uglify-js` version (`uglifyjs -V`)
|
||||
- JavaScript input - ideally as small as possible.
|
||||
- The `uglifyjs` CLI command executed or `minify()` options used.
|
||||
- An example of JavaScript output produced and/or the error or warning.
|
||||
|
||||
Note: the release version of `uglify-js` only supports ES5. Those wishing to minify ES6 should use the experimental [`harmony`](https://github.com/mishoo/UglifyJS2#harmony) branch.
|
||||
<!--
|
||||
Note: the release version of uglify-js only supports ES5. Those wishing
|
||||
to minify ES6 should use the experimental harmony branch.
|
||||
-->
|
||||
|
||||
@@ -561,7 +561,7 @@ function getOptions(flag, constants) {
|
||||
|
||||
var ast;
|
||||
try {
|
||||
ast = UglifyJS.parse(x, { expression: true });
|
||||
ast = UglifyJS.parse(x, { cli: true, expression: true });
|
||||
} catch(ex) {
|
||||
if (ex instanceof UglifyJS.JS_Parse_Error) {
|
||||
print_error("Error parsing arguments for flag `" + flag + "': " + x);
|
||||
|
||||
@@ -592,9 +592,10 @@ merge(Compressor.prototype, {
|
||||
// Restrict var replacement to constants if side effects encountered.
|
||||
if (side_effects_encountered |= lvalues_encountered) continue;
|
||||
|
||||
var value_has_side_effects = var_decl.value.has_side_effects(compressor);
|
||||
// Non-constant single use vars can only be replaced in same scope.
|
||||
if (ref.scope !== self) {
|
||||
side_effects_encountered |= var_decl.value.has_side_effects(compressor);
|
||||
side_effects_encountered |= value_has_side_effects;
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -620,6 +621,7 @@ merge(Compressor.prototype, {
|
||||
|| (parent instanceof AST_If && node !== parent.condition)
|
||||
|| (parent instanceof AST_Conditional && node !== parent.condition)
|
||||
|| (node instanceof AST_SymbolRef
|
||||
&& value_has_side_effects
|
||||
&& !are_references_in_scope(node.definition(), self))
|
||||
|| (parent instanceof AST_Binary
|
||||
&& (parent.operator == "&&" || parent.operator == "||")
|
||||
@@ -1959,6 +1961,9 @@ merge(Compressor.prototype, {
|
||||
return in_list ? MAP.splice(body) : make_node(AST_BlockStatement, node, {
|
||||
body: body
|
||||
});
|
||||
} else if (is_empty(node.init)) {
|
||||
node.init = null;
|
||||
return node;
|
||||
}
|
||||
}
|
||||
if (node instanceof AST_Scope && node !== self)
|
||||
@@ -2932,7 +2937,12 @@ merge(Compressor.prototype, {
|
||||
return car;
|
||||
}
|
||||
if (cdr instanceof AST_Binary && !(cdr instanceof AST_Assign)) {
|
||||
field = cdr.left.is_constant() ? "right" : "left";
|
||||
if (cdr.left.is_constant()) {
|
||||
if (cdr.operator == "||" || cdr.operator == "&&") break;
|
||||
field = "right";
|
||||
} else {
|
||||
field = "left";
|
||||
}
|
||||
} else if (cdr instanceof AST_Call
|
||||
|| cdr instanceof AST_Unary && cdr.operator != "++" && cdr.operator != "--") {
|
||||
field = "expression";
|
||||
@@ -3284,6 +3294,7 @@ merge(Compressor.prototype, {
|
||||
left: self.left,
|
||||
right: self.right.expression
|
||||
});
|
||||
break;
|
||||
}
|
||||
// -a + b => b - a
|
||||
if (self.left instanceof AST_UnaryPrefix
|
||||
@@ -3295,6 +3306,7 @@ merge(Compressor.prototype, {
|
||||
left: self.right,
|
||||
right: self.left.expression
|
||||
});
|
||||
break;
|
||||
}
|
||||
case "*":
|
||||
associative = compressor.option("unsafe_math");
|
||||
@@ -3543,19 +3555,17 @@ merge(Compressor.prototype, {
|
||||
}
|
||||
var consequent = self.consequent;
|
||||
var alternative = self.alternative;
|
||||
// if (foo) exp = something; else exp = something_else;
|
||||
// |
|
||||
// v
|
||||
// exp = foo ? something : something_else;
|
||||
if (consequent instanceof AST_Assign
|
||||
&& alternative instanceof AST_Assign
|
||||
&& consequent.operator == alternative.operator
|
||||
&& consequent.left.equivalent_to(alternative.left)
|
||||
&& (!consequent.left.has_side_effects(compressor)
|
||||
|| !self.condition.has_side_effects(compressor))
|
||||
) {
|
||||
/*
|
||||
* Stuff like this:
|
||||
* if (foo) exp = something; else exp = something_else;
|
||||
* ==>
|
||||
* exp = foo ? something : something_else;
|
||||
*/
|
||||
&& (!self.condition.has_side_effects(compressor)
|
||||
|| consequent.operator == "="
|
||||
&& !consequent.left.has_side_effects(compressor))) {
|
||||
return make_node(AST_Assign, self, {
|
||||
operator: consequent.operator,
|
||||
left: consequent.left,
|
||||
|
||||
@@ -799,7 +799,7 @@ function OutputStream(options) {
|
||||
output.print("for");
|
||||
output.space();
|
||||
output.with_parens(function(){
|
||||
if (self.init && !(self.init instanceof AST_EmptyStatement)) {
|
||||
if (self.init) {
|
||||
if (self.init instanceof AST_Definitions) {
|
||||
self.init.print(output);
|
||||
} else {
|
||||
|
||||
@@ -695,6 +695,7 @@ function parse($TEXT, options) {
|
||||
html5_comments : true,
|
||||
bare_returns : false,
|
||||
shebang : true,
|
||||
cli : false,
|
||||
});
|
||||
|
||||
var S = {
|
||||
@@ -1456,7 +1457,7 @@ function parse($TEXT, options) {
|
||||
|
||||
function make_unary(ctor, op, expr) {
|
||||
if ((op == "++" || op == "--") && !is_assignable(expr))
|
||||
croak("Invalid use of " + op + " operator");
|
||||
croak("Invalid use of " + op + " operator", null, ctor === AST_UnaryPrefix ? expr.start.col - 1 : null);
|
||||
return new ctor({ operator: op, expression: expr });
|
||||
};
|
||||
|
||||
@@ -1501,6 +1502,7 @@ function parse($TEXT, options) {
|
||||
};
|
||||
|
||||
function is_assignable(expr) {
|
||||
if (options.cli) return true;
|
||||
return expr instanceof AST_PropAccess || expr instanceof AST_SymbolRef;
|
||||
};
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
"homepage": "http://lisperator.net/uglifyjs",
|
||||
"author": "Mihai Bazon <mihai.bazon@gmail.com> (http://lisperator.net/)",
|
||||
"license": "BSD-2-Clause",
|
||||
"version": "2.8.15",
|
||||
"version": "2.8.16",
|
||||
"engines": {
|
||||
"node": ">=0.8.0"
|
||||
},
|
||||
|
||||
@@ -1522,3 +1522,71 @@ issue_1631_3: {
|
||||
}
|
||||
expect_stdout: "6"
|
||||
}
|
||||
|
||||
var_side_effects_1: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
}
|
||||
input: {
|
||||
var print = console.log.bind(console);
|
||||
function foo(x) {
|
||||
var twice = x * 2;
|
||||
print('Foo:', twice);
|
||||
}
|
||||
foo(10);
|
||||
}
|
||||
expect: {
|
||||
var print = console.log.bind(console);
|
||||
function foo(x) {
|
||||
print('Foo:', 2 * x);
|
||||
}
|
||||
foo(10);
|
||||
}
|
||||
expect_stdout: true
|
||||
}
|
||||
|
||||
var_side_effects_2: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
}
|
||||
input: {
|
||||
var print = console.log.bind(console);
|
||||
function foo(x) {
|
||||
var twice = x.y * 2;
|
||||
print('Foo:', twice);
|
||||
}
|
||||
foo({ y: 10 });
|
||||
}
|
||||
expect: {
|
||||
var print = console.log.bind(console);
|
||||
function foo(x) {
|
||||
var twice = 2 * x.y;
|
||||
print('Foo:', twice);
|
||||
}
|
||||
foo({ y: 10 });
|
||||
}
|
||||
expect_stdout: true
|
||||
}
|
||||
|
||||
var_side_effects_3: {
|
||||
options = {
|
||||
collapse_vars: true,
|
||||
pure_getters: true,
|
||||
}
|
||||
input: {
|
||||
var print = console.log.bind(console);
|
||||
function foo(x) {
|
||||
var twice = x.y * 2;
|
||||
print('Foo:', twice);
|
||||
}
|
||||
foo({ y: 10 });
|
||||
}
|
||||
expect: {
|
||||
var print = console.log.bind(console);
|
||||
function foo(x) {
|
||||
print('Foo:', 2 * x.y);
|
||||
}
|
||||
foo({ y: 10 });
|
||||
}
|
||||
expect_stdout: true
|
||||
}
|
||||
|
||||
@@ -893,3 +893,43 @@ equality_conditionals_true: {
|
||||
}
|
||||
expect_stdout: true
|
||||
}
|
||||
|
||||
issue_1645_1: {
|
||||
options = {
|
||||
conditionals: true,
|
||||
}
|
||||
input: {
|
||||
var a = 100, b = 10;
|
||||
(b = a) ? a++ + (b += a) ? b += a : b += a : b ^= a;
|
||||
console.log(a, b);
|
||||
}
|
||||
expect: {
|
||||
var a = 100, b = 10;
|
||||
(b = a) ? (a++ + (b += a), b += a) : b ^= a;
|
||||
console.log(a,b);
|
||||
}
|
||||
expect_stdout: true
|
||||
}
|
||||
|
||||
issue_1645_2: {
|
||||
options = {
|
||||
conditionals: true,
|
||||
}
|
||||
input: {
|
||||
var a = 0;
|
||||
function f() {
|
||||
return a++;
|
||||
}
|
||||
f() ? a += 2 : a += 4;
|
||||
console.log(a);
|
||||
}
|
||||
expect: {
|
||||
var a = 0;
|
||||
function f(){
|
||||
return a++;
|
||||
}
|
||||
f() ? a += 2 : a += 4;
|
||||
console.log(a);
|
||||
}
|
||||
expect_stdout: true
|
||||
}
|
||||
|
||||
@@ -791,3 +791,17 @@ issue_1583: {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
issue_1656: {
|
||||
options = {
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
beautify = {
|
||||
beautify: true,
|
||||
}
|
||||
input: {
|
||||
for(var a=0;;);
|
||||
}
|
||||
expect_exact: "for (;;) ;"
|
||||
}
|
||||
|
||||
@@ -789,3 +789,16 @@ unsafe_charAt_noop: {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
issue_1649: {
|
||||
options = {
|
||||
evaluate: true,
|
||||
}
|
||||
input: {
|
||||
console.log(-1 + -1);
|
||||
}
|
||||
expect: {
|
||||
console.log(-2);
|
||||
}
|
||||
expect_stdout: "-2";
|
||||
}
|
||||
|
||||
88
test/compress/issue-1639.js
Normal file
88
test/compress/issue-1639.js
Normal file
@@ -0,0 +1,88 @@
|
||||
|
||||
issue_1639_1: {
|
||||
options = {
|
||||
booleans: true,
|
||||
cascade: true,
|
||||
conditionals: true,
|
||||
evaluate: true,
|
||||
join_vars: true,
|
||||
loops: true,
|
||||
sequences: true,
|
||||
side_effects: true,
|
||||
}
|
||||
input: {
|
||||
var a = 100, b = 10;
|
||||
|
||||
var L1 = 5;
|
||||
while (--L1 > 0) {
|
||||
if ((--b), false) {
|
||||
if (b) {
|
||||
var ignore = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log(a, b);
|
||||
}
|
||||
expect: {
|
||||
for (var a = 100, b = 10, L1 = 5; --L1 > 0;)
|
||||
if (--b, !1) var ignore = 0;
|
||||
console.log(a, b);
|
||||
}
|
||||
expect_stdout: true
|
||||
}
|
||||
|
||||
issue_1639_2: {
|
||||
options = {
|
||||
booleans: true,
|
||||
cascade: true,
|
||||
conditionals: true,
|
||||
evaluate: true,
|
||||
join_vars: true,
|
||||
sequences: true,
|
||||
side_effects: true,
|
||||
}
|
||||
input: {
|
||||
var a = 100, b = 10;
|
||||
|
||||
function f19() {
|
||||
if (++a, false)
|
||||
if (a)
|
||||
if (++a);
|
||||
}
|
||||
f19();
|
||||
|
||||
console.log(a, b);
|
||||
}
|
||||
expect: {
|
||||
var a = 100, b = 10;
|
||||
function f19() {
|
||||
++a, 1;
|
||||
}
|
||||
f19(),
|
||||
console.log(a, b);
|
||||
}
|
||||
expect_stdout: true
|
||||
}
|
||||
|
||||
issue_1639_3: {
|
||||
options = {
|
||||
booleans: true,
|
||||
cascade: true,
|
||||
conditionals: true,
|
||||
evaluate: true,
|
||||
sequences: true,
|
||||
side_effects: true,
|
||||
}
|
||||
input: {
|
||||
var a = 100, b = 10;
|
||||
a++ && false && a ? 0 : 0;
|
||||
console.log(a, b);
|
||||
}
|
||||
expect: {
|
||||
var a = 100, b = 10;
|
||||
a++,
|
||||
console.log(a, b);
|
||||
}
|
||||
expect_stdout: true
|
||||
}
|
||||
45
test/compress/issue-1656.js
Normal file
45
test/compress/issue-1656.js
Normal file
@@ -0,0 +1,45 @@
|
||||
f7: {
|
||||
options = {
|
||||
booleans: true,
|
||||
cascade: true,
|
||||
collapse_vars: true,
|
||||
comparisons: true,
|
||||
conditionals: true,
|
||||
dead_code: true,
|
||||
drop_debugger: true,
|
||||
evaluate: true,
|
||||
hoist_funs: true,
|
||||
if_return: true,
|
||||
join_vars: true,
|
||||
loops: true,
|
||||
negate_iife: true,
|
||||
passes: 3,
|
||||
properties: true,
|
||||
reduce_vars: true,
|
||||
sequences: true,
|
||||
side_effects: true,
|
||||
toplevel: true,
|
||||
unused: true,
|
||||
}
|
||||
beautify = {
|
||||
beautify: true,
|
||||
}
|
||||
input: {
|
||||
var a = 100, b = 10;
|
||||
function f22464() {
|
||||
var brake146670 = 5;
|
||||
while (((b = a) ? !a : ~a ? null : b += a) && --brake146670 > 0) {
|
||||
}
|
||||
}
|
||||
f22464();
|
||||
console.log(a, b);
|
||||
}
|
||||
expect_exact: [
|
||||
"var a = 100, b = 10;",
|
||||
"",
|
||||
"!function() {",
|
||||
" for (;b = a, !1; ) ;",
|
||||
"}(), console.log(a, b);",
|
||||
]
|
||||
expect_stdout: true
|
||||
}
|
||||
@@ -440,3 +440,21 @@ issue_186_beautify_bracketize_ie8: {
|
||||
'}',
|
||||
]
|
||||
}
|
||||
|
||||
issue_1648: {
|
||||
options = {
|
||||
join_vars: true,
|
||||
loops: true,
|
||||
passes: 2,
|
||||
sequences: true,
|
||||
unused: true,
|
||||
}
|
||||
input: {
|
||||
function f() {
|
||||
x();
|
||||
var b = 1;
|
||||
while (1);
|
||||
}
|
||||
}
|
||||
expect_exact: "function f(){for(x();1;);}"
|
||||
}
|
||||
|
||||
@@ -30,7 +30,6 @@ booleans_global_defs: {
|
||||
expect: {
|
||||
console.log(!0);
|
||||
}
|
||||
expect_stdout: true
|
||||
}
|
||||
|
||||
condition_evaluate: {
|
||||
|
||||
1
test/input/invalid/assign_1.js
Normal file
1
test/input/invalid/assign_1.js
Normal file
@@ -0,0 +1 @@
|
||||
console.log(1 || 5--);
|
||||
1
test/input/invalid/assign_2.js
Normal file
1
test/input/invalid/assign_2.js
Normal file
@@ -0,0 +1 @@
|
||||
console.log(2 || (Math.random() /= 2));
|
||||
1
test/input/invalid/assign_3.js
Normal file
1
test/input/invalid/assign_3.js
Normal file
@@ -0,0 +1 @@
|
||||
console.log(3 || ++this);
|
||||
@@ -251,4 +251,59 @@ describe("bin/uglifyjs", function () {
|
||||
done();
|
||||
});
|
||||
});
|
||||
it("Should support hyphen as shorthand", function(done) {
|
||||
var command = uglifyjscmd + ' test/input/issue-1431/sample.js -m keep-fnames=true';
|
||||
|
||||
exec(command, function (err, stdout) {
|
||||
if (err) throw err;
|
||||
|
||||
assert.strictEqual(stdout, "function f(r){return function(){function n(n){return n*n}return r(n)}}function g(n){return n(1)+n(2)}console.log(f(g)()==5);\n");
|
||||
done();
|
||||
});
|
||||
});
|
||||
it("Should throw syntax error (5--)", function(done) {
|
||||
var command = uglifyjscmd + ' test/input/invalid/assign_1.js';
|
||||
|
||||
exec(command, function (err, stdout, stderr) {
|
||||
assert.ok(err);
|
||||
assert.strictEqual(stdout, "");
|
||||
assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [
|
||||
"Parse error at test/input/invalid/assign_1.js:1,18",
|
||||
"console.log(1 || 5--);",
|
||||
" ^",
|
||||
"SyntaxError: Invalid use of -- operator"
|
||||
].join("\n"));
|
||||
done();
|
||||
});
|
||||
});
|
||||
it("Should throw syntax error (Math.random() /= 2)", function(done) {
|
||||
var command = uglifyjscmd + ' test/input/invalid/assign_2.js';
|
||||
|
||||
exec(command, function (err, stdout, stderr) {
|
||||
assert.ok(err);
|
||||
assert.strictEqual(stdout, "");
|
||||
assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [
|
||||
"Parse error at test/input/invalid/assign_2.js:1,32",
|
||||
"console.log(2 || (Math.random() /= 2));",
|
||||
" ^",
|
||||
"SyntaxError: Invalid assignment"
|
||||
].join("\n"));
|
||||
done();
|
||||
});
|
||||
});
|
||||
it("Should throw syntax error (++this)", function(done) {
|
||||
var command = uglifyjscmd + ' test/input/invalid/assign_3.js';
|
||||
|
||||
exec(command, function (err, stdout, stderr) {
|
||||
assert.ok(err);
|
||||
assert.strictEqual(stdout, "");
|
||||
assert.strictEqual(stderr.split(/\n/).slice(0, 4).join("\n"), [
|
||||
"Parse error at test/input/invalid/assign_3.js:1,18",
|
||||
"console.log(3 || ++this);",
|
||||
" ^",
|
||||
"SyntaxError: Invalid use of ++ operator"
|
||||
].join("\n"));
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -5,7 +5,7 @@ var path = require("path");
|
||||
describe("minify() with input file globs", function() {
|
||||
it("minify() with one input file glob string.", function() {
|
||||
var result = Uglify.minify("test/input/issue-1242/foo.*");
|
||||
assert.strictEqual(result.code, 'function foo(o){var n=2*o;print("Foo:",n)}var print=console.log.bind(console);');
|
||||
assert.strictEqual(result.code, 'function foo(o){print("Foo:",2*o)}var print=console.log.bind(console);');
|
||||
});
|
||||
it("minify() with an array of one input file glob.", function() {
|
||||
var result = Uglify.minify([
|
||||
@@ -20,7 +20,7 @@ describe("minify() with input file globs", function() {
|
||||
], {
|
||||
compress: { toplevel: true }
|
||||
});
|
||||
assert.strictEqual(result.code, 'var print=console.log.bind(console),a=function(n){return 3*n}(3),b=function(n){return n/2}(12);print("qux",a,b),function(n){var o=2*n;print("Foo:",o)}(11);');
|
||||
assert.strictEqual(result.code, 'var print=console.log.bind(console),a=function(n){return 3*n}(3),b=function(n){return n/2}(12);print("qux",a,b),function(n){print("Foo:",2*n)}(11);');
|
||||
});
|
||||
it("should throw with non-matching glob string", function() {
|
||||
var glob = "test/input/issue-1242/blah.*";
|
||||
|
||||
@@ -105,6 +105,23 @@ function run_compress_tests() {
|
||||
function test_case(test) {
|
||||
log_test(test.name);
|
||||
U.base54.reset();
|
||||
var output_options = test.beautify || {};
|
||||
var expect;
|
||||
if (test.expect) {
|
||||
expect = make_code(as_toplevel(test.expect, test.mangle), output_options);
|
||||
} else {
|
||||
expect = test.expect_exact;
|
||||
}
|
||||
var input = as_toplevel(test.input, test.mangle);
|
||||
var input_code = make_code(input, output_options);
|
||||
var input_formatted = make_code(test.input, {
|
||||
beautify: true,
|
||||
quote_style: 3,
|
||||
keep_quoted_props: true
|
||||
});
|
||||
if (test.mangle_props) {
|
||||
input = U.mangle_properties(input, test.mangle_props);
|
||||
}
|
||||
var options = U.defaults(test.options, {
|
||||
warnings: false
|
||||
});
|
||||
@@ -117,22 +134,6 @@ function run_compress_tests() {
|
||||
if (!options.warnings) options.warnings = true;
|
||||
}
|
||||
var cmp = new U.Compressor(options, true);
|
||||
var output_options = test.beautify || {};
|
||||
var expect;
|
||||
if (test.expect) {
|
||||
expect = make_code(as_toplevel(test.expect, test.mangle), output_options);
|
||||
} else {
|
||||
expect = test.expect_exact;
|
||||
}
|
||||
var input = as_toplevel(test.input, test.mangle);
|
||||
var input_code = make_code(test.input, {
|
||||
beautify: true,
|
||||
quote_style: 3,
|
||||
keep_quoted_props: true
|
||||
});
|
||||
if (test.mangle_props) {
|
||||
input = U.mangle_properties(input, test.mangle_props);
|
||||
}
|
||||
var output = cmp.compress(input);
|
||||
output.figure_out_scope(test.mangle);
|
||||
if (test.mangle) {
|
||||
@@ -142,7 +143,7 @@ function run_compress_tests() {
|
||||
output = make_code(output, output_options);
|
||||
if (expect != output) {
|
||||
log("!!! failed\n---INPUT---\n{input}\n---OUTPUT---\n{output}\n---EXPECTED---\n{expected}\n\n", {
|
||||
input: input_code,
|
||||
input: input_formatted,
|
||||
output: output,
|
||||
expected: expect
|
||||
});
|
||||
@@ -155,7 +156,7 @@ function run_compress_tests() {
|
||||
var reparsed_ast = U.parse(output);
|
||||
} catch (ex) {
|
||||
log("!!! Test matched expected result but cannot parse output\n---INPUT---\n{input}\n---OUTPUT---\n{output}\n--REPARSE ERROR--\n{error}\n\n", {
|
||||
input: input_code,
|
||||
input: input_formatted,
|
||||
output: output,
|
||||
error: ex.toString(),
|
||||
});
|
||||
@@ -174,7 +175,7 @@ function run_compress_tests() {
|
||||
var actual_warnings = JSON.stringify(warnings_emitted);
|
||||
if (expected_warnings != actual_warnings) {
|
||||
log("!!! failed\n---INPUT---\n{input}\n---EXPECTED WARNINGS---\n{expected_warnings}\n---ACTUAL WARNINGS---\n{actual_warnings}\n\n", {
|
||||
input: input_code,
|
||||
input: input_formatted,
|
||||
expected_warnings: expected_warnings,
|
||||
actual_warnings: actual_warnings,
|
||||
});
|
||||
@@ -183,13 +184,13 @@ function run_compress_tests() {
|
||||
}
|
||||
}
|
||||
if (test.expect_stdout) {
|
||||
var stdout = run_code(make_code(input, output_options));
|
||||
var stdout = run_code(input_code);
|
||||
if (test.expect_stdout === true) {
|
||||
test.expect_stdout = stdout;
|
||||
}
|
||||
if (!same_stdout(test.expect_stdout, stdout)) {
|
||||
log("!!! Invalid input or expected stdout\n---INPUT---\n{input}\n---EXPECTED {expected_type}---\n{expected}\n---ACTUAL {actual_type}---\n{actual}\n\n", {
|
||||
input: input_code,
|
||||
input: input_formatted,
|
||||
expected_type: typeof test.expect_stdout == "string" ? "STDOUT" : "ERROR",
|
||||
expected: test.expect_stdout,
|
||||
actual_type: typeof stdout == "string" ? "STDOUT" : "ERROR",
|
||||
@@ -201,7 +202,7 @@ function run_compress_tests() {
|
||||
stdout = run_code(output);
|
||||
if (!same_stdout(test.expect_stdout, stdout)) {
|
||||
log("!!! failed\n---INPUT---\n{input}\n---EXPECTED {expected_type}---\n{expected}\n---ACTUAL {actual_type}---\n{actual}\n\n", {
|
||||
input: input_code,
|
||||
input: input_formatted,
|
||||
expected_type: typeof test.expect_stdout == "string" ? "STDOUT" : "ERROR",
|
||||
expected: test.expect_stdout,
|
||||
actual_type: typeof stdout == "string" ? "STDOUT" : "ERROR",
|
||||
|
||||
225
test/ufuzz.js
Normal file
225
test/ufuzz.js
Normal file
@@ -0,0 +1,225 @@
|
||||
// ufuzz.js
|
||||
// derived from https://github.com/qfox/uglyfuzzer by Peter van der Zee
|
||||
"use strict";
|
||||
|
||||
// workaround for tty output truncation upon process.exit()
|
||||
[process.stdout, process.stderr].forEach(function(stream){
|
||||
if (stream._handle && stream._handle.setBlocking)
|
||||
stream._handle.setBlocking(true);
|
||||
});
|
||||
|
||||
var vm = require("vm");
|
||||
var minify = require("..").minify;
|
||||
|
||||
function run_code(code) {
|
||||
var stdout = "";
|
||||
var original_write = process.stdout.write;
|
||||
process.stdout.write = function(chunk) {
|
||||
stdout += chunk;
|
||||
};
|
||||
try {
|
||||
new vm.Script(code).runInNewContext({ console: console }, { timeout: 5000 });
|
||||
return stdout;
|
||||
} catch (ex) {
|
||||
return ex;
|
||||
} finally {
|
||||
process.stdout.write = original_write;
|
||||
}
|
||||
}
|
||||
|
||||
function rng(max) {
|
||||
return Math.floor(max * Math.random());
|
||||
}
|
||||
|
||||
function createFunctionDecls(n, recurmax) {
|
||||
if (--recurmax < 0) { return ';'; }
|
||||
var s = '';
|
||||
while (--n > 0) {
|
||||
s += createFunctionDecl(recurmax) + '\n';
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
var funcs = 0;
|
||||
function createFunctionDecl(recurmax) {
|
||||
if (--recurmax < 0) { return ';'; }
|
||||
var func = funcs++;
|
||||
return 'function f' + func + '(){' + createStatements(3, recurmax) + '}\nf' + func + '();';
|
||||
}
|
||||
|
||||
function createStatements(n, recurmax) {
|
||||
if (--recurmax < 0) { return ';'; }
|
||||
var s = '';
|
||||
while (--n > 0) {
|
||||
s += createStatement(recurmax);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
var loops = 0;
|
||||
function createStatement(recurmax) {
|
||||
var loop = ++loops;
|
||||
if (--recurmax < 0) { return ';'; }
|
||||
switch (rng(7)) {
|
||||
case 0:
|
||||
return '{' + createStatement(recurmax) + '}';
|
||||
case 1:
|
||||
return 'if (' + createExpression(recurmax) + ')' + createStatement(recurmax);
|
||||
case 2:
|
||||
return '{var brake' + loop + ' = 5; do {' + createStatement(recurmax) + '} while ((' + createExpression(recurmax) + ') && --brake' + loop + ' > 0);}';
|
||||
case 3:
|
||||
return '{var brake' + loop + ' = 5; while ((' + createExpression(recurmax) + ') && --brake' + loop + ' > 0)' + createStatement(recurmax) + '}';
|
||||
case 4:
|
||||
return 'for (var brake' + loop + ' = 5; (' + createExpression(recurmax) + ') && brake' + loop + ' > 0; --brake' + loop + ')' + createStatement(recurmax);
|
||||
case 5:
|
||||
return ';';
|
||||
case 6:
|
||||
return createExpression() + ';';
|
||||
}
|
||||
}
|
||||
|
||||
function createExpression(recurmax) {
|
||||
if (--recurmax < 0) { return '0'; }
|
||||
switch (rng(8)) {
|
||||
case 0:
|
||||
return '(' + createUnaryOp() + 'a)';
|
||||
case 1:
|
||||
return '(a' + (Math.random() > 0.5 ? '++' : '--') + ')';
|
||||
case 2:
|
||||
return '(b ' + createAssignment() + ' a)';
|
||||
case 3:
|
||||
return '(' + Math.random() + ' > 0.5 ? a : b)';
|
||||
case 4:
|
||||
return createExpression(recurmax) + createBinaryOp() + createExpression(recurmax);
|
||||
case 5:
|
||||
return createValue();
|
||||
case 6:
|
||||
return '(' + createExpression(recurmax) + ')';
|
||||
case 7:
|
||||
return createExpression(recurmax) + '?(' + createExpression(recurmax) + '):(' + createExpression(recurmax) + ')';
|
||||
}
|
||||
}
|
||||
|
||||
function createValue() {
|
||||
var values = [
|
||||
'true',
|
||||
'false',
|
||||
'22',
|
||||
'0',
|
||||
'(-1)',
|
||||
'NaN',
|
||||
'undefined',
|
||||
'null',
|
||||
'"foo"',
|
||||
'"bar"' ];
|
||||
return values[rng(values.length)];
|
||||
}
|
||||
|
||||
function createBinaryOp() {
|
||||
switch (rng(6)) {
|
||||
case 0:
|
||||
return '+';
|
||||
case 1:
|
||||
return '-';
|
||||
case 2:
|
||||
return ',';
|
||||
case 3:
|
||||
return '&&';
|
||||
case 4:
|
||||
return '||';
|
||||
case 5:
|
||||
return '^';
|
||||
}
|
||||
}
|
||||
|
||||
function createAssignment() {
|
||||
switch (rng(4)) {
|
||||
case 0:
|
||||
return '=';
|
||||
case 1:
|
||||
return '-=';
|
||||
case 2:
|
||||
return '^=';
|
||||
case 3:
|
||||
return '+=';
|
||||
}
|
||||
}
|
||||
|
||||
function createUnaryOp() {
|
||||
switch (rng(4)) {
|
||||
case 0:
|
||||
return '--';
|
||||
case 1:
|
||||
return '++';
|
||||
case 2:
|
||||
return '~';
|
||||
case 3:
|
||||
return '!';
|
||||
}
|
||||
}
|
||||
|
||||
function log() {
|
||||
console.log("//=============================================================");
|
||||
console.log("// original code");
|
||||
console.log("//");
|
||||
console.log(original_code);
|
||||
console.log();
|
||||
console.log();
|
||||
console.log("//-------------------------------------------------------------");
|
||||
console.log("// original code (beautify'd)");
|
||||
console.log("//");
|
||||
console.log(beautify_code);
|
||||
console.log();
|
||||
console.log();
|
||||
console.log("//-------------------------------------------------------------");
|
||||
console.log("// uglified code");
|
||||
console.log("//");
|
||||
console.log(uglify_code);
|
||||
console.log();
|
||||
console.log();
|
||||
console.log("original result:");
|
||||
console.log(original_result);
|
||||
console.log("beautified result:");
|
||||
console.log(beautify_result);
|
||||
console.log("uglified result:");
|
||||
console.log(uglify_result);
|
||||
}
|
||||
|
||||
var num_iterations = +process.argv[2] || 1/0;
|
||||
var verbose = !!process.argv[3];
|
||||
for (var round = 0; round < num_iterations; round++) {
|
||||
process.stdout.write(round + " of " + num_iterations + "\r");
|
||||
var original_code = [
|
||||
"var a = 100, b = 10;",
|
||||
createFunctionDecls(rng(3) + 1, 10),
|
||||
"console.log(a, b);"
|
||||
].join("\n");
|
||||
var beautify_code = minify(original_code, {
|
||||
fromString: true,
|
||||
mangle: false,
|
||||
compress: false,
|
||||
output: {
|
||||
beautify: true,
|
||||
bracketize: true,
|
||||
},
|
||||
}).code;
|
||||
|
||||
var uglify_code = minify(beautify_code, {
|
||||
fromString: true,
|
||||
mangle: false,
|
||||
compress: {
|
||||
passes: 3,
|
||||
},
|
||||
output: {
|
||||
beautify: true,
|
||||
bracketize: true,
|
||||
},
|
||||
}).code;
|
||||
|
||||
var original_result = run_code(original_code);
|
||||
var beautify_result = run_code(beautify_code);
|
||||
var uglify_result = run_code(uglify_code);
|
||||
var ok = original_result == beautify_result && original_result == uglify_result;
|
||||
if (verbose || !ok) log();
|
||||
if (!ok) process.exit(1);
|
||||
}
|
||||
Reference in New Issue
Block a user