2016-06-06 23:33:13 +00:00
|
|
|
var assert = require("assert");
|
2018-06-21 17:04:15 +00:00
|
|
|
var UglifyJS = require("../..");
|
2016-06-06 23:33:13 +00:00
|
|
|
|
2018-04-04 20:12:04 +00:00
|
|
|
describe("parentheses", function() {
|
2016-06-12 15:34:05 +00:00
|
|
|
it("Should add trailing parentheses for new expressions with zero arguments in beautify mode", function() {
|
2016-06-06 23:33:13 +00:00
|
|
|
var tests = [
|
|
|
|
|
"new x(1);",
|
2016-06-10 13:42:55 +00:00
|
|
|
"new x;",
|
|
|
|
|
"new new x;",
|
2016-06-06 23:33:13 +00:00
|
|
|
"new (function(foo){this.foo=foo;})(1);",
|
2016-06-10 13:42:55 +00:00
|
|
|
"new (function(foo){this.foo=foo;})();",
|
|
|
|
|
"new (function test(foo){this.foo=foo;})(1);",
|
|
|
|
|
"new (function test(foo){this.foo=foo;})();",
|
2016-06-06 23:33:13 +00:00
|
|
|
"new true;",
|
|
|
|
|
"new (0);",
|
|
|
|
|
"new (!0);",
|
2016-06-10 13:42:55 +00:00
|
|
|
"new (bar = function(foo) {this.foo=foo;})(123);",
|
|
|
|
|
"new (bar = function(foo) {this.foo=foo;})();"
|
2016-06-06 23:33:13 +00:00
|
|
|
];
|
|
|
|
|
var expected = [
|
|
|
|
|
"new x(1);",
|
2016-06-12 15:34:05 +00:00
|
|
|
"new x();",
|
|
|
|
|
"new new x()();",
|
2016-06-06 23:33:13 +00:00
|
|
|
"new function(foo) {\n this.foo = foo;\n}(1);",
|
2016-06-12 15:34:05 +00:00
|
|
|
"new function(foo) {\n this.foo = foo;\n}();",
|
2016-06-10 13:42:55 +00:00
|
|
|
"new function test(foo) {\n this.foo = foo;\n}(1);",
|
2016-06-12 15:34:05 +00:00
|
|
|
"new function test(foo) {\n this.foo = foo;\n}();",
|
|
|
|
|
"new true();",
|
|
|
|
|
"new 0();",
|
|
|
|
|
"new (!0)();",
|
2016-06-10 13:42:55 +00:00
|
|
|
"new (bar = function(foo) {\n this.foo = foo;\n})(123);",
|
2016-06-12 15:34:05 +00:00
|
|
|
"new (bar = function(foo) {\n this.foo = foo;\n})();"
|
2016-06-06 23:33:13 +00:00
|
|
|
];
|
|
|
|
|
for (var i = 0; i < tests.length; i++) {
|
|
|
|
|
assert.strictEqual(
|
2018-06-21 17:04:15 +00:00
|
|
|
UglifyJS.minify(tests[i], {
|
2016-06-06 23:33:13 +00:00
|
|
|
output: {beautify: true},
|
|
|
|
|
compress: false,
|
|
|
|
|
mangle: false
|
|
|
|
|
}).code,
|
|
|
|
|
expected[i]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
2016-06-12 15:34:05 +00:00
|
|
|
|
|
|
|
|
it("Should not add trailing parentheses for new expressions with zero arguments in non-beautify mode", function() {
|
|
|
|
|
var tests = [
|
|
|
|
|
"new x(1);",
|
|
|
|
|
"new x;",
|
|
|
|
|
"new new x;",
|
|
|
|
|
"new (function(foo){this.foo=foo;})(1);",
|
|
|
|
|
"new (function(foo){this.foo=foo;})();",
|
|
|
|
|
"new (function test(foo){this.foo=foo;})(1);",
|
|
|
|
|
"new (function test(foo){this.foo=foo;})();",
|
|
|
|
|
"new true;",
|
|
|
|
|
"new (0);",
|
|
|
|
|
"new (!0);",
|
|
|
|
|
"new (bar = function(foo) {this.foo=foo;})(123);",
|
|
|
|
|
"new (bar = function(foo) {this.foo=foo;})();"
|
|
|
|
|
];
|
|
|
|
|
var expected = [
|
|
|
|
|
"new x(1);",
|
|
|
|
|
"new x;",
|
|
|
|
|
"new(new x);",
|
|
|
|
|
"new function(foo){this.foo=foo}(1);",
|
|
|
|
|
"new function(foo){this.foo=foo};",
|
|
|
|
|
"new function test(foo){this.foo=foo}(1);",
|
|
|
|
|
"new function test(foo){this.foo=foo};",
|
|
|
|
|
"new true;",
|
|
|
|
|
"new 0;",
|
|
|
|
|
"new(!0);",
|
|
|
|
|
"new(bar=function(foo){this.foo=foo})(123);",
|
|
|
|
|
"new(bar=function(foo){this.foo=foo});"
|
|
|
|
|
];
|
|
|
|
|
for (var i = 0; i < tests.length; i++) {
|
|
|
|
|
assert.strictEqual(
|
2018-06-21 17:04:15 +00:00
|
|
|
UglifyJS.minify(tests[i], {
|
2016-06-12 15:34:05 +00:00
|
|
|
output: {beautify: false},
|
|
|
|
|
compress: false,
|
|
|
|
|
mangle: false
|
|
|
|
|
}).code,
|
|
|
|
|
expected[i]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
2018-04-04 20:12:04 +00:00
|
|
|
|
2021-02-23 21:57:11 +00:00
|
|
|
it("Should compress leading parentheses with reasonable performance", function() {
|
2018-04-04 20:12:04 +00:00
|
|
|
this.timeout(30000);
|
|
|
|
|
var code = [
|
|
|
|
|
"({}?0:1)&&x();",
|
|
|
|
|
"(function(){}).name;",
|
|
|
|
|
];
|
|
|
|
|
for (var i = 16; --i >= 0;) {
|
2018-04-26 07:02:17 +00:00
|
|
|
code = code.concat(code);
|
2018-04-04 20:12:04 +00:00
|
|
|
}
|
|
|
|
|
code = code.join("");
|
2018-06-21 17:04:15 +00:00
|
|
|
var result = UglifyJS.minify(code, {
|
2018-04-04 20:12:04 +00:00
|
|
|
compress: false,
|
|
|
|
|
mangle: false,
|
|
|
|
|
});
|
|
|
|
|
if (result.error) throw result.error;
|
|
|
|
|
// Dismal performance for `assert.strictEqual()` in Node.js 6
|
|
|
|
|
assert.ok(result.code === code);
|
|
|
|
|
});
|
|
|
|
|
});
|