@@ -451,16 +451,11 @@ function OutputStream(options) {
|
|||||||
|
|
||||||
function prepend_comments(node) {
|
function prepend_comments(node) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var start = node.start;
|
var scan = node instanceof AST_Exit && node.value;
|
||||||
if (!start) return;
|
var comments = dump(node);
|
||||||
if (start.comments_before && start.comments_before._dumped === self) return;
|
if (!comments) return;
|
||||||
var comments = start.comments_before;
|
|
||||||
if (!comments) {
|
|
||||||
comments = start.comments_before = [];
|
|
||||||
}
|
|
||||||
comments._dumped = self;
|
|
||||||
|
|
||||||
if (node instanceof AST_Exit && node.value) {
|
if (scan) {
|
||||||
var tw = new TreeWalker(function(node) {
|
var tw = new TreeWalker(function(node) {
|
||||||
var parent = tw.parent();
|
var parent = tw.parent();
|
||||||
if (parent instanceof AST_Exit
|
if (parent instanceof AST_Exit
|
||||||
@@ -471,11 +466,8 @@ function OutputStream(options) {
|
|||||||
|| parent instanceof AST_Sequence && parent.expressions[0] === node
|
|| parent instanceof AST_Sequence && parent.expressions[0] === node
|
||||||
|| parent instanceof AST_Sub && parent.expression === node
|
|| parent instanceof AST_Sub && parent.expression === node
|
||||||
|| parent instanceof AST_UnaryPostfix) {
|
|| parent instanceof AST_UnaryPostfix) {
|
||||||
var text = node.start.comments_before;
|
var before = dump(node);
|
||||||
if (text && text._dumped !== self) {
|
if (before) comments = comments.concat(before);
|
||||||
text._dumped = self;
|
|
||||||
comments = comments.concat(text);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -518,13 +510,29 @@ function OutputStream(options) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (!last_nlb) {
|
if (!last_nlb) {
|
||||||
if (start.nlb) {
|
if (node.start.nlb) {
|
||||||
print("\n");
|
print("\n");
|
||||||
indent();
|
indent();
|
||||||
} else {
|
} else {
|
||||||
space();
|
space();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function dump(node) {
|
||||||
|
var token = node.start;
|
||||||
|
if (!token) {
|
||||||
|
if (!scan) return;
|
||||||
|
node.start = token = new AST_Token();
|
||||||
|
}
|
||||||
|
var comments = token.comments_before;
|
||||||
|
if (!comments) {
|
||||||
|
if (!scan) return;
|
||||||
|
token.comments_before = comments = [];
|
||||||
|
}
|
||||||
|
if (comments._dumped === self) return;
|
||||||
|
comments._dumped = self;
|
||||||
|
return comments;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function append_comments(node, tail) {
|
function append_comments(node, tail) {
|
||||||
|
|||||||
@@ -260,6 +260,23 @@ describe("comments", function() {
|
|||||||
].join("\n"));
|
].join("\n"));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("Should handle programmatic AST insertions gracefully", function() {
|
||||||
|
var ast = UglifyJS.parse([
|
||||||
|
"function f() {",
|
||||||
|
" //foo",
|
||||||
|
" bar;",
|
||||||
|
" return;",
|
||||||
|
"}",
|
||||||
|
].join("\n"));
|
||||||
|
ast.body[0].body[0] = new UglifyJS.AST_Throw({value: ast.body[0].body[0].body});
|
||||||
|
ast.body[0].body[1].value = new UglifyJS.AST_Number({value: 42});
|
||||||
|
assert.strictEqual(ast.print_to_string({comments: "all"}), [
|
||||||
|
"function f(){",
|
||||||
|
"//foo",
|
||||||
|
"throw bar;return 42}",
|
||||||
|
].join("\n"));
|
||||||
|
});
|
||||||
|
|
||||||
describe("comment before constant", function() {
|
describe("comment before constant", function() {
|
||||||
var js = 'function f() { /*c1*/ var /*c2*/ foo = /*c3*/ false; return foo; }';
|
var js = 'function f() { /*c1*/ var /*c2*/ foo = /*c3*/ false; return foo; }';
|
||||||
|
|
||||||
|
|||||||
@@ -219,8 +219,9 @@ function run_compress_tests() {
|
|||||||
var input_code = make_code(input);
|
var input_code = make_code(input);
|
||||||
var input_formatted = make_code(test.input, {
|
var input_formatted = make_code(test.input, {
|
||||||
beautify: true,
|
beautify: true,
|
||||||
|
comments: "all",
|
||||||
|
keep_quoted_props: true,
|
||||||
quote_style: 3,
|
quote_style: 3,
|
||||||
keep_quoted_props: true
|
|
||||||
});
|
});
|
||||||
try {
|
try {
|
||||||
U.parse(input_code);
|
U.parse(input_code);
|
||||||
|
|||||||
Reference in New Issue
Block a user