fix corner case in max_line_len (#5313)

- speed up `max_line_len` & `preserve_line`
This commit is contained in:
Alex Lam S.L
2022-01-26 15:47:21 +00:00
committed by GitHub
parent 5c863b74d7
commit 371d25944d
4 changed files with 100 additions and 68 deletions

View File

@@ -103,7 +103,6 @@ function OutputStream(options) {
var current_col = 0;
var current_line = 1;
var current_pos = 0;
var indentation = options.indent_start;
var last;
var line_end = 0;
@@ -115,17 +114,17 @@ function OutputStream(options) {
var might_need_semicolon;
var need_newline_indented = false;
var need_space = false;
var newline_insert = -1;
var output;
var stack;
var OUTPUT;
var stored = "";
function reset() {
last = "";
might_need_space = false;
might_need_semicolon = false;
stack = [];
var str = OUTPUT;
OUTPUT = "";
var str = output;
output = "";
return str;
}
@@ -227,32 +226,30 @@ function OutputStream(options) {
} : noop;
function insert_newlines(count) {
var index = OUTPUT.lastIndexOf("\n");
if (line_end < index) line_end = index;
var left = OUTPUT.slice(0, line_end);
var right = OUTPUT.slice(line_end);
adjust_mappings(count, right.length - current_col);
stored += output.slice(0, line_end);
output = output.slice(line_end);
var new_col = output.length;
adjust_mappings(count, new_col - current_col);
current_line += count;
current_pos += count;
current_col = right.length;
OUTPUT = left;
while (count--) OUTPUT += "\n";
OUTPUT += right;
current_col = new_col;
while (count--) stored += "\n";
}
var fix_line = options.max_line_len ? function() {
var fix_line = options.max_line_len ? function(flush) {
if (line_fixed) {
if (current_col > options.max_line_len) {
AST_Node.warn("Output exceeds {max_line_len} characters", options);
}
return;
}
if (current_col > options.max_line_len) insert_newlines(1);
line_fixed = true;
flush_mappings();
if (current_col > options.max_line_len) {
insert_newlines(1);
line_fixed = true;
}
if (line_fixed || flush) flush_mappings();
} : noop;
var requireSemicolonChars = makePredicate("( [ + * / - , .");
var require_semicolon = makePredicate("( [ + * / - , .");
var print = options.beautify
|| options.comments
@@ -276,32 +273,32 @@ function OutputStream(options) {
space();
}
}
newline_insert = -1;
var prev = last.slice(-1);
if (might_need_semicolon) {
might_need_semicolon = false;
if (prev == ":" && ch == "}" || (!ch || ";}".indexOf(ch) < 0) && prev != ";") {
if (options.semicolons || requireSemicolonChars[ch]) {
OUTPUT += ";";
if (prev == ":" && ch == "}" || prev != ";" && (!ch || ";}".indexOf(ch) < 0)) {
var need_semicolon = require_semicolon[ch];
if (need_semicolon || options.semicolons) {
output += ";";
current_col++;
current_pos++;
if (!line_fixed) {
fix_line();
if (line_fixed && !need_semicolon && output == ";") {
output = "";
current_col = 0;
}
}
if (line_end == output.length - 1) line_end++;
} else {
fix_line();
OUTPUT += "\n";
current_pos++;
output += "\n";
current_line++;
current_col = 0;
if (/^\s+$/.test(str)) {
// reset the semicolon flag, since we didn't print one
// now and might still have to later
might_need_semicolon = true;
}
// reset the semicolon flag, since we didn't print one
// now and might still have to later
if (/^\s+$/.test(str)) might_need_semicolon = true;
}
if (!options.beautify)
might_need_space = false;
if (!options.beautify) might_need_space = false;
}
}
@@ -312,9 +309,8 @@ function OutputStream(options) {
|| str == "--" && last == "!"
|| str == "in" && prev == "/"
|| last == "--" && ch == ">") {
OUTPUT += " ";
output += " ";
current_col++;
current_pos++;
}
if (prev != "<" || str != "!") might_need_space = false;
}
@@ -324,14 +320,13 @@ function OutputStream(options) {
token: mapping_token,
name: mapping_name,
line: current_line,
col: current_col
col: current_col,
});
mapping_token = false;
if (line_fixed) flush_mappings();
}
OUTPUT += str;
current_pos += str.length;
output += str;
var a = str.split(/\r?\n/), n = a.length - 1;
current_line += n;
current_col += a[0].length;
@@ -346,7 +341,7 @@ function OutputStream(options) {
if (might_need_semicolon) {
might_need_semicolon = false;
if (prev == ":" && ch == "}" || (!ch || ";}".indexOf(ch) < 0) && prev != ";") {
OUTPUT += ";";
output += ";";
might_need_space = false;
}
}
@@ -357,11 +352,11 @@ function OutputStream(options) {
|| str == "--" && last == "!"
|| str == "in" && prev == "/"
|| last == "--" && ch == ">") {
OUTPUT += " ";
output += " ";
}
if (prev != "<" || str != "!") might_need_space = false;
}
OUTPUT += str;
output += str;
last = str;
};
@@ -385,18 +380,13 @@ function OutputStream(options) {
var may_add_newline = options.max_line_len || options.preserve_line ? function() {
fix_line();
line_end = OUTPUT.length;
line_end = output.length;
line_fixed = false;
} : noop;
var newline = options.beautify ? function() {
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++;
print("\n");
line_end = output.length;
} : may_add_newline;
var semicolon = options.beautify ? function() {
@@ -452,13 +442,12 @@ function OutputStream(options) {
} : noop;
function get() {
if (!line_fixed) fix_line();
return OUTPUT;
if (!line_fixed) fix_line(true);
return stored + output;
}
function has_nlb() {
var index = OUTPUT.lastIndexOf("\n");
return /^ *$/.test(OUTPUT.slice(index + 1));
return /(^|\n) *$/.test(output);
}
function pad_comment(token, force) {
@@ -515,7 +504,7 @@ function OutputStream(options) {
scan.walk(tw);
}
if (current_pos == 0) {
if (current_line == 1 && current_col == 0) {
if (comments.length > 0 && options.shebang && comments[0].type == "comment5") {
print("#!" + comments.shift().value + "\n");
indent();
@@ -559,19 +548,17 @@ function OutputStream(options) {
return !/comment[134]/.test(c.type);
}))) return;
comments._dumped = self;
var insert = OUTPUT.length;
comments.filter(comment_filter, node).forEach(function(comment, index) {
pad_comment(comment, index || !tail);
print_comment(comment);
});
if (OUTPUT.length > insert) newline_insert = insert;
}
return {
get : get,
reset : reset,
indent : indent,
should_break : options.width ? function() {
should_break : options.beautify && options.width ? function() {
return current_col - indentation >= options.width;
} : return_false,
has_parens : function() { return last.slice(-1) == "(" },