fix whitespace around comments
This commit is contained in:
parent
e3d99ef1e0
commit
11f1beb337
|
|
@ -201,6 +201,8 @@ function OutputStream(options) {
|
||||||
var might_need_semicolon = false;
|
var might_need_semicolon = false;
|
||||||
var might_add_newline = 0;
|
var might_add_newline = 0;
|
||||||
var need_newline_indented = false;
|
var need_newline_indented = false;
|
||||||
|
var need_space = false;
|
||||||
|
var newline_insert = -1;
|
||||||
var last = "";
|
var last = "";
|
||||||
var mapping_token, mapping_name, mappings = options.source_map && [];
|
var mapping_token, mapping_name, mappings = options.source_map && [];
|
||||||
|
|
||||||
|
|
@ -266,6 +268,13 @@ function OutputStream(options) {
|
||||||
indent();
|
indent();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (need_space && ch) {
|
||||||
|
need_space = false;
|
||||||
|
if (!/[\s;})]/.test(ch)) {
|
||||||
|
space();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
newline_insert = -1;
|
||||||
var prev = last.charAt(last.length - 1);
|
var prev = last.charAt(last.length - 1);
|
||||||
if (might_need_semicolon) {
|
if (might_need_semicolon) {
|
||||||
might_need_semicolon = false;
|
might_need_semicolon = false;
|
||||||
|
|
@ -364,7 +373,13 @@ function OutputStream(options) {
|
||||||
} : function(col, cont) { return cont() };
|
} : function(col, cont) { return cont() };
|
||||||
|
|
||||||
var newline = options.beautify ? function() {
|
var newline = options.beautify ? function() {
|
||||||
print("\n");
|
if (newline_insert < 0) return print("\n");
|
||||||
|
if (OUTPUT[newline_insert] != "\n") {
|
||||||
|
OUTPUT = OUTPUT.slice(0, newline_insert) + "\n" + OUTPUT.slice(newline_insert);
|
||||||
|
current_pos++;
|
||||||
|
current_line++;
|
||||||
|
}
|
||||||
|
newline_insert++;
|
||||||
} : options.max_line_len ? function() {
|
} : options.max_line_len ? function() {
|
||||||
ensure_line_len();
|
ensure_line_len();
|
||||||
might_add_newline = OUTPUT.length;
|
might_add_newline = OUTPUT.length;
|
||||||
|
|
@ -521,11 +536,16 @@ function OutputStream(options) {
|
||||||
var comments = token[tail ? "comments_before" : "comments_after"];
|
var comments = token[tail ? "comments_before" : "comments_after"];
|
||||||
if (comments && comments._dumped !== self) {
|
if (comments && comments._dumped !== self) {
|
||||||
comments._dumped = self;
|
comments._dumped = self;
|
||||||
|
var insert = OUTPUT.length;
|
||||||
comments.filter(comment_filter, node).forEach(function(c, i) {
|
comments.filter(comment_filter, node).forEach(function(c, i) {
|
||||||
if (need_newline_indented || c.nlb) {
|
need_space = false;
|
||||||
|
if (need_newline_indented) {
|
||||||
print("\n");
|
print("\n");
|
||||||
indent();
|
indent();
|
||||||
need_newline_indented = false;
|
need_newline_indented = false;
|
||||||
|
} else if (c.nlb && (i > 0 || !/(^|\n) *$/.test(OUTPUT))) {
|
||||||
|
print("\n");
|
||||||
|
indent();
|
||||||
} else if (i > 0 || !tail) {
|
} else if (i > 0 || !tail) {
|
||||||
space();
|
space();
|
||||||
}
|
}
|
||||||
|
|
@ -534,8 +554,10 @@ function OutputStream(options) {
|
||||||
need_newline_indented = true;
|
need_newline_indented = true;
|
||||||
} else if (c.type == "comment2") {
|
} else if (c.type == "comment2") {
|
||||||
print("/*" + c.value + "*/");
|
print("/*" + c.value + "*/");
|
||||||
|
need_space = true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
if (OUTPUT.length > insert) newline_insert = insert;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
10
lib/parse.js
10
lib/parse.js
|
|
@ -1276,9 +1276,17 @@ function parse($TEXT, options) {
|
||||||
case "(":
|
case "(":
|
||||||
next();
|
next();
|
||||||
var ex = expression(true);
|
var ex = expression(true);
|
||||||
start.comments_before.orig_len = start.comments_before.length;
|
var orig_len = start.comments_before.length;
|
||||||
|
start.comments_before.orig_len = orig_len;
|
||||||
[].push.apply(start.comments_before, ex.start.comments_before);
|
[].push.apply(start.comments_before, ex.start.comments_before);
|
||||||
ex.start.comments_before = start.comments_before;
|
ex.start.comments_before = start.comments_before;
|
||||||
|
if (orig_len == 0 && start.comments_before.length > 0) {
|
||||||
|
var comment = start.comments_before[0];
|
||||||
|
if (!comment.nlb) {
|
||||||
|
comment.nlb = start.nlb;
|
||||||
|
start.nlb = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
start.comments_after = ex.start.comments_after;
|
start.comments_after = ex.start.comments_after;
|
||||||
ex.start = start;
|
ex.start = start;
|
||||||
expect(")");
|
expect(")");
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user