-var UglifyJS = require("../node");
var assert = require("assert");
+var UglifyJS = require("../..");
describe("arguments", function() {
it("Should known that arguments in functions are local scoped", function() {
var ast = UglifyJS.parse("var arguments; var f = function() {arguments.length}");
ast.figure_out_scope();
-
// Test scope of `var arguments`
assert.strictEqual(ast.find_variable("arguments").global, true);
-
// Select arguments symbol in function
var symbol = ast.body[1].definitions[0].value.find_variable("arguments");
-
assert.strictEqual(symbol.global, false);
assert.strictEqual(symbol.scope, ast. // From ast
body[1]. // Select 2nd statement (equals to `var f ...`)
assert.strictEqual(ast.body[0].body[0].uses_arguments, true);
assert.strictEqual(ast.body[0].body[0].body[0].uses_arguments, false);
});
-});
\ No newline at end of file
+});
+++ /dev/null
-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;");
- });
-});
+++ /dev/null
-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"));
- });
-});
+++ /dev/null
-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}');
- });
-});
--- /dev/null
+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}");
+ });
+ });
+});
var assert = require("assert");
-var uglify = require("../node");
+var UglifyJS = require("../node");
describe("Directives", 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
assert.strictEqual(tokenizer.has_directive("use strict"), false);
for (var i = 0; i < tests.length; i++) {
// 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 {
- var parser = uglify.parse(tokenizer);
+ var parser = UglifyJS.parse(tokenizer);
throw new Error("Expected parser to fail");
} 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 (])");
}
["'tests';\n\n", true],
["\n\n\"use strict\";\n\n", true]
].forEach(function(test) {
- var out = uglify.OutputStream();
+ var out = UglifyJS.OutputStream();
out.print(test[0]);
out.print_string("", null, true);
assert.strictEqual(out.get() === test[0] + ';""', test[1], test[0]);
it("Should only print 2 semicolons spread over 2 lines in beautify mode", function() {
assert.strictEqual(
- uglify.minify(
+ UglifyJS.minify(
'"use strict";\'use strict\';"use strict";"use strict";;\'use strict\';console.log(\'use strict\');',
{output: {beautify: true, quote_style: 3}, compress: false}
).code,
for (var i = 0; i < tests.length; i++) {
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][0]
);
});
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}
).code;
assert.strictEqual(code, '"use strict";;"use strict"\n');
];
for (var i = 0; i < tests.length; i++) {
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][0] + " using mode " + tests[i][1]
);
for (var i = 0; i < tests.length; i++) {
assert.strictEqual(
- uglify.minify(tests[i][0]).code,
+ UglifyJS.minify(tests[i][0]).code,
tests[i][1],
tests[i][0]
);
-var UglifyJS = require("../node");
var assert = require("assert");
+var UglifyJS = require("../node");
describe("Getters and setters", function() {
it("Should not accept operator symbols as getter/setter name", function() {
+++ /dev/null
-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}");
- });
-});
var assert = require("assert");
-var uglify = require("../../");
+var UglifyJS = require("../..");
describe("ie8", function() {
it("Should be able to minify() with undefined as catch parameter in a try...catch statement", function() {
assert.strictEqual(
- uglify.minify([
+ UglifyJS.minify([
"function a(b){",
" try {",
" throw 'Stuff';",
+++ /dev/null
-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);
- });
- });
-});
-var Uglify = require('../../');
var assert = require("assert");
+var UglifyJS = require("../..");
describe("let", function() {
this.timeout(30000);
s += "var v" + i + "=0;";
}
s += '}';
- var result = Uglify.minify(s, {
+ var result = UglifyJS.minify(s, {
compress: false
}).code;
for (var i = 0; i < 18000; i++) {
s += "v.b" + i + ";";
}
- var result = Uglify.minify(s, {
+ var result = UglifyJS.minify(s, {
compress: false,
ie8: true,
mangle: {
-var Uglify = require("../node");
var assert = require("assert");
+var UglifyJS = require("../node");
describe("line-endings", function() {
var options = {
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 result = Uglify.minify(js, options);
+ var result = UglifyJS.minify(js, options);
assert.strictEqual(result.code, expected_code);
});
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 result = Uglify.minify(js, options);
+ var result = UglifyJS.minify(js, options);
assert.strictEqual(result.code, expected_code);
});
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 result = Uglify.minify(js, options);
+ var result = UglifyJS.minify(js, options);
assert.strictEqual(result.code, expected_code);
});
]
var test = function(input) {
return function() {
- Uglify.parse(input);
+ UglifyJS.parse(input);
}
}
var fail = function(e) {
- return e instanceof Uglify.JS_Parse_Error &&
- e.message === "Unexpected line terminator";
+ return e instanceof UglifyJS.JS_Parse_Error
+ && e.message === "Unexpected line terminator";
}
for (var i = 0; i < inputs.length; i++) {
assert.throws(test(inputs[i]), fail);
}
});
});
-
-var Uglify = require('../../');
var assert = require("assert");
+var UglifyJS = require("../..");
describe("Input file as map", function() {
it("Should accept object", function() {
var jsMap = {
'/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);
assert.strictEqual(result.code, 'var foo={x:1,y:2,z:3};');
assert.deepEqual(map.sources, ['/scripts/foo.js']);
assert.strictEqual(map.file, undefined);
- result = Uglify.minify(jsMap);
+ result = UglifyJS.minify(jsMap);
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);
assert.strictEqual(map.file, 'out.js');
});
'var foo = {"x": 1, y: 2, \'z\': 3};',
'var bar = 15;'
];
- var result = Uglify.minify(jsSeq, {sourceMap: true});
+ var result = UglifyJS.minify(jsSeq, {sourceMap: true});
var map = JSON.parse(result.map);
assert.strictEqual(result.code, 'var foo={x:1,y:2,z:3},bar=15;');
var jsMap = {
'/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);
assert.strictEqual(result.code, 'var foo={x:1,y:2,z:3};');
-var Uglify = require('../../');
var assert = require("assert");
var readFileSync = require("fs").readFileSync;
var run_code = require("../sandbox").run_code;
+var UglifyJS = require("../../");
function read(path) {
return readFileSync(path, "utf8");
describe("minify", 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 result = Uglify.minify(js);
+ var result = UglifyJS.minify(js);
assert.strictEqual(result.code, 'function foo(n){return n?3:7}');
});
it("Should skip inherited keys from `files`", function() {
var files = Object.create({ skip: this });
files[0] = "alert(1 + 1)";
- var result = Uglify.minify(files);
+ var result = UglifyJS.minify(files);
assert.strictEqual(result.code, "alert(2);");
});
"qux.js",
].forEach(function(file) {
var code = read("test/input/issue-1242/" + file);
- var result = Uglify.minify(code, {
+ var result = UglifyJS.minify(code, {
mangle: {
cache: cache,
toplevel: true
"qux.js",
].forEach(function(file) {
var code = read("test/input/issue-1242/" + file);
- var result = Uglify.minify(code, {
+ var result = UglifyJS.minify(code, {
mangle: {
toplevel: true
},
'"xxyyy";var j={t:2,u:3},k=4;',
'console.log(i.s,j.t,j.u,k);',
].forEach(function(code) {
- var result = Uglify.minify(code, {
+ var result = UglifyJS.minify(code, {
compress: false,
mangle: {
properties: true,
});
it("Should not parse invalid use of reserved words", function() {
- assert.strictEqual(Uglify.minify("function enum(){}").error, undefined);
- assert.strictEqual(Uglify.minify("function static(){}").error, undefined);
- assert.strictEqual(Uglify.minify("function this(){}").error.message, "Unexpected token: name (this)");
+ assert.strictEqual(UglifyJS.minify("function enum(){}").error, undefined);
+ assert.strictEqual(UglifyJS.minify("function static(){}").error, undefined);
+ assert.strictEqual(UglifyJS.minify("function this(){}").error.message, "Unexpected token: name (this)");
});
describe("keep_quoted_props", function() {
it("Should preserve quotes in object literals", function() {
var js = 'var foo = {"x": 1, y: 2, \'z\': 3};';
- var result = Uglify.minify(js, {
+ var result = UglifyJS.minify(js, {
output: {
keep_quoted_props: true
}});
it("Should preserve quote styles when quote_style is 3", function() {
var js = 'var foo = {"x": 1, y: 2, \'z\': 3};';
- var result = Uglify.minify(js, {
+ var result = UglifyJS.minify(js, {
output: {
keep_quoted_props: true,
quote_style: 3
it("Should not preserve quotes in object literals when disabled", function() {
var js = 'var foo = {"x": 1, y: 2, \'z\': 3};';
- var result = Uglify.minify(js, {
+ var result = UglifyJS.minify(js, {
output: {
keep_quoted_props: false,
quote_style: 3
describe("mangleProperties", function() {
it("Shouldn't mangle quoted properties", function() {
var js = 'a["foo"] = "bar"; a.color = "red"; x = {"bar": 10};';
- var result = Uglify.minify(js, {
+ var result = UglifyJS.minify(js, {
compress: {
properties: false
},
'a["foo"]="bar",a.a="red",x={"bar":10};');
});
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: {
properties: {
keep_quoted: true
describe("#__PURE__", 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: {
comments: "all",
beautify: false,
assert.strictEqual(code, "// comment1 comment2\nbar();");
});
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: {
comments: "all",
beautify: false,
describe("JS_Parse_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;
assert.ok(err instanceof Error);
assert.strictEqual(err.stack.split(/\n/)[0], "SyntaxError: Unexpected token punc {, expected punc ,");
assert.strictEqual(err.col, 12);
});
it("should reject duplicated label name", function() {
- var result = Uglify.minify("L:{L:{}}");
+ var result = UglifyJS.minify("L:{L:{}}");
var err = result.error;
assert.ok(err instanceof Error);
assert.strictEqual(err.stack.split(/\n/)[0], "SyntaxError: Label L defined twice");
describe("global_defs", function() {
it("should throw for non-trivial expressions", function() {
- var result = Uglify.minify("alert(42);", {
+ var result = UglifyJS.minify("alert(42);", {
compress: {
global_defs: {
"@alert": "debugger"
it("should skip inherited properties", function() {
var foo = Object.create({ skip: this });
foo.bar = 42;
- var result = Uglify.minify("alert(FOO);", {
+ var result = UglifyJS.minify("alert(FOO);", {
compress: {
global_defs: {
FOO: foo
"}",
"f();",
].join("\n");
- var ast = Uglify.minify(code, {
+ var ast = UglifyJS.minify(code, {
compress: false,
mangle: false,
output: {
assert.strictEqual(ast.body[0].body.length, 2);
assert.strictEqual(ast.body[0].body[0].TYPE, "SimpleStatement");
var stat = ast.body[0].body[0];
- Uglify.minify(ast, {
+ UglifyJS.minify(ast, {
compress: {
sequences: false
},
it("Should be repeatable", function() {
var code = "!function(x){return x(x)}(y);";
for (var i = 0; i < 2; i++) {
- assert.strictEqual(Uglify.minify(code, {
+ assert.strictEqual(UglifyJS.minify(code, {
compress: {
toplevel: true,
},
describe("enclose", function() {
var code = read("test/input/enclose/input.js");
it("Should work with true", function() {
- var result = Uglify.minify(code, {
+ var result = UglifyJS.minify(code, {
compress: false,
enclose: true,
mangle: false,
assert.strictEqual(result.code, '(function(){function enclose(){console.log("test enclose")}enclose()})();');
});
it("Should work with arg", function() {
- var result = Uglify.minify(code, {
+ var result = UglifyJS.minify(code, {
compress: false,
enclose: 'undefined',
mangle: false,
assert.strictEqual(result.code, '(function(undefined){function enclose(){console.log("test enclose")}enclose()})();');
});
it("Should work with arg:value", function() {
- var result = Uglify.minify(code, {
+ var result = UglifyJS.minify(code, {
compress: false,
enclose: 'window,undefined:window',
mangle: false,
assert.strictEqual(result.code, '(function(window,undefined){function enclose(){console.log("test enclose")}enclose()})(window);');
});
it("Should work alongside wrap", function() {
- var result = Uglify.minify(code, {
+ var result = UglifyJS.minify(code, {
compress: false,
enclose: 'window,undefined:window',
mangle: false,
var assert = require("assert");
-var uglify = require("../node");
+var UglifyJS = require("../node");
describe("Number literals", function() {
it("Should not allow legacy octal literals in strict mode", function() {
];
var test = function(input) {
return function() {
- uglify.parse(input);
+ UglifyJS.parse(input);
}
};
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";
};
for (var i = 0; i < inputs.length; i++) {
-var UglifyJS = require("../node");
var assert = require("assert");
+var UglifyJS = require("../..");
describe("operator", function() {
it("Should handle mixing of ++/+/--/- correctly", function() {
var assert = require("assert");
-var uglify = require("../../");
+var UglifyJS = require("../..");
describe("parentheses", function() {
it("Should add trailing parentheses for new expressions with zero arguments in beautify mode", function() {
];
for (var i = 0; i < tests.length; i++) {
assert.strictEqual(
- uglify.minify(tests[i], {
+ UglifyJS.minify(tests[i], {
output: {beautify: true},
compress: false,
mangle: false
];
for (var i = 0; i < tests.length; i++) {
assert.strictEqual(
- uglify.minify(tests[i], {
+ UglifyJS.minify(tests[i], {
output: {beautify: false},
compress: false,
mangle: false
code = code.concat(code);
}
code = code.join("");
- var result = uglify.minify(code, {
+ var result = UglifyJS.minify(code, {
compress: false,
mangle: false,
});
var assert = require("assert");
var readFileSync = require("fs").readFileSync;
-var Uglify = require("../../");
+var SourceMapConsumer = require("source-map").SourceMapConsumer;
+var UglifyJS = require("../..");
function read(path) {
return readFileSync(path, "utf8");
}
function source_map(code) {
- return JSON.parse(Uglify.minify(code, {
+ return JSON.parse(UglifyJS.minify(code, {
compress: false,
mangle: false,
sourceMap: true,
}).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() {
it("Should give correct version", function() {
var map = source_map("var x = 1 + 1;");
});
it("Should mark array/object literals", function() {
- var result = Uglify.minify([
+ var result = UglifyJS.minify([
"var obj = {};",
"obj.wat([]);",
].join("\n"), {
it("Should give correct sourceRoot", function() {
var code = "console.log(42);";
- var result = Uglify.minify(code, {
+ var result = UglifyJS.minify(code, {
sourceMap: {
root: "//foo.bar/",
},
});
describe("inSourceMap", function() {
- it("Should read the given string filename correctly when sourceMapIncludeSources is enabled (#1236)", function() {
- var result = Uglify.minify(read("./test/input/issue-1236/simple.js"), {
+ it("Should read the given string filename correctly when sourceMapIncludeSources is enabled", function() {
+ var result = UglifyJS.minify(read("./test/input/issue-1236/simple.js"), {
sourceMap: {
content: read("./test/input/issue-1236/simple.js.map"),
filename: "simple.min.js",
assert.equal(map.sourcesContent[0], 'let foo = x => "foo " + x;\nconsole.log(foo("bar"));');
});
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 },
sourceMap: {
content: "inline",
assert.strictEqual(result.code + "\n", readFileSync("test/input/issue-520/output.js", "utf8"));
});
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 = [];
- Uglify.AST_Node.warn_function = function(txt) {
+ UglifyJS.AST_Node.warn_function = function(txt) {
warnings.push(txt);
};
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,
sourceMap: {
content: "inline"
assert.strictEqual(warnings.length, 1);
assert.strictEqual(warnings[0], "inline source map not found: 0");
} 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() {
- var warn_function = Uglify.AST_Node.warn_function;
+ var warn_function = UglifyJS.AST_Node.warn_function;
var warnings = [];
- Uglify.AST_Node.warn_function = function(txt) {
+ UglifyJS.AST_Node.warn_function = function(txt) {
warnings.push(txt);
};
try {
- var result = Uglify.minify([
+ var result = UglifyJS.minify([
read("./test/input/issue-520/input.js"),
read("./test/input/issue-1323/sample.js"),
], {
assert.strictEqual(warnings.length, 1);
assert.strictEqual(warnings[0], "inline source map not found: 1");
} finally {
- Uglify.AST_Node.warn_function = warn_function;
+ UglifyJS.AST_Node.warn_function = warn_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,
mangle: false,
sourceMap: {
if (result.error) throw result.error;
var map = JSON.parse(result.map);
assert.strictEqual(map.sourcesContent.length, 1);
- result = Uglify.minify(result.code, {
+ result = UglifyJS.minify(result.code, {
compress: false,
mangle: false,
sourceMap: {
describe("sourceMapInline", 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: {
url: "inline"
}
"//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIjAiXSwibmFtZXMiOlsiYSIsImZvbyJdLCJtYXBwaW5ncyI6IkFBQUEsSUFBSUEsRUFBSSxTQUFTQyxHQUFPLE9BQU9BIn0=");
});
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;
var code = result.code;
assert.strictEqual(code, "var a=function(n){return n};");
});
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: {
max_line_len: 20
},
"var tëst = '→unicøde←';",
"alert(tst);",
].join("\n");
- var result = Uglify.minify(code, {
+ var result = UglifyJS.minify(code, {
sourceMap: {
includeSources: true,
url: "inline",
map = JSON.parse(new Buffer(encoded, "base64").toString());
assert.strictEqual(map.sourcesContent.length, 1);
assert.strictEqual(map.sourcesContent[0], code);
- result = Uglify.minify(result.code, {
+ result = UglifyJS.minify(result.code, {
sourceMap: {
content: "inline",
includeSources: true,
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);
+ });
+ });
+ });
});
var assert = require("assert");
var exec = require("child_process").exec;
-var uglify = require("../node");
+var UglifyJS = require("../..");
describe("spidermonkey export/import sanity test", function() {
it("should produce a functional build when using --self with spidermonkey", function(done) {
it("should not add unnecessary escape slashes to regexps", function() {
var input = "/[\\\\/]/;";
- var ast = uglify.parse(input).to_mozilla_ast();
+ var ast = UglifyJS.parse(input).to_mozilla_ast();
assert.equal(
- uglify.AST_Node.from_mozilla_ast(ast).print_to_string(),
+ UglifyJS.AST_Node.from_mozilla_ast(ast).print_to_string(),
input
);
});
var counter_directives;
var counter_strings;
- var checkWalker = new uglify.TreeWalker(function(node, descend) {
- if (node instanceof uglify.AST_String) {
+ var checkWalker = new UglifyJS.TreeWalker(function(node, descend) {
+ if (node instanceof UglifyJS.AST_String) {
counter_strings++;
- } else if (node instanceof uglify.AST_Directive) {
+ } else if (node instanceof UglifyJS.AST_Directive) {
counter_directives++;
}
});
counter_directives = 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 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);
-var UglifyJS = require("../node");
var assert = require("assert");
+var UglifyJS = require("../node");
describe("String literals", function() {
it("Should throw syntax error if a string literal contains a newline", function() {
};
var error = function(e) {
- return e instanceof UglifyJS.JS_Parse_Error &&
- e.message === "Unterminated string constant";
+ return e instanceof UglifyJS.JS_Parse_Error
+ && e.message === "Unterminated string constant";
};
for (var input in inputs) {
};
var error = function(e) {
- return e instanceof UglifyJS.JS_Parse_Error &&
- e.message === "Legacy octal escape sequences are not allowed in strict mode";
+ return e instanceof UglifyJS.JS_Parse_Error
+ && e.message === "Legacy octal escape sequences are not allowed in strict mode";
}
for (var input in inputs) {
-var UglifyJS = require("../node");
var assert = require("assert");
+var UglifyJS = require("../..");
-describe("Accessor tokens", function() {
- it("Should fill the token information for accessors (issue #1492)", function() {
+describe("tokens", function() {
+ it("Should give correct positions for accessors", function() {
// location 0 1 2 3 4
// 01234567890123456789012345678901234567890123456789
var ast = UglifyJS.parse("var obj = { get latest() { return undefined; } }");
-
// test all AST_ObjectProperty tokens are set as expected
- var checkedAST_ObjectProperty = false;
- var checkWalker = new UglifyJS.TreeWalker(function(node, descend) {
+ var found = false;
+ ast.walk(new UglifyJS.TreeWalker(function(node) {
if (node instanceof UglifyJS.AST_ObjectProperty) {
- checkedAST_ObjectProperty = true;
-
+ found = true;
assert.equal(node.start.pos, 12);
assert.equal(node.end.endpos, 46);
-
assert(node.key instanceof UglifyJS.AST_SymbolAccessor);
assert.equal(node.key.start.pos, 16);
assert.equal(node.key.end.endpos, 22);
-
assert(node.value instanceof UglifyJS.AST_Accessor);
assert.equal(node.value.start.pos, 22);
assert.equal(node.value.end.endpos, 46);
-
}
- });
- ast.walk(checkWalker);
- assert(checkedAST_ObjectProperty, "AST_ObjectProperty not found");
+ }));
+ assert(found, "AST_ObjectProperty not found");
});
-});
\ No newline at end of file
+});
var assert = require("assert");
-var uglify = require("../node");
+var UglifyJS = require("../node");
describe("With", function() {
it("Should throw syntaxError when using with statement in strict mode", function() {
var code = '"use strict";\nthrow NotEarlyError;\nwith ({}) { }';
var test = function() {
- uglify.parse(code);
+ UglifyJS.parse(code);
}
var error = function(e) {
- return e instanceof uglify.JS_Parse_Error &&
- e.message === "Strict mode may not include a with statement";
+ return e instanceof UglifyJS.JS_Parse_Error
+ && e.message === "Strict mode may not include a with statement";
}
assert.throws(test, error);
});
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();
assert.equal(ast.uses_with, true);
assert.equal(ast.body[0].expression.scope.uses_with, true);