diff options
Diffstat (limited to 'test/mocha')
-rw-r--r-- | test/mocha/arguments.js | 7 | ||||
-rw-r--r-- | test/mocha/comment-filter.js | 89 | ||||
-rw-r--r-- | test/mocha/comment.js | 262 | ||||
-rw-r--r-- | test/mocha/comment_before_constant.js | 22 | ||||
-rw-r--r-- | test/mocha/comments.js | 380 | ||||
-rw-r--r-- | test/mocha/directives.js | 22 | ||||
-rw-r--r-- | test/mocha/getter-setter.js | 2 | ||||
-rw-r--r-- | test/mocha/huge-number-of-comments.js | 14 | ||||
-rw-r--r-- | test/mocha/ie8.js | 4 | ||||
-rw-r--r-- | test/mocha/input-sourcemaps.js | 68 | ||||
-rw-r--r-- | test/mocha/let.js | 6 | ||||
-rw-r--r-- | test/mocha/line-endings.js | 15 | ||||
-rw-r--r-- | test/mocha/minify-file-map.js | 12 | ||||
-rw-r--r-- | test/mocha/minify.js | 54 | ||||
-rw-r--r-- | test/mocha/number-literal.js | 6 | ||||
-rw-r--r-- | test/mocha/operator.js | 2 | ||||
-rw-r--r-- | test/mocha/parentheses.js | 8 | ||||
-rw-r--r-- | test/mocha/sourcemaps.js | 110 | ||||
-rw-r--r-- | test/mocha/spidermonkey.js | 16 | ||||
-rw-r--r-- | test/mocha/string-literal.js | 10 | ||||
-rw-r--r-- | test/mocha/tokens.js (renamed from test/mocha/accessorTokens-1492.js) | 24 | ||||
-rw-r--r-- | test/mocha/with.js | 10 |
22 files changed, 562 insertions, 581 deletions
diff --git a/test/mocha/arguments.js b/test/mocha/arguments.js index 998978eb..6970853c 100644 --- a/test/mocha/arguments.js +++ b/test/mocha/arguments.js @@ -1,17 +1,14 @@ -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 ...`) @@ -27,4 +24,4 @@ describe("arguments", function() { 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 +}); diff --git a/test/mocha/comment-filter.js b/test/mocha/comment-filter.js deleted file mode 100644 index 25233d11..00000000 --- a/test/mocha/comment-filter.js +++ /dev/null @@ -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;"); - }); -}); diff --git a/test/mocha/comment.js b/test/mocha/comment.js deleted file mode 100644 index 9fc50470..00000000 --- a/test/mocha/comment.js +++ /dev/null @@ -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")); - }); -}); diff --git a/test/mocha/comment_before_constant.js b/test/mocha/comment_before_constant.js deleted file mode 100644 index 9b69e078..00000000 --- a/test/mocha/comment_before_constant.js +++ /dev/null @@ -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}'); - }); -}); diff --git a/test/mocha/comments.js b/test/mocha/comments.js new file mode 100644 index 00000000..b350a406 --- /dev/null +++ b/test/mocha/comments.js @@ -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}"); + }); + }); +}); diff --git a/test/mocha/directives.js b/test/mocha/directives.js index 256bb870..0010e9b0 100644 --- a/test/mocha/directives.js +++ b/test/mocha/directives.js @@ -1,9 +1,9 @@ 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); @@ -161,13 +161,13 @@ describe("Directives", function() { 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 (])"); } @@ -186,7 +186,7 @@ describe("Directives", function() { ["'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]); @@ -195,7 +195,7 @@ describe("Directives", function() { 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, @@ -225,7 +225,7 @@ describe("Directives", function() { 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] ); @@ -233,7 +233,7 @@ describe("Directives", 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} ).code; assert.strictEqual(code, '"use strict";;"use strict"\n'); @@ -340,7 +340,7 @@ describe("Directives", function() { ]; 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] ); @@ -372,7 +372,7 @@ describe("Directives", function() { 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] ); diff --git a/test/mocha/getter-setter.js b/test/mocha/getter-setter.js index cd4433e0..84e7e30d 100644 --- a/test/mocha/getter-setter.js +++ b/test/mocha/getter-setter.js @@ -1,5 +1,5 @@ -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() { diff --git a/test/mocha/huge-number-of-comments.js b/test/mocha/huge-number-of-comments.js deleted file mode 100644 index a58f8d0a..00000000 --- a/test/mocha/huge-number-of-comments.js +++ /dev/null @@ -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}"); - }); -}); diff --git a/test/mocha/ie8.js b/test/mocha/ie8.js index 8f3a27fa..2187047b 100644 --- a/test/mocha/ie8.js +++ b/test/mocha/ie8.js @@ -1,10 +1,10 @@ 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';", diff --git a/test/mocha/input-sourcemaps.js b/test/mocha/input-sourcemaps.js deleted file mode 100644 index 22c4a232..00000000 --- a/test/mocha/input-sourcemaps.js +++ /dev/null @@ -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); - }); - }); -}); diff --git a/test/mocha/let.js b/test/mocha/let.js index 8685746b..09e84b74 100644 --- a/test/mocha/let.js +++ b/test/mocha/let.js @@ -1,5 +1,5 @@ -var Uglify = require('../../'); var assert = require("assert"); +var UglifyJS = require("../.."); describe("let", function() { this.timeout(30000); @@ -10,7 +10,7 @@ describe("let", function() { s += "var v" + i + "=0;"; } s += '}'; - var result = Uglify.minify(s, { + var result = UglifyJS.minify(s, { compress: false }).code; @@ -39,7 +39,7 @@ describe("let", function() { 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: { diff --git a/test/mocha/line-endings.js b/test/mocha/line-endings.js index 6b666813..07e85faf 100644 --- a/test/mocha/line-endings.js +++ b/test/mocha/line-endings.js @@ -1,5 +1,5 @@ -var Uglify = require("../node"); var assert = require("assert"); +var UglifyJS = require("../node"); describe("line-endings", function() { var options = { @@ -14,19 +14,19 @@ describe("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 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); }); @@ -44,16 +44,15 @@ describe("line-endings", function() { ] 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); } }); }); - diff --git a/test/mocha/minify-file-map.js b/test/mocha/minify-file-map.js index cae5ccb7..cffb2c62 100644 --- a/test/mocha/minify-file-map.js +++ b/test/mocha/minify-file-map.js @@ -1,22 +1,22 @@ -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'); }); @@ -26,7 +26,7 @@ describe("Input file as map", function() { '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;'); @@ -37,7 +37,7 @@ describe("Input file as map", function() { 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};'); diff --git a/test/mocha/minify.js b/test/mocha/minify.js index f394cc62..1871ed71 100644 --- a/test/mocha/minify.js +++ b/test/mocha/minify.js @@ -1,7 +1,7 @@ -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"); @@ -10,14 +10,14 @@ function read(path) { 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);"); }); @@ -32,7 +32,7 @@ describe("minify", function() { "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 @@ -65,7 +65,7 @@ describe("minify", function() { "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 }, @@ -96,7 +96,7 @@ describe("minify", function() { '"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, @@ -117,15 +117,15 @@ describe("minify", function() { }); 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 }}); @@ -134,7 +134,7 @@ describe("minify", function() { 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 @@ -144,7 +144,7 @@ describe("minify", function() { 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 @@ -156,7 +156,7 @@ describe("minify", function() { 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 }, @@ -174,7 +174,7 @@ describe("minify", function() { '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 @@ -188,7 +188,7 @@ describe("minify", function() { 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, @@ -198,7 +198,7 @@ describe("minify", function() { 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, @@ -211,7 +211,7 @@ describe("minify", function() { 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 «,»"); @@ -220,7 +220,7 @@ describe("minify", function() { 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"); @@ -232,7 +232,7 @@ describe("minify", function() { 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" @@ -246,7 +246,7 @@ describe("minify", function() { 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 @@ -266,7 +266,7 @@ describe("minify", function() { "}", "f();", ].join("\n"); - var ast = Uglify.minify(code, { + var ast = UglifyJS.minify(code, { compress: false, mangle: false, output: { @@ -279,7 +279,7 @@ describe("minify", function() { 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 }, @@ -294,7 +294,7 @@ describe("minify", function() { 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, }, @@ -307,7 +307,7 @@ describe("minify", function() { 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, @@ -316,7 +316,7 @@ describe("minify", function() { 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, @@ -325,7 +325,7 @@ describe("minify", function() { 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, @@ -334,7 +334,7 @@ describe("minify", function() { 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, diff --git a/test/mocha/number-literal.js b/test/mocha/number-literal.js index eb2b407d..c7560eb5 100644 --- a/test/mocha/number-literal.js +++ b/test/mocha/number-literal.js @@ -1,5 +1,5 @@ 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() { @@ -9,11 +9,11 @@ describe("Number literals", 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++) { diff --git a/test/mocha/operator.js b/test/mocha/operator.js index 64db02c1..d36d3a1d 100644 --- a/test/mocha/operator.js +++ b/test/mocha/operator.js @@ -1,5 +1,5 @@ -var UglifyJS = require("../node"); var assert = require("assert"); +var UglifyJS = require("../.."); describe("operator", function() { it("Should handle mixing of ++/+/--/- correctly", function() { diff --git a/test/mocha/parentheses.js b/test/mocha/parentheses.js index 7646e284..373db2da 100644 --- a/test/mocha/parentheses.js +++ b/test/mocha/parentheses.js @@ -1,5 +1,5 @@ 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() { @@ -33,7 +33,7 @@ describe("parentheses", 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 @@ -74,7 +74,7 @@ describe("parentheses", function() { ]; 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 @@ -94,7 +94,7 @@ describe("parentheses", function() { code = code.concat(code); } code = code.join(""); - var result = uglify.minify(code, { + var result = UglifyJS.minify(code, { compress: false, mangle: false, }); diff --git a/test/mocha/sourcemaps.js b/test/mocha/sourcemaps.js index 8590b308..db79a64a 100644 --- a/test/mocha/sourcemaps.js +++ b/test/mocha/sourcemaps.js @@ -1,19 +1,52 @@ 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;"); @@ -36,7 +69,7 @@ describe("sourcemaps", function() { }); it("Should mark array/object literals", function() { - var result = Uglify.minify([ + var result = UglifyJS.minify([ "var obj = {};", "obj.wat([]);", ].join("\n"), { @@ -50,7 +83,7 @@ describe("sourcemaps", function() { 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/", }, @@ -61,8 +94,8 @@ describe("sourcemaps", function() { }); 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", @@ -76,7 +109,7 @@ describe("sourcemaps", function() { 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", @@ -88,13 +121,13 @@ describe("sourcemaps", function() { 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" @@ -104,17 +137,17 @@ describe("sourcemaps", function() { 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"), ], { @@ -131,11 +164,11 @@ describe("sourcemaps", function() { 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: { @@ -146,7 +179,7 @@ describe("sourcemaps", function() { 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: { @@ -161,7 +194,7 @@ describe("sourcemaps", function() { 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" } @@ -172,13 +205,13 @@ describe("sourcemaps", function() { "//# 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 }, @@ -194,7 +227,7 @@ describe("sourcemaps", function() { "var tëst = '→unicøde←';", "alert(tëst);", ].join("\n"); - var result = Uglify.minify(code, { + var result = UglifyJS.minify(code, { sourceMap: { includeSources: true, url: "inline", @@ -208,7 +241,7 @@ describe("sourcemaps", function() { 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, @@ -221,4 +254,37 @@ describe("sourcemaps", function() { 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); + }); + }); + }); }); diff --git a/test/mocha/spidermonkey.js b/test/mocha/spidermonkey.js index 756a2f43..fc4f40cb 100644 --- a/test/mocha/spidermonkey.js +++ b/test/mocha/spidermonkey.js @@ -1,6 +1,6 @@ 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) { @@ -25,9 +25,9 @@ describe("spidermonkey export/import sanity test", function() { 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 ); }); @@ -99,10 +99,10 @@ describe("spidermonkey export/import sanity test", function() { 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++; } }); @@ -111,9 +111,9 @@ describe("spidermonkey export/import sanity test", function() { 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); diff --git a/test/mocha/string-literal.js b/test/mocha/string-literal.js index f595f29a..aca915b7 100644 --- a/test/mocha/string-literal.js +++ b/test/mocha/string-literal.js @@ -1,5 +1,5 @@ -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() { @@ -18,8 +18,8 @@ describe("String literals", 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) { @@ -48,8 +48,8 @@ describe("String literals", function() { }; 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) { diff --git a/test/mocha/accessorTokens-1492.js b/test/mocha/tokens.js index 250d9b97..6241f25d 100644 --- a/test/mocha/accessorTokens-1492.js +++ b/test/mocha/tokens.js @@ -1,32 +1,26 @@ -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 +}); diff --git a/test/mocha/with.js b/test/mocha/with.js index 72fd76ef..c6283800 100644 --- a/test/mocha/with.js +++ b/test/mocha/with.js @@ -1,20 +1,20 @@ 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); |