improve mocha tests (#3195)

This commit is contained in:
Alex Lam S.L
2018-06-22 01:04:15 +08:00
committed by alexlamsl
parent 766a4147d4
commit 28330913d8
22 changed files with 562 additions and 581 deletions

View File

@@ -1,17 +1,14 @@
var UglifyJS = require("../node");
var assert = require("assert"); var assert = require("assert");
var UglifyJS = require("../..");
describe("arguments", function() { describe("arguments", function() {
it("Should known that arguments in functions are local scoped", function() { it("Should known that arguments in functions are local scoped", function() {
var ast = UglifyJS.parse("var arguments; var f = function() {arguments.length}"); var ast = UglifyJS.parse("var arguments; var f = function() {arguments.length}");
ast.figure_out_scope(); ast.figure_out_scope();
// Test scope of `var arguments` // Test scope of `var arguments`
assert.strictEqual(ast.find_variable("arguments").global, true); assert.strictEqual(ast.find_variable("arguments").global, true);
// Select arguments symbol in function // Select arguments symbol in function
var symbol = ast.body[1].definitions[0].value.find_variable("arguments"); var symbol = ast.body[1].definitions[0].value.find_variable("arguments");
assert.strictEqual(symbol.global, false); assert.strictEqual(symbol.global, false);
assert.strictEqual(symbol.scope, ast. // From ast assert.strictEqual(symbol.scope, ast. // From ast
body[1]. // Select 2nd statement (equals to `var f ...`) body[1]. // Select 2nd statement (equals to `var f ...`)
@@ -27,4 +24,4 @@ describe("arguments", function() {
assert.strictEqual(ast.body[0].body[0].uses_arguments, true); assert.strictEqual(ast.body[0].body[0].uses_arguments, true);
assert.strictEqual(ast.body[0].body[0].body[0].uses_arguments, false); assert.strictEqual(ast.body[0].body[0].body[0].uses_arguments, false);
}); });
}); });

View File

@@ -1,89 +0,0 @@
var UglifyJS = require("../node");
var assert = require("assert");
describe("comment filters", function() {
it("Should be able to filter comments by passing regexp", function() {
var ast = UglifyJS.parse("/*!test1*/\n/*test2*/\n//!test3\n//test4\n<!--test5\n<!--!test6\n-->test7\n-->!test8");
assert.strictEqual(ast.print_to_string({comments: /^!/}), "/*!test1*/\n//!test3\n//!test6\n//!test8\n");
});
it("Should be able to filter comments with the 'all' option", function() {
var ast = UglifyJS.parse("/*!test1*/\n/*test2*/\n//!test3\n//test4\n<!--test5\n<!--!test6\n-->test7\n-->!test8");
assert.strictEqual(ast.print_to_string({comments: "all"}), "/*!test1*/\n/*test2*/\n//!test3\n//test4\n//test5\n//!test6\n//test7\n//!test8\n");
});
it("Should be able to filter commments with the 'some' option", function() {
var ast = UglifyJS.parse("// foo\n/*@preserve*/\n// bar\n/*@license*/\n//@license with the wrong comment type\n/*@cc_on something*/");
assert.strictEqual(ast.print_to_string({comments: "some"}), "/*@preserve*/\n/*@license*/\n/*@cc_on something*/");
});
it("Should be able to filter comments by passing a function", function() {
var ast = UglifyJS.parse("/*TEST 123*/\n//An other comment\n//8 chars.");
var f = function(node, comment) {
return comment.value.length === 8;
};
assert.strictEqual(ast.print_to_string({comments: f}), "/*TEST 123*/\n//8 chars.\n");
});
it("Should be able to filter comments by passing regex in string format", function() {
var ast = UglifyJS.parse("/*!test1*/\n/*test2*/\n//!test3\n//test4\n<!--test5\n<!--!test6\n-->test7\n-->!test8");
assert.strictEqual(ast.print_to_string({comments: "/^!/"}), "/*!test1*/\n//!test3\n//!test6\n//!test8\n");
});
it("Should be able to get the comment and comment type when using a function", function() {
var ast = UglifyJS.parse("/*!test1*/\n/*test2*/\n//!test3\n//test4\n<!--test5\n<!--!test6\n-->test7\n-->!test8");
var f = function(node, comment) {
return comment.type == "comment1" || comment.type == "comment3";
};
assert.strictEqual(ast.print_to_string({comments: f}), "//!test3\n//test4\n//test5\n//!test6\n");
});
it("Should be able to filter comments by passing a boolean", function() {
var ast = UglifyJS.parse("/*!test1*/\n/*test2*/\n//!test3\n//test4\n<!--test5\n<!--!test6\n-->test7\n-->!test8");
assert.strictEqual(ast.print_to_string({comments: true}), "/*!test1*/\n/*test2*/\n//!test3\n//test4\n//test5\n//!test6\n//test7\n//!test8\n");
assert.strictEqual(ast.print_to_string({comments: false}), "");
});
it("Should never be able to filter comment5 (shebangs)", function() {
var ast = UglifyJS.parse("#!Random comment\n//test1\n/*test2*/");
var f = function(node, comment) {
assert.strictEqual(comment.type === "comment5", false);
return true;
};
assert.strictEqual(ast.print_to_string({comments: f}), "#!Random comment\n//test1\n/*test2*/");
});
it("Should never be able to filter comment5 when using 'some' as filter", function() {
var ast = UglifyJS.parse("#!foo\n//foo\n/*@preserve*/\n/* please hide me */");
assert.strictEqual(ast.print_to_string({comments: "some"}), "#!foo\n/*@preserve*/");
});
it("Should have no problem on multiple calls", function() {
const options = {
comments: /ok/
};
assert.strictEqual(UglifyJS.parse("/* ok */ function a(){}").print_to_string(options), "/* ok */function a(){}");
assert.strictEqual(UglifyJS.parse("/* ok */ function a(){}").print_to_string(options), "/* ok */function a(){}");
assert.strictEqual(UglifyJS.parse("/* ok */ function a(){}").print_to_string(options), "/* ok */function a(){}");
});
it("Should handle shebang and preamble correctly", function() {
var code = UglifyJS.minify("#!/usr/bin/node\nvar x = 10;", {
output: { preamble: "/* Build */" }
}).code;
assert.strictEqual(code, "#!/usr/bin/node\n/* Build */\nvar x=10;");
});
it("Should handle preamble without shebang correctly", function() {
var code = UglifyJS.minify("var x = 10;", {
output: { preamble: "/* Build */" }
}).code;
assert.strictEqual(code, "/* Build */\nvar x=10;");
});
});

View File

@@ -1,262 +0,0 @@
var assert = require("assert");
var uglify = require("../node");
describe("Comment", function() {
it("Should recognize eol of single line comments", function() {
var tests = [
"//Some comment 1\n>",
"//Some comment 2\r>",
"//Some comment 3\r\n>",
"//Some comment 4\u2028>",
"//Some comment 5\u2029>"
];
var fail = function(e) {
return e instanceof uglify.JS_Parse_Error &&
e.message === "Unexpected token: operator (>)" &&
e.line === 2 &&
e.col === 0;
}
for (var i = 0; i < tests.length; i++) {
assert.throws(function() {
uglify.parse(tests[i]);
}, fail, tests[i]);
}
});
it("Should update the position of a multiline comment correctly", function() {
var tests = [
"/*Some comment 1\n\n\n*/\n>\n\n\n\n\n\n",
"/*Some comment 2\r\n\r\n\r\n*/\r\n>\n\n\n\n\n\n",
"/*Some comment 3\r\r\r*/\r>\n\n\n\n\n\n",
"/*Some comment 4\u2028\u2028\u2028*/\u2028>\n\n\n\n\n\n",
"/*Some comment 5\u2029\u2029\u2029*/\u2029>\n\n\n\n\n\n"
];
var fail = function(e) {
return e instanceof uglify.JS_Parse_Error &&
e.message === "Unexpected token: operator (>)" &&
e.line === 5 &&
e.col === 0;
}
for (var i = 0; i < tests.length; i++) {
assert.throws(function() {
uglify.parse(tests[i]);
}, fail, tests[i]);
}
});
it("Should handle comment within return correctly", function() {
var result = uglify.minify([
"function unequal(x, y) {",
" return (",
" // Either one",
" x < y",
" ||",
" y < x",
" );",
"}",
].join("\n"), {
compress: false,
mangle: false,
output: {
beautify: true,
comments: "all",
},
});
if (result.error) throw result.error;
assert.strictEqual(result.code, [
"function unequal(x, y) {",
" // Either one",
" return x < y || y < x;",
"}",
].join("\n"));
});
it("Should handle comment folded into return correctly", function() {
var result = uglify.minify([
"function f() {",
" /* boo */ x();",
" return y();",
"}",
].join("\n"), {
mangle: false,
output: {
beautify: true,
comments: "all",
},
});
if (result.error) throw result.error;
assert.strictEqual(result.code, [
"function f() {",
" /* boo */",
" return x(), y();",
"}",
].join("\n"));
});
it("Should not drop comments after first OutputStream", function() {
var code = "/* boo */\nx();";
var ast = uglify.parse(code);
var out1 = uglify.OutputStream({
beautify: true,
comments: "all",
});
ast.print(out1);
var out2 = uglify.OutputStream({
beautify: true,
comments: "all",
});
ast.print(out2);
assert.strictEqual(out1.get(), code);
assert.strictEqual(out2.get(), out1.get());
});
it("Should retain trailing comments", function() {
var code = [
"if (foo /* lost comment */ && bar /* lost comment */) {",
" // this one is kept",
" {/* lost comment */}",
" !function() {",
" // lost comment",
" }();",
" function baz() {/* lost comment */}",
" // lost comment",
"}",
"// comments right before EOF are lost as well",
].join("\n");
var result = uglify.minify(code, {
compress: false,
mangle: false,
output: {
beautify: true,
comments: "all",
},
});
if (result.error) throw result.error;
assert.strictEqual(result.code, code);
});
it("Should retain comments within braces", function() {
var code = [
"{/* foo */}",
"a({/* foo */});",
"while (a) {/* foo */}",
"switch (a) {/* foo */}",
"if (a) {/* foo */} else {/* bar */}",
].join("\n\n");
var result = uglify.minify(code, {
compress: false,
mangle: false,
output: {
beautify: true,
comments: "all",
},
});
if (result.error) throw result.error;
assert.strictEqual(result.code, code);
});
it("Should correctly preserve new lines around comments", function() {
var tests = [
[
"// foo",
"// bar",
"x();",
].join("\n"),
[
"// foo",
"/* bar */",
"x();",
].join("\n"),
[
"// foo",
"/* bar */ x();",
].join("\n"),
[
"/* foo */",
"// bar",
"x();",
].join("\n"),
[
"/* foo */ // bar",
"x();",
].join("\n"),
[
"/* foo */",
"/* bar */",
"x();",
].join("\n"),
[
"/* foo */",
"/* bar */ x();",
].join("\n"),
[
"/* foo */ /* bar */",
"x();",
].join("\n"),
"/* foo */ /* bar */ x();",
].forEach(function(code) {
var result = uglify.minify(code, {
compress: false,
mangle: false,
output: {
beautify: true,
comments: "all",
},
});
if (result.error) throw result.error;
assert.strictEqual(result.code, code);
});
});
it("Should preserve new line before comment without beautify", function() {
var code = [
"function f(){",
"/* foo */bar()}",
].join("\n");
var result = uglify.minify(code, {
compress: false,
mangle: false,
output: {
comments: "all",
},
});
if (result.error) throw result.error;
assert.strictEqual(result.code, code);
});
it("Should preserve comments around IIFE", function() {
var result = uglify.minify("/*a*/(/*b*/function(){/*c*/}/*d*/)/*e*/();", {
compress: false,
mangle: false,
output: {
comments: "all",
},
});
if (result.error) throw result.error;
assert.strictEqual(result.code, "/*a*/ /*b*/(function(){/*c*/}/*d*/ /*e*/)();");
});
it("Should output line comments after statements", function() {
var result = uglify.minify([
"x()//foo",
"{y()//bar",
"}",
].join("\n"), {
compress: false,
mangle: false,
output: {
comments: "all",
},
});
if (result.error) throw result.error;
assert.strictEqual(result.code, [
"x();//foo",
"{y();//bar",
"}",
].join("\n"));
});
});

View File

@@ -1,22 +0,0 @@
var Uglify = require('../../');
var assert = require("assert");
describe("comment before constant", function() {
var js = 'function f() { /*c1*/ var /*c2*/ foo = /*c3*/ false; return foo; }';
it("Should test comment before constant is retained and output after mangle.", function() {
var result = Uglify.minify(js, {
compress: { collapse_vars: false, reduce_vars: false },
output: { comments: true },
});
assert.strictEqual(result.code, 'function f(){/*c1*/var/*c2*/n=/*c3*/!1;return n}');
});
it("Should test code works when comments disabled.", function() {
var result = Uglify.minify(js, {
compress: { collapse_vars: false, reduce_vars: false },
output: { comments: false },
});
assert.strictEqual(result.code, 'function f(){var n=!1;return n}');
});
});

380
test/mocha/comments.js Normal file
View File

@@ -0,0 +1,380 @@
var assert = require("assert");
var UglifyJS = require("../node");
describe("comments", function() {
it("Should recognize eol of single line comments", function() {
var tests = [
"//Some comment 1\n>",
"//Some comment 2\r>",
"//Some comment 3\r\n>",
"//Some comment 4\u2028>",
"//Some comment 5\u2029>"
];
var fail = function(e) {
return e instanceof UglifyJS.JS_Parse_Error
&& e.message === "Unexpected token: operator (>)"
&& e.line === 2
&& e.col === 0;
}
for (var i = 0; i < tests.length; i++) {
assert.throws(function() {
UglifyJS.parse(tests[i]);
}, fail, tests[i]);
}
});
it("Should update the position of a multiline comment correctly", function() {
var tests = [
"/*Some comment 1\n\n\n*/\n>\n\n\n\n\n\n",
"/*Some comment 2\r\n\r\n\r\n*/\r\n>\n\n\n\n\n\n",
"/*Some comment 3\r\r\r*/\r>\n\n\n\n\n\n",
"/*Some comment 4\u2028\u2028\u2028*/\u2028>\n\n\n\n\n\n",
"/*Some comment 5\u2029\u2029\u2029*/\u2029>\n\n\n\n\n\n"
];
var fail = function(e) {
return e instanceof UglifyJS.JS_Parse_Error
&& e.message === "Unexpected token: operator (>)"
&& e.line === 5
&& e.col === 0;
}
for (var i = 0; i < tests.length; i++) {
assert.throws(function() {
UglifyJS.parse(tests[i]);
}, fail, tests[i]);
}
});
it("Should handle comment within return correctly", function() {
var result = UglifyJS.minify([
"function unequal(x, y) {",
" return (",
" // Either one",
" x < y",
" ||",
" y < x",
" );",
"}",
].join("\n"), {
compress: false,
mangle: false,
output: {
beautify: true,
comments: "all",
},
});
if (result.error) throw result.error;
assert.strictEqual(result.code, [
"function unequal(x, y) {",
" // Either one",
" return x < y || y < x;",
"}",
].join("\n"));
});
it("Should handle comment folded into return correctly", function() {
var result = UglifyJS.minify([
"function f() {",
" /* boo */ x();",
" return y();",
"}",
].join("\n"), {
mangle: false,
output: {
beautify: true,
comments: "all",
},
});
if (result.error) throw result.error;
assert.strictEqual(result.code, [
"function f() {",
" /* boo */",
" return x(), y();",
"}",
].join("\n"));
});
it("Should not drop comments after first OutputStream", function() {
var code = "/* boo */\nx();";
var ast = UglifyJS.parse(code);
var out1 = UglifyJS.OutputStream({
beautify: true,
comments: "all",
});
ast.print(out1);
var out2 = UglifyJS.OutputStream({
beautify: true,
comments: "all",
});
ast.print(out2);
assert.strictEqual(out1.get(), code);
assert.strictEqual(out2.get(), out1.get());
});
it("Should retain trailing comments", function() {
var code = [
"if (foo /* lost comment */ && bar /* lost comment */) {",
" // this one is kept",
" {/* lost comment */}",
" !function() {",
" // lost comment",
" }();",
" function baz() {/* lost comment */}",
" // lost comment",
"}",
"// comments right before EOF are lost as well",
].join("\n");
var result = UglifyJS.minify(code, {
compress: false,
mangle: false,
output: {
beautify: true,
comments: "all",
},
});
if (result.error) throw result.error;
assert.strictEqual(result.code, code);
});
it("Should retain comments within braces", function() {
var code = [
"{/* foo */}",
"a({/* foo */});",
"while (a) {/* foo */}",
"switch (a) {/* foo */}",
"if (a) {/* foo */} else {/* bar */}",
].join("\n\n");
var result = UglifyJS.minify(code, {
compress: false,
mangle: false,
output: {
beautify: true,
comments: "all",
},
});
if (result.error) throw result.error;
assert.strictEqual(result.code, code);
});
it("Should correctly preserve new lines around comments", function() {
var tests = [
[
"// foo",
"// bar",
"x();",
].join("\n"),
[
"// foo",
"/* bar */",
"x();",
].join("\n"),
[
"// foo",
"/* bar */ x();",
].join("\n"),
[
"/* foo */",
"// bar",
"x();",
].join("\n"),
[
"/* foo */ // bar",
"x();",
].join("\n"),
[
"/* foo */",
"/* bar */",
"x();",
].join("\n"),
[
"/* foo */",
"/* bar */ x();",
].join("\n"),
[
"/* foo */ /* bar */",
"x();",
].join("\n"),
"/* foo */ /* bar */ x();",
].forEach(function(code) {
var result = UglifyJS.minify(code, {
compress: false,
mangle: false,
output: {
beautify: true,
comments: "all",
},
});
if (result.error) throw result.error;
assert.strictEqual(result.code, code);
});
});
it("Should preserve new line before comment without beautify", function() {
var code = [
"function f(){",
"/* foo */bar()}",
].join("\n");
var result = UglifyJS.minify(code, {
compress: false,
mangle: false,
output: {
comments: "all",
},
});
if (result.error) throw result.error;
assert.strictEqual(result.code, code);
});
it("Should preserve comments around IIFE", function() {
var result = UglifyJS.minify("/*a*/(/*b*/function(){/*c*/}/*d*/)/*e*/();", {
compress: false,
mangle: false,
output: {
comments: "all",
},
});
if (result.error) throw result.error;
assert.strictEqual(result.code, "/*a*/ /*b*/(function(){/*c*/}/*d*/ /*e*/)();");
});
it("Should output line comments after statements", function() {
var result = UglifyJS.minify([
"x()//foo",
"{y()//bar",
"}",
].join("\n"), {
compress: false,
mangle: false,
output: {
comments: "all",
},
});
if (result.error) throw result.error;
assert.strictEqual(result.code, [
"x();//foo",
"{y();//bar",
"}",
].join("\n"));
});
describe("comment before constant", function() {
var js = 'function f() { /*c1*/ var /*c2*/ foo = /*c3*/ false; return foo; }';
it("Should test comment before constant is retained and output after mangle.", function() {
var result = UglifyJS.minify(js, {
compress: { collapse_vars: false, reduce_vars: false },
output: { comments: true },
});
assert.strictEqual(result.code, 'function f(){/*c1*/var/*c2*/n=/*c3*/!1;return n}');
});
it("Should test code works when comments disabled.", function() {
var result = UglifyJS.minify(js, {
compress: { collapse_vars: false, reduce_vars: false },
output: { comments: false },
});
assert.strictEqual(result.code, 'function f(){var n=!1;return n}');
});
});
describe("comment filters", function() {
it("Should be able to filter comments by passing regexp", function() {
var ast = UglifyJS.parse("/*!test1*/\n/*test2*/\n//!test3\n//test4\n<!--test5\n<!--!test6\n-->test7\n-->!test8");
assert.strictEqual(ast.print_to_string({comments: /^!/}), "/*!test1*/\n//!test3\n//!test6\n//!test8\n");
});
it("Should be able to filter comments with the 'all' option", function() {
var ast = UglifyJS.parse("/*!test1*/\n/*test2*/\n//!test3\n//test4\n<!--test5\n<!--!test6\n-->test7\n-->!test8");
assert.strictEqual(ast.print_to_string({comments: "all"}), "/*!test1*/\n/*test2*/\n//!test3\n//test4\n//test5\n//!test6\n//test7\n//!test8\n");
});
it("Should be able to filter commments with the 'some' option", function() {
var ast = UglifyJS.parse("// foo\n/*@preserve*/\n// bar\n/*@license*/\n//@license with the wrong comment type\n/*@cc_on something*/");
assert.strictEqual(ast.print_to_string({comments: "some"}), "/*@preserve*/\n/*@license*/\n/*@cc_on something*/");
});
it("Should be able to filter comments by passing a function", function() {
var ast = UglifyJS.parse("/*TEST 123*/\n//An other comment\n//8 chars.");
var f = function(node, comment) {
return comment.value.length === 8;
};
assert.strictEqual(ast.print_to_string({comments: f}), "/*TEST 123*/\n//8 chars.\n");
});
it("Should be able to filter comments by passing regex in string format", function() {
var ast = UglifyJS.parse("/*!test1*/\n/*test2*/\n//!test3\n//test4\n<!--test5\n<!--!test6\n-->test7\n-->!test8");
assert.strictEqual(ast.print_to_string({comments: "/^!/"}), "/*!test1*/\n//!test3\n//!test6\n//!test8\n");
});
it("Should be able to get the comment and comment type when using a function", function() {
var ast = UglifyJS.parse("/*!test1*/\n/*test2*/\n//!test3\n//test4\n<!--test5\n<!--!test6\n-->test7\n-->!test8");
var f = function(node, comment) {
return comment.type == "comment1" || comment.type == "comment3";
};
assert.strictEqual(ast.print_to_string({comments: f}), "//!test3\n//test4\n//test5\n//!test6\n");
});
it("Should be able to filter comments by passing a boolean", function() {
var ast = UglifyJS.parse("/*!test1*/\n/*test2*/\n//!test3\n//test4\n<!--test5\n<!--!test6\n-->test7\n-->!test8");
assert.strictEqual(ast.print_to_string({comments: true}), "/*!test1*/\n/*test2*/\n//!test3\n//test4\n//test5\n//!test6\n//test7\n//!test8\n");
assert.strictEqual(ast.print_to_string({comments: false}), "");
});
it("Should never be able to filter comment5 (shebangs)", function() {
var ast = UglifyJS.parse("#!Random comment\n//test1\n/*test2*/");
var f = function(node, comment) {
assert.strictEqual(comment.type === "comment5", false);
return true;
};
assert.strictEqual(ast.print_to_string({comments: f}), "#!Random comment\n//test1\n/*test2*/");
});
it("Should never be able to filter comment5 when using 'some' as filter", function() {
var ast = UglifyJS.parse("#!foo\n//foo\n/*@preserve*/\n/* please hide me */");
assert.strictEqual(ast.print_to_string({comments: "some"}), "#!foo\n/*@preserve*/");
});
it("Should have no problem on multiple calls", function() {
const options = {
comments: /ok/
};
assert.strictEqual(UglifyJS.parse("/* ok */ function a(){}").print_to_string(options), "/* ok */function a(){}");
assert.strictEqual(UglifyJS.parse("/* ok */ function a(){}").print_to_string(options), "/* ok */function a(){}");
assert.strictEqual(UglifyJS.parse("/* ok */ function a(){}").print_to_string(options), "/* ok */function a(){}");
});
it("Should handle shebang and preamble correctly", function() {
var code = UglifyJS.minify("#!/usr/bin/node\nvar x = 10;", {
output: { preamble: "/* Build */" }
}).code;
assert.strictEqual(code, "#!/usr/bin/node\n/* Build */\nvar x=10;");
});
it("Should handle preamble without shebang correctly", function() {
var code = UglifyJS.minify("var x = 10;", {
output: { preamble: "/* Build */" }
}).code;
assert.strictEqual(code, "/* Build */\nvar x=10;");
});
});
describe("Huge number of comments.", function() {
it("Should parse and compress code with thousands of consecutive comments", function() {
var js = "function lots_of_comments(x) { return 7 -";
for (var i = 1; i <= 5000; ++i) js += "// " + i + "\n";
for (; i <= 10000; ++i) js += "/* " + i + " */ /**/";
js += "x; }";
var result = UglifyJS.minify(js, { mangle: false });
assert.strictEqual(result.code, "function lots_of_comments(x){return 7-x}");
});
});
});

View File

@@ -1,9 +1,9 @@
var assert = require("assert"); var assert = require("assert");
var uglify = require("../node"); var UglifyJS = require("../node");
describe("Directives", function() { describe("Directives", function() {
it ("Should allow tokenizer to store directives state", function() { it ("Should allow tokenizer to store directives state", function() {
var tokenizer = uglify.tokenizer("", "foo.js"); var tokenizer = UglifyJS.tokenizer("", "foo.js");
// Stack level 0 // Stack level 0
assert.strictEqual(tokenizer.has_directive("use strict"), false); assert.strictEqual(tokenizer.has_directive("use strict"), false);
@@ -161,13 +161,13 @@ describe("Directives", function() {
for (var i = 0; i < tests.length; i++) { for (var i = 0; i < tests.length; i++) {
// Fail parser deliberately to get state at failure // Fail parser deliberately to get state at failure
var tokenizer = uglify.tokenizer(tests[i].input + "]", "foo.js"); var tokenizer = UglifyJS.tokenizer(tests[i].input + "]", "foo.js");
try { try {
var parser = uglify.parse(tokenizer); var parser = UglifyJS.parse(tokenizer);
throw new Error("Expected parser to fail"); throw new Error("Expected parser to fail");
} catch (e) { } catch (e) {
assert.strictEqual(e instanceof uglify.JS_Parse_Error, true); assert.strictEqual(e instanceof UglifyJS.JS_Parse_Error, true);
assert.strictEqual(e.message, "Unexpected token: punc (])"); assert.strictEqual(e.message, "Unexpected token: punc (])");
} }
@@ -186,7 +186,7 @@ describe("Directives", function() {
["'tests';\n\n", true], ["'tests';\n\n", true],
["\n\n\"use strict\";\n\n", true] ["\n\n\"use strict\";\n\n", true]
].forEach(function(test) { ].forEach(function(test) {
var out = uglify.OutputStream(); var out = UglifyJS.OutputStream();
out.print(test[0]); out.print(test[0]);
out.print_string("", null, true); out.print_string("", null, true);
assert.strictEqual(out.get() === test[0] + ';""', test[1], test[0]); assert.strictEqual(out.get() === test[0] + ';""', test[1], test[0]);
@@ -195,7 +195,7 @@ describe("Directives", function() {
it("Should only print 2 semicolons spread over 2 lines in beautify mode", function() { it("Should only print 2 semicolons spread over 2 lines in beautify mode", function() {
assert.strictEqual( assert.strictEqual(
uglify.minify( UglifyJS.minify(
'"use strict";\'use strict\';"use strict";"use strict";;\'use strict\';console.log(\'use strict\');', '"use strict";\'use strict\';"use strict";"use strict";;\'use strict\';console.log(\'use strict\');',
{output: {beautify: true, quote_style: 3}, compress: false} {output: {beautify: true, quote_style: 3}, compress: false}
).code, ).code,
@@ -225,7 +225,7 @@ describe("Directives", function() {
for (var i = 0; i < tests.length; i++) { for (var i = 0; i < tests.length; i++) {
assert.strictEqual( assert.strictEqual(
uglify.minify(tests[i][0], {compress: false, mangle: false}).code, UglifyJS.minify(tests[i][0], {compress: false, mangle: false}).code,
tests[i][1], tests[i][1],
tests[i][0] tests[i][0]
); );
@@ -233,7 +233,7 @@ describe("Directives", function() {
}); });
it("Should add double semicolon when relying on automatic semicolon insertion", function() { it("Should add double semicolon when relying on automatic semicolon insertion", function() {
var code = uglify.minify('"use strict";"use\\x20strict";', var code = UglifyJS.minify('"use strict";"use\\x20strict";',
{output: {semicolons: false}, compress: false} {output: {semicolons: false}, compress: false}
).code; ).code;
assert.strictEqual(code, '"use strict";;"use strict"\n'); assert.strictEqual(code, '"use strict";;"use strict"\n');
@@ -340,7 +340,7 @@ describe("Directives", function() {
]; ];
for (var i = 0; i < tests.length; i++) { for (var i = 0; i < tests.length; i++) {
assert.strictEqual( assert.strictEqual(
uglify.minify(tests[i][0], {output:{quote_style: tests[i][1]}, compress: false}).code, UglifyJS.minify(tests[i][0], {output:{quote_style: tests[i][1]}, compress: false}).code,
tests[i][2], tests[i][2],
tests[i][0] + " using mode " + tests[i][1] tests[i][0] + " using mode " + tests[i][1]
); );
@@ -372,7 +372,7 @@ describe("Directives", function() {
for (var i = 0; i < tests.length; i++) { for (var i = 0; i < tests.length; i++) {
assert.strictEqual( assert.strictEqual(
uglify.minify(tests[i][0]).code, UglifyJS.minify(tests[i][0]).code,
tests[i][1], tests[i][1],
tests[i][0] tests[i][0]
); );

View File

@@ -1,5 +1,5 @@
var UglifyJS = require("../node");
var assert = require("assert"); var assert = require("assert");
var UglifyJS = require("../node");
describe("Getters and setters", function() { describe("Getters and setters", function() {
it("Should not accept operator symbols as getter/setter name", function() { it("Should not accept operator symbols as getter/setter name", function() {

View File

@@ -1,14 +0,0 @@
var Uglify = require('../../');
var assert = require("assert");
describe("Huge number of comments.", function() {
it("Should parse and compress code with thousands of consecutive comments", function() {
var js = 'function lots_of_comments(x) { return 7 -';
var i;
for (i = 1; i <= 5000; ++i) { js += "// " + i + "\n"; }
for (; i <= 10000; ++i) { js += "/* " + i + " */ /**/"; }
js += "x; }";
var result = Uglify.minify(js, { mangle: false });
assert.strictEqual(result.code, "function lots_of_comments(x){return 7-x}");
});
});

View File

@@ -1,10 +1,10 @@
var assert = require("assert"); var assert = require("assert");
var uglify = require("../../"); var UglifyJS = require("../..");
describe("ie8", function() { describe("ie8", function() {
it("Should be able to minify() with undefined as catch parameter in a try...catch statement", function() { it("Should be able to minify() with undefined as catch parameter in a try...catch statement", function() {
assert.strictEqual( assert.strictEqual(
uglify.minify([ UglifyJS.minify([
"function a(b){", "function a(b){",
" try {", " try {",
" throw 'Stuff';", " throw 'Stuff';",

View File

@@ -1,68 +0,0 @@
var assert = require("assert");
var Uglify = require("../../");
var SourceMapConsumer = require("source-map").SourceMapConsumer;
function getMap() {
return {
"version": 3,
"sources": ["index.js"],
"names": [],
"mappings": ";;AAAA,IAAI,MAAM,SAAN,GAAM;AAAA,SAAK,SAAS,CAAd;AAAA,CAAV;AACA,QAAQ,GAAR,CAAY,IAAI,KAAJ,CAAZ",
"file": "bundle.js",
"sourcesContent": ["let foo = x => \"foo \" + x;\nconsole.log(foo(\"bar\"));"]
};
}
function prepareMap(sourceMap) {
var code = [
'"use strict";',
"",
"var foo = function foo(x) {",
' return "foo " + x;',
"};",
'console.log(foo("bar"));',
"",
"//# sourceMappingURL=bundle.js.map",
].join("\n");
var result = Uglify.minify(code, {
sourceMap: {
content: sourceMap,
includeSources: true,
}
});
if (result.error) throw result.error;
return new SourceMapConsumer(result.map);
}
describe("input sourcemaps", function() {
it("Should copy over original sourcesContent", function() {
var orig = getMap();
var map = prepareMap(orig);
assert.equal(map.sourceContentFor("index.js"), orig.sourcesContent[0]);
});
it("Should copy sourcesContent if sources are relative", function() {
var relativeMap = getMap();
relativeMap.sources = ['./index.js'];
var map = prepareMap(relativeMap);
assert.notEqual(map.sourcesContent, null);
assert.equal(map.sourcesContent.length, 1);
assert.equal(map.sourceContentFor("index.js"), relativeMap.sourcesContent[0]);
});
it("Should not have invalid mappings from inputSourceMap (issue #882)", function() {
var map = prepareMap(getMap());
// The original source has only 2 lines, check that mappings don't have more lines
var msg = "Mapping should not have higher line number than the original file had";
map.eachMapping(function(mapping) {
assert.ok(mapping.originalLine <= 2, msg);
});
map.allGeneratedPositionsFor({
source: "index.js",
line: 1,
column: 1
}).forEach(function(pos) {
assert.ok(pos.line <= 2, msg);
});
});
});

View File

@@ -1,5 +1,5 @@
var Uglify = require('../../');
var assert = require("assert"); var assert = require("assert");
var UglifyJS = require("../..");
describe("let", function() { describe("let", function() {
this.timeout(30000); this.timeout(30000);
@@ -10,7 +10,7 @@ describe("let", function() {
s += "var v" + i + "=0;"; s += "var v" + i + "=0;";
} }
s += '}'; s += '}';
var result = Uglify.minify(s, { var result = UglifyJS.minify(s, {
compress: false compress: false
}).code; }).code;
@@ -39,7 +39,7 @@ describe("let", function() {
for (var i = 0; i < 18000; i++) { for (var i = 0; i < 18000; i++) {
s += "v.b" + i + ";"; s += "v.b" + i + ";";
} }
var result = Uglify.minify(s, { var result = UglifyJS.minify(s, {
compress: false, compress: false,
ie8: true, ie8: true,
mangle: { mangle: {

View File

@@ -1,5 +1,5 @@
var Uglify = require("../node");
var assert = require("assert"); var assert = require("assert");
var UglifyJS = require("../node");
describe("line-endings", function() { describe("line-endings", function() {
var options = { var options = {
@@ -14,19 +14,19 @@ describe("line-endings", function() {
it("Should parse LF line endings", function() { it("Should parse LF line endings", function() {
var js = '/*!one\n2\n3*///comment\nfunction f(x) {\n if (x)\n//comment\n return 3;\n}\n'; var js = '/*!one\n2\n3*///comment\nfunction f(x) {\n if (x)\n//comment\n return 3;\n}\n';
var result = Uglify.minify(js, options); var result = UglifyJS.minify(js, options);
assert.strictEqual(result.code, expected_code); assert.strictEqual(result.code, expected_code);
}); });
it("Should parse CR/LF line endings", function() { it("Should parse CR/LF line endings", function() {
var js = '/*!one\r\n2\r\n3*///comment\r\nfunction f(x) {\r\n if (x)\r\n//comment\r\n return 3;\r\n}\r\n'; var js = '/*!one\r\n2\r\n3*///comment\r\nfunction f(x) {\r\n if (x)\r\n//comment\r\n return 3;\r\n}\r\n';
var result = Uglify.minify(js, options); var result = UglifyJS.minify(js, options);
assert.strictEqual(result.code, expected_code); assert.strictEqual(result.code, expected_code);
}); });
it("Should parse CR line endings", function() { it("Should parse CR line endings", function() {
var js = '/*!one\r2\r3*///comment\rfunction f(x) {\r if (x)\r//comment\r return 3;\r}\r'; var js = '/*!one\r2\r3*///comment\rfunction f(x) {\r if (x)\r//comment\r return 3;\r}\r';
var result = Uglify.minify(js, options); var result = UglifyJS.minify(js, options);
assert.strictEqual(result.code, expected_code); assert.strictEqual(result.code, expected_code);
}); });
@@ -44,16 +44,15 @@ describe("line-endings", function() {
] ]
var test = function(input) { var test = function(input) {
return function() { return function() {
Uglify.parse(input); UglifyJS.parse(input);
} }
} }
var fail = function(e) { var fail = function(e) {
return e instanceof Uglify.JS_Parse_Error && return e instanceof UglifyJS.JS_Parse_Error
e.message === "Unexpected line terminator"; && e.message === "Unexpected line terminator";
} }
for (var i = 0; i < inputs.length; i++) { for (var i = 0; i < inputs.length; i++) {
assert.throws(test(inputs[i]), fail); assert.throws(test(inputs[i]), fail);
} }
}); });
}); });

View File

@@ -1,22 +1,22 @@
var Uglify = require('../../');
var assert = require("assert"); var assert = require("assert");
var UglifyJS = require("../..");
describe("Input file as map", function() { describe("Input file as map", function() {
it("Should accept object", function() { it("Should accept object", function() {
var jsMap = { var jsMap = {
'/scripts/foo.js': 'var foo = {"x": 1, y: 2, \'z\': 3};' '/scripts/foo.js': 'var foo = {"x": 1, y: 2, \'z\': 3};'
}; };
var result = Uglify.minify(jsMap, {sourceMap: true}); var result = UglifyJS.minify(jsMap, {sourceMap: true});
var map = JSON.parse(result.map); var map = JSON.parse(result.map);
assert.strictEqual(result.code, 'var foo={x:1,y:2,z:3};'); assert.strictEqual(result.code, 'var foo={x:1,y:2,z:3};');
assert.deepEqual(map.sources, ['/scripts/foo.js']); assert.deepEqual(map.sources, ['/scripts/foo.js']);
assert.strictEqual(map.file, undefined); assert.strictEqual(map.file, undefined);
result = Uglify.minify(jsMap); result = UglifyJS.minify(jsMap);
assert.strictEqual(result.map, undefined); assert.strictEqual(result.map, undefined);
result = Uglify.minify(jsMap, {sourceMap: {filename: 'out.js'}}); result = UglifyJS.minify(jsMap, {sourceMap: {filename: 'out.js'}});
map = JSON.parse(result.map); map = JSON.parse(result.map);
assert.strictEqual(map.file, 'out.js'); assert.strictEqual(map.file, 'out.js');
}); });
@@ -26,7 +26,7 @@ describe("Input file as map", function() {
'var foo = {"x": 1, y: 2, \'z\': 3};', 'var foo = {"x": 1, y: 2, \'z\': 3};',
'var bar = 15;' 'var bar = 15;'
]; ];
var result = Uglify.minify(jsSeq, {sourceMap: true}); var result = UglifyJS.minify(jsSeq, {sourceMap: true});
var map = JSON.parse(result.map); var map = JSON.parse(result.map);
assert.strictEqual(result.code, 'var foo={x:1,y:2,z:3},bar=15;'); assert.strictEqual(result.code, 'var foo={x:1,y:2,z:3},bar=15;');
@@ -37,7 +37,7 @@ describe("Input file as map", function() {
var jsMap = { var jsMap = {
'/scripts/foo.js': 'var foo = {"x": 1, y: 2, \'z\': 3};' '/scripts/foo.js': 'var foo = {"x": 1, y: 2, \'z\': 3};'
}; };
var result = Uglify.minify(jsMap, {sourceMap: {includeSources: true}}); var result = UglifyJS.minify(jsMap, {sourceMap: {includeSources: true}});
var map = JSON.parse(result.map); var map = JSON.parse(result.map);
assert.strictEqual(result.code, 'var foo={x:1,y:2,z:3};'); assert.strictEqual(result.code, 'var foo={x:1,y:2,z:3};');

View File

@@ -1,7 +1,7 @@
var Uglify = require('../../');
var assert = require("assert"); var assert = require("assert");
var readFileSync = require("fs").readFileSync; var readFileSync = require("fs").readFileSync;
var run_code = require("../sandbox").run_code; var run_code = require("../sandbox").run_code;
var UglifyJS = require("../../");
function read(path) { function read(path) {
return readFileSync(path, "utf8"); return readFileSync(path, "utf8");
@@ -10,14 +10,14 @@ function read(path) {
describe("minify", function() { describe("minify", function() {
it("Should test basic sanity of minify with default options", function() { it("Should test basic sanity of minify with default options", function() {
var js = 'function foo(bar) { if (bar) return 3; else return 7; var u = not_called(); }'; var js = 'function foo(bar) { if (bar) return 3; else return 7; var u = not_called(); }';
var result = Uglify.minify(js); var result = UglifyJS.minify(js);
assert.strictEqual(result.code, 'function foo(n){return n?3:7}'); assert.strictEqual(result.code, 'function foo(n){return n?3:7}');
}); });
it("Should skip inherited keys from `files`", function() { it("Should skip inherited keys from `files`", function() {
var files = Object.create({ skip: this }); var files = Object.create({ skip: this });
files[0] = "alert(1 + 1)"; files[0] = "alert(1 + 1)";
var result = Uglify.minify(files); var result = UglifyJS.minify(files);
assert.strictEqual(result.code, "alert(2);"); assert.strictEqual(result.code, "alert(2);");
}); });
@@ -32,7 +32,7 @@ describe("minify", function() {
"qux.js", "qux.js",
].forEach(function(file) { ].forEach(function(file) {
var code = read("test/input/issue-1242/" + file); var code = read("test/input/issue-1242/" + file);
var result = Uglify.minify(code, { var result = UglifyJS.minify(code, {
mangle: { mangle: {
cache: cache, cache: cache,
toplevel: true toplevel: true
@@ -65,7 +65,7 @@ describe("minify", function() {
"qux.js", "qux.js",
].forEach(function(file) { ].forEach(function(file) {
var code = read("test/input/issue-1242/" + file); var code = read("test/input/issue-1242/" + file);
var result = Uglify.minify(code, { var result = UglifyJS.minify(code, {
mangle: { mangle: {
toplevel: true toplevel: true
}, },
@@ -96,7 +96,7 @@ describe("minify", function() {
'"xxyyy";var j={t:2,u:3},k=4;', '"xxyyy";var j={t:2,u:3},k=4;',
'console.log(i.s,j.t,j.u,k);', 'console.log(i.s,j.t,j.u,k);',
].forEach(function(code) { ].forEach(function(code) {
var result = Uglify.minify(code, { var result = UglifyJS.minify(code, {
compress: false, compress: false,
mangle: { mangle: {
properties: true, properties: true,
@@ -117,15 +117,15 @@ describe("minify", function() {
}); });
it("Should not parse invalid use of reserved words", function() { it("Should not parse invalid use of reserved words", function() {
assert.strictEqual(Uglify.minify("function enum(){}").error, undefined); assert.strictEqual(UglifyJS.minify("function enum(){}").error, undefined);
assert.strictEqual(Uglify.minify("function static(){}").error, undefined); assert.strictEqual(UglifyJS.minify("function static(){}").error, undefined);
assert.strictEqual(Uglify.minify("function this(){}").error.message, "Unexpected token: name (this)"); assert.strictEqual(UglifyJS.minify("function this(){}").error.message, "Unexpected token: name (this)");
}); });
describe("keep_quoted_props", function() { describe("keep_quoted_props", function() {
it("Should preserve quotes in object literals", function() { it("Should preserve quotes in object literals", function() {
var js = 'var foo = {"x": 1, y: 2, \'z\': 3};'; var js = 'var foo = {"x": 1, y: 2, \'z\': 3};';
var result = Uglify.minify(js, { var result = UglifyJS.minify(js, {
output: { output: {
keep_quoted_props: true keep_quoted_props: true
}}); }});
@@ -134,7 +134,7 @@ describe("minify", function() {
it("Should preserve quote styles when quote_style is 3", function() { it("Should preserve quote styles when quote_style is 3", function() {
var js = 'var foo = {"x": 1, y: 2, \'z\': 3};'; var js = 'var foo = {"x": 1, y: 2, \'z\': 3};';
var result = Uglify.minify(js, { var result = UglifyJS.minify(js, {
output: { output: {
keep_quoted_props: true, keep_quoted_props: true,
quote_style: 3 quote_style: 3
@@ -144,7 +144,7 @@ describe("minify", function() {
it("Should not preserve quotes in object literals when disabled", function() { it("Should not preserve quotes in object literals when disabled", function() {
var js = 'var foo = {"x": 1, y: 2, \'z\': 3};'; var js = 'var foo = {"x": 1, y: 2, \'z\': 3};';
var result = Uglify.minify(js, { var result = UglifyJS.minify(js, {
output: { output: {
keep_quoted_props: false, keep_quoted_props: false,
quote_style: 3 quote_style: 3
@@ -156,7 +156,7 @@ describe("minify", function() {
describe("mangleProperties", function() { describe("mangleProperties", function() {
it("Shouldn't mangle quoted properties", function() { it("Shouldn't mangle quoted properties", function() {
var js = 'a["foo"] = "bar"; a.color = "red"; x = {"bar": 10};'; var js = 'a["foo"] = "bar"; a.color = "red"; x = {"bar": 10};';
var result = Uglify.minify(js, { var result = UglifyJS.minify(js, {
compress: { compress: {
properties: false properties: false
}, },
@@ -174,7 +174,7 @@ describe("minify", function() {
'a["foo"]="bar",a.a="red",x={"bar":10};'); 'a["foo"]="bar",a.a="red",x={"bar":10};');
}); });
it("Should not mangle quoted property within dead code", function() { it("Should not mangle quoted property within dead code", function() {
var result = Uglify.minify('({ "keep": 1 }); g.keep = g.change;', { var result = UglifyJS.minify('({ "keep": 1 }); g.keep = g.change;', {
mangle: { mangle: {
properties: { properties: {
keep_quoted: true keep_quoted: true
@@ -188,7 +188,7 @@ describe("minify", function() {
describe("#__PURE__", function() { describe("#__PURE__", function() {
it("should drop #__PURE__ hint after use", function() { it("should drop #__PURE__ hint after use", function() {
var result = Uglify.minify('//@__PURE__ comment1 #__PURE__ comment2\n foo(), bar();', { var result = UglifyJS.minify('//@__PURE__ comment1 #__PURE__ comment2\n foo(), bar();', {
output: { output: {
comments: "all", comments: "all",
beautify: false, beautify: false,
@@ -198,7 +198,7 @@ describe("minify", function() {
assert.strictEqual(code, "// comment1 comment2\nbar();"); assert.strictEqual(code, "// comment1 comment2\nbar();");
}); });
it("should drop #__PURE__ hint if function is retained", function() { it("should drop #__PURE__ hint if function is retained", function() {
var result = Uglify.minify("var a = /*#__PURE__*/(function(){ foo(); })();", { var result = UglifyJS.minify("var a = /*#__PURE__*/(function(){ foo(); })();", {
output: { output: {
comments: "all", comments: "all",
beautify: false, beautify: false,
@@ -211,7 +211,7 @@ describe("minify", function() {
describe("JS_Parse_Error", function() { describe("JS_Parse_Error", function() {
it("should return syntax error", function() { it("should return syntax error", function() {
var result = Uglify.minify("function f(a{}"); var result = UglifyJS.minify("function f(a{}");
var err = result.error; var err = result.error;
assert.ok(err instanceof Error); assert.ok(err instanceof Error);
assert.strictEqual(err.stack.split(/\n/)[0], "SyntaxError: Unexpected token punc «{», expected punc «,»"); assert.strictEqual(err.stack.split(/\n/)[0], "SyntaxError: Unexpected token punc «{», expected punc «,»");
@@ -220,7 +220,7 @@ describe("minify", function() {
assert.strictEqual(err.col, 12); assert.strictEqual(err.col, 12);
}); });
it("should reject duplicated label name", function() { it("should reject duplicated label name", function() {
var result = Uglify.minify("L:{L:{}}"); var result = UglifyJS.minify("L:{L:{}}");
var err = result.error; var err = result.error;
assert.ok(err instanceof Error); assert.ok(err instanceof Error);
assert.strictEqual(err.stack.split(/\n/)[0], "SyntaxError: Label L defined twice"); assert.strictEqual(err.stack.split(/\n/)[0], "SyntaxError: Label L defined twice");
@@ -232,7 +232,7 @@ describe("minify", function() {
describe("global_defs", function() { describe("global_defs", function() {
it("should throw for non-trivial expressions", function() { it("should throw for non-trivial expressions", function() {
var result = Uglify.minify("alert(42);", { var result = UglifyJS.minify("alert(42);", {
compress: { compress: {
global_defs: { global_defs: {
"@alert": "debugger" "@alert": "debugger"
@@ -246,7 +246,7 @@ describe("minify", function() {
it("should skip inherited properties", function() { it("should skip inherited properties", function() {
var foo = Object.create({ skip: this }); var foo = Object.create({ skip: this });
foo.bar = 42; foo.bar = 42;
var result = Uglify.minify("alert(FOO);", { var result = UglifyJS.minify("alert(FOO);", {
compress: { compress: {
global_defs: { global_defs: {
FOO: foo FOO: foo
@@ -266,7 +266,7 @@ describe("minify", function() {
"}", "}",
"f();", "f();",
].join("\n"); ].join("\n");
var ast = Uglify.minify(code, { var ast = UglifyJS.minify(code, {
compress: false, compress: false,
mangle: false, mangle: false,
output: { output: {
@@ -279,7 +279,7 @@ describe("minify", function() {
assert.strictEqual(ast.body[0].body.length, 2); assert.strictEqual(ast.body[0].body.length, 2);
assert.strictEqual(ast.body[0].body[0].TYPE, "SimpleStatement"); assert.strictEqual(ast.body[0].body[0].TYPE, "SimpleStatement");
var stat = ast.body[0].body[0]; var stat = ast.body[0].body[0];
Uglify.minify(ast, { UglifyJS.minify(ast, {
compress: { compress: {
sequences: false sequences: false
}, },
@@ -294,7 +294,7 @@ describe("minify", function() {
it("Should be repeatable", function() { it("Should be repeatable", function() {
var code = "!function(x){return x(x)}(y);"; var code = "!function(x){return x(x)}(y);";
for (var i = 0; i < 2; i++) { for (var i = 0; i < 2; i++) {
assert.strictEqual(Uglify.minify(code, { assert.strictEqual(UglifyJS.minify(code, {
compress: { compress: {
toplevel: true, toplevel: true,
}, },
@@ -307,7 +307,7 @@ describe("minify", function() {
describe("enclose", function() { describe("enclose", function() {
var code = read("test/input/enclose/input.js"); var code = read("test/input/enclose/input.js");
it("Should work with true", function() { it("Should work with true", function() {
var result = Uglify.minify(code, { var result = UglifyJS.minify(code, {
compress: false, compress: false,
enclose: true, enclose: true,
mangle: false, mangle: false,
@@ -316,7 +316,7 @@ describe("minify", function() {
assert.strictEqual(result.code, '(function(){function enclose(){console.log("test enclose")}enclose()})();'); assert.strictEqual(result.code, '(function(){function enclose(){console.log("test enclose")}enclose()})();');
}); });
it("Should work with arg", function() { it("Should work with arg", function() {
var result = Uglify.minify(code, { var result = UglifyJS.minify(code, {
compress: false, compress: false,
enclose: 'undefined', enclose: 'undefined',
mangle: false, mangle: false,
@@ -325,7 +325,7 @@ describe("minify", function() {
assert.strictEqual(result.code, '(function(undefined){function enclose(){console.log("test enclose")}enclose()})();'); assert.strictEqual(result.code, '(function(undefined){function enclose(){console.log("test enclose")}enclose()})();');
}); });
it("Should work with arg:value", function() { it("Should work with arg:value", function() {
var result = Uglify.minify(code, { var result = UglifyJS.minify(code, {
compress: false, compress: false,
enclose: 'window,undefined:window', enclose: 'window,undefined:window',
mangle: false, mangle: false,
@@ -334,7 +334,7 @@ describe("minify", function() {
assert.strictEqual(result.code, '(function(window,undefined){function enclose(){console.log("test enclose")}enclose()})(window);'); assert.strictEqual(result.code, '(function(window,undefined){function enclose(){console.log("test enclose")}enclose()})(window);');
}); });
it("Should work alongside wrap", function() { it("Should work alongside wrap", function() {
var result = Uglify.minify(code, { var result = UglifyJS.minify(code, {
compress: false, compress: false,
enclose: 'window,undefined:window', enclose: 'window,undefined:window',
mangle: false, mangle: false,

View File

@@ -1,5 +1,5 @@
var assert = require("assert"); var assert = require("assert");
var uglify = require("../node"); var UglifyJS = require("../node");
describe("Number literals", function() { describe("Number literals", function() {
it("Should not allow legacy octal literals in strict mode", function() { it("Should not allow legacy octal literals in strict mode", function() {
@@ -9,11 +9,11 @@ describe("Number literals", function() {
]; ];
var test = function(input) { var test = function(input) {
return function() { return function() {
uglify.parse(input); UglifyJS.parse(input);
} }
}; };
var error = function(e) { var error = function(e) {
return e instanceof uglify.JS_Parse_Error return e instanceof UglifyJS.JS_Parse_Error
&& e.message === "Legacy octal literals are not allowed in strict mode"; && e.message === "Legacy octal literals are not allowed in strict mode";
}; };
for (var i = 0; i < inputs.length; i++) { for (var i = 0; i < inputs.length; i++) {

View File

@@ -1,5 +1,5 @@
var UglifyJS = require("../node");
var assert = require("assert"); var assert = require("assert");
var UglifyJS = require("../..");
describe("operator", function() { describe("operator", function() {
it("Should handle mixing of ++/+/--/- correctly", function() { it("Should handle mixing of ++/+/--/- correctly", function() {

View File

@@ -1,5 +1,5 @@
var assert = require("assert"); var assert = require("assert");
var uglify = require("../../"); var UglifyJS = require("../..");
describe("parentheses", function() { describe("parentheses", function() {
it("Should add trailing parentheses for new expressions with zero arguments in beautify mode", function() { it("Should add trailing parentheses for new expressions with zero arguments in beautify mode", function() {
@@ -33,7 +33,7 @@ describe("parentheses", function() {
]; ];
for (var i = 0; i < tests.length; i++) { for (var i = 0; i < tests.length; i++) {
assert.strictEqual( assert.strictEqual(
uglify.minify(tests[i], { UglifyJS.minify(tests[i], {
output: {beautify: true}, output: {beautify: true},
compress: false, compress: false,
mangle: false mangle: false
@@ -74,7 +74,7 @@ describe("parentheses", function() {
]; ];
for (var i = 0; i < tests.length; i++) { for (var i = 0; i < tests.length; i++) {
assert.strictEqual( assert.strictEqual(
uglify.minify(tests[i], { UglifyJS.minify(tests[i], {
output: {beautify: false}, output: {beautify: false},
compress: false, compress: false,
mangle: false mangle: false
@@ -94,7 +94,7 @@ describe("parentheses", function() {
code = code.concat(code); code = code.concat(code);
} }
code = code.join(""); code = code.join("");
var result = uglify.minify(code, { var result = UglifyJS.minify(code, {
compress: false, compress: false,
mangle: false, mangle: false,
}); });

View File

@@ -1,19 +1,52 @@
var assert = require("assert"); var assert = require("assert");
var readFileSync = require("fs").readFileSync; var readFileSync = require("fs").readFileSync;
var Uglify = require("../../"); var SourceMapConsumer = require("source-map").SourceMapConsumer;
var UglifyJS = require("../..");
function read(path) { function read(path) {
return readFileSync(path, "utf8"); return readFileSync(path, "utf8");
} }
function source_map(code) { function source_map(code) {
return JSON.parse(Uglify.minify(code, { return JSON.parse(UglifyJS.minify(code, {
compress: false, compress: false,
mangle: false, mangle: false,
sourceMap: true, sourceMap: true,
}).map); }).map);
} }
function get_map() {
return {
"version": 3,
"sources": ["index.js"],
"names": [],
"mappings": ";;AAAA,IAAI,MAAM,SAAN,GAAM;AAAA,SAAK,SAAS,CAAd;AAAA,CAAV;AACA,QAAQ,GAAR,CAAY,IAAI,KAAJ,CAAZ",
"file": "bundle.js",
"sourcesContent": ["let foo = x => \"foo \" + x;\nconsole.log(foo(\"bar\"));"]
};
}
function prepare_map(sourceMap) {
var code = [
'"use strict";',
"",
"var foo = function foo(x) {",
' return "foo " + x;',
"};",
'console.log(foo("bar"));',
"",
"//# sourceMappingURL=bundle.js.map",
].join("\n");
var result = UglifyJS.minify(code, {
sourceMap: {
content: sourceMap,
includeSources: true,
}
});
if (result.error) throw result.error;
return new SourceMapConsumer(result.map);
}
describe("sourcemaps", function() { describe("sourcemaps", function() {
it("Should give correct version", function() { it("Should give correct version", function() {
var map = source_map("var x = 1 + 1;"); var map = source_map("var x = 1 + 1;");
@@ -36,7 +69,7 @@ describe("sourcemaps", function() {
}); });
it("Should mark array/object literals", function() { it("Should mark array/object literals", function() {
var result = Uglify.minify([ var result = UglifyJS.minify([
"var obj = {};", "var obj = {};",
"obj.wat([]);", "obj.wat([]);",
].join("\n"), { ].join("\n"), {
@@ -50,7 +83,7 @@ describe("sourcemaps", function() {
it("Should give correct sourceRoot", function() { it("Should give correct sourceRoot", function() {
var code = "console.log(42);"; var code = "console.log(42);";
var result = Uglify.minify(code, { var result = UglifyJS.minify(code, {
sourceMap: { sourceMap: {
root: "//foo.bar/", root: "//foo.bar/",
}, },
@@ -61,8 +94,8 @@ describe("sourcemaps", function() {
}); });
describe("inSourceMap", function() { describe("inSourceMap", function() {
it("Should read the given string filename correctly when sourceMapIncludeSources is enabled (#1236)", function() { it("Should read the given string filename correctly when sourceMapIncludeSources is enabled", function() {
var result = Uglify.minify(read("./test/input/issue-1236/simple.js"), { var result = UglifyJS.minify(read("./test/input/issue-1236/simple.js"), {
sourceMap: { sourceMap: {
content: read("./test/input/issue-1236/simple.js.map"), content: read("./test/input/issue-1236/simple.js.map"),
filename: "simple.min.js", filename: "simple.min.js",
@@ -76,7 +109,7 @@ describe("sourcemaps", function() {
assert.equal(map.sourcesContent[0], 'let foo = x => "foo " + x;\nconsole.log(foo("bar"));'); assert.equal(map.sourcesContent[0], 'let foo = x => "foo " + x;\nconsole.log(foo("bar"));');
}); });
it("Should process inline source map", function() { it("Should process inline source map", function() {
var result = Uglify.minify(read("./test/input/issue-520/input.js"), { var result = UglifyJS.minify(read("./test/input/issue-520/input.js"), {
compress: { toplevel: true }, compress: { toplevel: true },
sourceMap: { sourceMap: {
content: "inline", content: "inline",
@@ -88,13 +121,13 @@ describe("sourcemaps", function() {
assert.strictEqual(result.code + "\n", readFileSync("test/input/issue-520/output.js", "utf8")); assert.strictEqual(result.code + "\n", readFileSync("test/input/issue-520/output.js", "utf8"));
}); });
it("Should warn for missing inline source map", function() { it("Should warn for missing inline source map", function() {
var warn_function = Uglify.AST_Node.warn_function; var warn_function = UglifyJS.AST_Node.warn_function;
var warnings = []; var warnings = [];
Uglify.AST_Node.warn_function = function(txt) { UglifyJS.AST_Node.warn_function = function(txt) {
warnings.push(txt); warnings.push(txt);
}; };
try { try {
var result = Uglify.minify(read("./test/input/issue-1323/sample.js"), { var result = UglifyJS.minify(read("./test/input/issue-1323/sample.js"), {
mangle: false, mangle: false,
sourceMap: { sourceMap: {
content: "inline" content: "inline"
@@ -104,17 +137,17 @@ describe("sourcemaps", function() {
assert.strictEqual(warnings.length, 1); assert.strictEqual(warnings.length, 1);
assert.strictEqual(warnings[0], "inline source map not found: 0"); assert.strictEqual(warnings[0], "inline source map not found: 0");
} finally { } finally {
Uglify.AST_Node.warn_function = warn_function; UglifyJS.AST_Node.warn_function = warn_function;
} }
}); });
it("Should handle multiple input and inline source map", function() { it("Should handle multiple input and inline source map", function() {
var warn_function = Uglify.AST_Node.warn_function; var warn_function = UglifyJS.AST_Node.warn_function;
var warnings = []; var warnings = [];
Uglify.AST_Node.warn_function = function(txt) { UglifyJS.AST_Node.warn_function = function(txt) {
warnings.push(txt); warnings.push(txt);
}; };
try { try {
var result = Uglify.minify([ var result = UglifyJS.minify([
read("./test/input/issue-520/input.js"), read("./test/input/issue-520/input.js"),
read("./test/input/issue-1323/sample.js"), read("./test/input/issue-1323/sample.js"),
], { ], {
@@ -131,11 +164,11 @@ describe("sourcemaps", function() {
assert.strictEqual(warnings.length, 1); assert.strictEqual(warnings.length, 1);
assert.strictEqual(warnings[0], "inline source map not found: 1"); assert.strictEqual(warnings[0], "inline source map not found: 1");
} finally { } finally {
Uglify.AST_Node.warn_function = warn_function; UglifyJS.AST_Node.warn_function = warn_function;
} }
}); });
it("Should drop source contents for includeSources=false", function() { it("Should drop source contents for includeSources=false", function() {
var result = Uglify.minify(read("./test/input/issue-520/input.js"), { var result = UglifyJS.minify(read("./test/input/issue-520/input.js"), {
compress: false, compress: false,
mangle: false, mangle: false,
sourceMap: { sourceMap: {
@@ -146,7 +179,7 @@ describe("sourcemaps", function() {
if (result.error) throw result.error; if (result.error) throw result.error;
var map = JSON.parse(result.map); var map = JSON.parse(result.map);
assert.strictEqual(map.sourcesContent.length, 1); assert.strictEqual(map.sourcesContent.length, 1);
result = Uglify.minify(result.code, { result = UglifyJS.minify(result.code, {
compress: false, compress: false,
mangle: false, mangle: false,
sourceMap: { sourceMap: {
@@ -161,7 +194,7 @@ describe("sourcemaps", function() {
describe("sourceMapInline", function() { describe("sourceMapInline", function() {
it("Should append source map to output js when sourceMapInline is enabled", function() { it("Should append source map to output js when sourceMapInline is enabled", function() {
var result = Uglify.minify('var a = function(foo) { return foo; };', { var result = UglifyJS.minify('var a = function(foo) { return foo; };', {
sourceMap: { sourceMap: {
url: "inline" url: "inline"
} }
@@ -172,13 +205,13 @@ describe("sourcemaps", function() {
"//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIjAiXSwibmFtZXMiOlsiYSIsImZvbyJdLCJtYXBwaW5ncyI6IkFBQUEsSUFBSUEsRUFBSSxTQUFTQyxHQUFPLE9BQU9BIn0="); "//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIjAiXSwibmFtZXMiOlsiYSIsImZvbyJdLCJtYXBwaW5ncyI6IkFBQUEsSUFBSUEsRUFBSSxTQUFTQyxHQUFPLE9BQU9BIn0=");
}); });
it("Should not append source map to output js when sourceMapInline is not enabled", function() { it("Should not append source map to output js when sourceMapInline is not enabled", function() {
var result = Uglify.minify('var a = function(foo) { return foo; };'); var result = UglifyJS.minify('var a = function(foo) { return foo; };');
if (result.error) throw result.error; if (result.error) throw result.error;
var code = result.code; var code = result.code;
assert.strictEqual(code, "var a=function(n){return n};"); assert.strictEqual(code, "var a=function(n){return n};");
}); });
it("Should work with max_line_len", function() { it("Should work with max_line_len", function() {
var result = Uglify.minify(read("./test/input/issue-505/input.js"), { var result = UglifyJS.minify(read("./test/input/issue-505/input.js"), {
output: { output: {
max_line_len: 20 max_line_len: 20
}, },
@@ -194,7 +227,7 @@ describe("sourcemaps", function() {
"var tëst = '→unicøde←';", "var tëst = '→unicøde←';",
"alert(tëst);", "alert(tëst);",
].join("\n"); ].join("\n");
var result = Uglify.minify(code, { var result = UglifyJS.minify(code, {
sourceMap: { sourceMap: {
includeSources: true, includeSources: true,
url: "inline", url: "inline",
@@ -208,7 +241,7 @@ describe("sourcemaps", function() {
map = JSON.parse(new Buffer(encoded, "base64").toString()); map = JSON.parse(new Buffer(encoded, "base64").toString());
assert.strictEqual(map.sourcesContent.length, 1); assert.strictEqual(map.sourcesContent.length, 1);
assert.strictEqual(map.sourcesContent[0], code); assert.strictEqual(map.sourcesContent[0], code);
result = Uglify.minify(result.code, { result = UglifyJS.minify(result.code, {
sourceMap: { sourceMap: {
content: "inline", content: "inline",
includeSources: true, includeSources: true,
@@ -221,4 +254,37 @@ describe("sourcemaps", function() {
assert.strictEqual(map.names[1], "alert"); assert.strictEqual(map.names[1], "alert");
}); });
}); });
describe("input sourcemaps", function() {
it("Should copy over original sourcesContent", function() {
var orig = get_map();
var map = prepare_map(orig);
assert.equal(map.sourceContentFor("index.js"), orig.sourcesContent[0]);
});
it("Should copy sourcesContent if sources are relative", function() {
var relativeMap = get_map();
relativeMap.sources = ['./index.js'];
var map = prepare_map(relativeMap);
assert.notEqual(map.sourcesContent, null);
assert.equal(map.sourcesContent.length, 1);
assert.equal(map.sourceContentFor("index.js"), relativeMap.sourcesContent[0]);
});
it("Should not have invalid mappings from inputSourceMap", function() {
var map = prepare_map(get_map());
// The original source has only 2 lines, check that mappings don't have more lines
var msg = "Mapping should not have higher line number than the original file had";
map.eachMapping(function(mapping) {
assert.ok(mapping.originalLine <= 2, msg);
});
map.allGeneratedPositionsFor({
source: "index.js",
line: 1,
column: 1
}).forEach(function(pos) {
assert.ok(pos.line <= 2, msg);
});
});
});
}); });

View File

@@ -1,6 +1,6 @@
var assert = require("assert"); var assert = require("assert");
var exec = require("child_process").exec; var exec = require("child_process").exec;
var uglify = require("../node"); var UglifyJS = require("../..");
describe("spidermonkey export/import sanity test", function() { describe("spidermonkey export/import sanity test", function() {
it("should produce a functional build when using --self with spidermonkey", function(done) { it("should produce a functional build when using --self with spidermonkey", function(done) {
@@ -25,9 +25,9 @@ describe("spidermonkey export/import sanity test", function() {
it("should not add unnecessary escape slashes to regexps", function() { it("should not add unnecessary escape slashes to regexps", function() {
var input = "/[\\\\/]/;"; var input = "/[\\\\/]/;";
var ast = uglify.parse(input).to_mozilla_ast(); var ast = UglifyJS.parse(input).to_mozilla_ast();
assert.equal( assert.equal(
uglify.AST_Node.from_mozilla_ast(ast).print_to_string(), UglifyJS.AST_Node.from_mozilla_ast(ast).print_to_string(),
input input
); );
}); });
@@ -99,10 +99,10 @@ describe("spidermonkey export/import sanity test", function() {
var counter_directives; var counter_directives;
var counter_strings; var counter_strings;
var checkWalker = new uglify.TreeWalker(function(node, descend) { var checkWalker = new UglifyJS.TreeWalker(function(node, descend) {
if (node instanceof uglify.AST_String) { if (node instanceof UglifyJS.AST_String) {
counter_strings++; counter_strings++;
} else if (node instanceof uglify.AST_Directive) { } else if (node instanceof UglifyJS.AST_Directive) {
counter_directives++; counter_directives++;
} }
}); });
@@ -111,9 +111,9 @@ describe("spidermonkey export/import sanity test", function() {
counter_directives = 0; counter_directives = 0;
counter_strings = 0; counter_strings = 0;
var ast = uglify.parse(tests[i].input); var ast = UglifyJS.parse(tests[i].input);
var moz_ast = ast.to_mozilla_ast(); var moz_ast = ast.to_mozilla_ast();
var from_moz_ast = uglify.AST_Node.from_mozilla_ast(moz_ast); var from_moz_ast = UglifyJS.AST_Node.from_mozilla_ast(moz_ast);
from_moz_ast.walk(checkWalker); from_moz_ast.walk(checkWalker);

View File

@@ -1,5 +1,5 @@
var UglifyJS = require("../node");
var assert = require("assert"); var assert = require("assert");
var UglifyJS = require("../node");
describe("String literals", function() { describe("String literals", function() {
it("Should throw syntax error if a string literal contains a newline", function() { it("Should throw syntax error if a string literal contains a newline", function() {
@@ -18,8 +18,8 @@ describe("String literals", function() {
}; };
var error = function(e) { var error = function(e) {
return e instanceof UglifyJS.JS_Parse_Error && return e instanceof UglifyJS.JS_Parse_Error
e.message === "Unterminated string constant"; && e.message === "Unterminated string constant";
}; };
for (var input in inputs) { for (var input in inputs) {
@@ -48,8 +48,8 @@ describe("String literals", function() {
}; };
var error = function(e) { var error = function(e) {
return e instanceof UglifyJS.JS_Parse_Error && return e instanceof UglifyJS.JS_Parse_Error
e.message === "Legacy octal escape sequences are not allowed in strict mode"; && e.message === "Legacy octal escape sequences are not allowed in strict mode";
} }
for (var input in inputs) { for (var input in inputs) {

View File

@@ -1,32 +1,26 @@
var UglifyJS = require("../node");
var assert = require("assert"); var assert = require("assert");
var UglifyJS = require("../..");
describe("Accessor tokens", function() { describe("tokens", function() {
it("Should fill the token information for accessors (issue #1492)", function() { it("Should give correct positions for accessors", function() {
// location 0 1 2 3 4 // location 0 1 2 3 4
// 01234567890123456789012345678901234567890123456789 // 01234567890123456789012345678901234567890123456789
var ast = UglifyJS.parse("var obj = { get latest() { return undefined; } }"); var ast = UglifyJS.parse("var obj = { get latest() { return undefined; } }");
// test all AST_ObjectProperty tokens are set as expected // test all AST_ObjectProperty tokens are set as expected
var checkedAST_ObjectProperty = false; var found = false;
var checkWalker = new UglifyJS.TreeWalker(function(node, descend) { ast.walk(new UglifyJS.TreeWalker(function(node) {
if (node instanceof UglifyJS.AST_ObjectProperty) { if (node instanceof UglifyJS.AST_ObjectProperty) {
checkedAST_ObjectProperty = true; found = true;
assert.equal(node.start.pos, 12); assert.equal(node.start.pos, 12);
assert.equal(node.end.endpos, 46); assert.equal(node.end.endpos, 46);
assert(node.key instanceof UglifyJS.AST_SymbolAccessor); assert(node.key instanceof UglifyJS.AST_SymbolAccessor);
assert.equal(node.key.start.pos, 16); assert.equal(node.key.start.pos, 16);
assert.equal(node.key.end.endpos, 22); assert.equal(node.key.end.endpos, 22);
assert(node.value instanceof UglifyJS.AST_Accessor); assert(node.value instanceof UglifyJS.AST_Accessor);
assert.equal(node.value.start.pos, 22); assert.equal(node.value.start.pos, 22);
assert.equal(node.value.end.endpos, 46); assert.equal(node.value.end.endpos, 46);
} }
}); }));
ast.walk(checkWalker); assert(found, "AST_ObjectProperty not found");
assert(checkedAST_ObjectProperty, "AST_ObjectProperty not found");
}); });
}); });

View File

@@ -1,20 +1,20 @@
var assert = require("assert"); var assert = require("assert");
var uglify = require("../node"); var UglifyJS = require("../node");
describe("With", function() { describe("With", function() {
it("Should throw syntaxError when using with statement in strict mode", function() { it("Should throw syntaxError when using with statement in strict mode", function() {
var code = '"use strict";\nthrow NotEarlyError;\nwith ({}) { }'; var code = '"use strict";\nthrow NotEarlyError;\nwith ({}) { }';
var test = function() { var test = function() {
uglify.parse(code); UglifyJS.parse(code);
} }
var error = function(e) { var error = function(e) {
return e instanceof uglify.JS_Parse_Error && return e instanceof UglifyJS.JS_Parse_Error
e.message === "Strict mode may not include a with statement"; && e.message === "Strict mode may not include a with statement";
} }
assert.throws(test, error); assert.throws(test, error);
}); });
it("Should set uses_with for scopes involving With statements", function() { it("Should set uses_with for scopes involving With statements", function() {
var ast = uglify.parse("with(e) {f(1, 2)}"); var ast = UglifyJS.parse("with(e) {f(1, 2)}");
ast.figure_out_scope(); ast.figure_out_scope();
assert.equal(ast.uses_with, true); assert.equal(ast.uses_with, true);
assert.equal(ast.body[0].expression.scope.uses_with, true); assert.equal(ast.body[0].expression.scope.uses_with, true);