2016-06-13 10:36:47 +00:00
|
|
|
var assert = require("assert");
|
2017-05-07 19:24:42 +00:00
|
|
|
var uglify = require("../node");
|
2016-06-13 10:36:47 +00:00
|
|
|
|
|
|
|
|
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 &&
|
2017-02-26 19:40:54 +00:00
|
|
|
e.message === "Unexpected token: operator (>)" &&
|
2016-06-13 10:36:47 +00:00
|
|
|
e.line === 2 &&
|
|
|
|
|
e.col === 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < tests.length; i++) {
|
|
|
|
|
assert.throws(function() {
|
2017-04-15 15:50:50 +00:00
|
|
|
uglify.parse(tests[i]);
|
2016-06-13 10:36:47 +00:00
|
|
|
}, 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 &&
|
2017-02-26 19:40:54 +00:00
|
|
|
e.message === "Unexpected token: operator (>)" &&
|
2016-06-13 10:36:47 +00:00
|
|
|
e.line === 5 &&
|
|
|
|
|
e.col === 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < tests.length; i++) {
|
|
|
|
|
assert.throws(function() {
|
2017-04-15 15:50:50 +00:00
|
|
|
uglify.parse(tests[i]);
|
2016-06-13 10:36:47 +00:00
|
|
|
}, fail, tests[i]);
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-12-21 09:39:00 +00:00
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
});
|
2016-06-13 10:36:47 +00:00
|
|
|
});
|