speed up OutputStream (#4844)
This commit is contained in:
115
lib/output.js
115
lib/output.js
@@ -101,44 +101,32 @@ function OutputStream(options) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var OUTPUT;
|
var current_col = 0;
|
||||||
var current_col;
|
var current_line = 1;
|
||||||
var current_line;
|
var current_pos = 0;
|
||||||
var current_pos;
|
var indentation = options.indent_start;
|
||||||
var has_parens;
|
|
||||||
var indentation;
|
|
||||||
var last;
|
var last;
|
||||||
var line_end;
|
var line_end = 0;
|
||||||
var line_fixed;
|
var line_fixed = true;
|
||||||
var mappings;
|
var mappings = options.source_map && [];
|
||||||
var mapping_name;
|
var mapping_name;
|
||||||
var mapping_token;
|
var mapping_token;
|
||||||
var might_need_space;
|
var might_need_space;
|
||||||
var might_need_semicolon;
|
var might_need_semicolon;
|
||||||
var need_newline_indented;
|
var need_newline_indented = false;
|
||||||
var need_space;
|
var need_space = false;
|
||||||
var newline_insert;
|
var newline_insert = -1;
|
||||||
var stack;
|
var stack;
|
||||||
|
var OUTPUT;
|
||||||
|
|
||||||
function reset() {
|
function reset() {
|
||||||
OUTPUT = "";
|
|
||||||
current_col = 0;
|
|
||||||
current_line = 1;
|
|
||||||
current_pos = 0;
|
|
||||||
has_parens = false;
|
|
||||||
indentation = options.indent_start;
|
|
||||||
last = "";
|
last = "";
|
||||||
line_end = 0;
|
|
||||||
line_fixed = true;
|
|
||||||
mappings = options.source_map && [];
|
|
||||||
mapping_name = null;
|
|
||||||
mapping_token = null;
|
|
||||||
might_need_space = false;
|
might_need_space = false;
|
||||||
might_need_semicolon = false;
|
might_need_semicolon = false;
|
||||||
need_newline_indented = false;
|
|
||||||
need_space = false;
|
|
||||||
newline_insert = -1;
|
|
||||||
stack = [];
|
stack = [];
|
||||||
|
var str = OUTPUT;
|
||||||
|
OUTPUT = "";
|
||||||
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
reset();
|
reset();
|
||||||
@@ -266,8 +254,14 @@ function OutputStream(options) {
|
|||||||
|
|
||||||
var requireSemicolonChars = makePredicate("( [ + * / - , .");
|
var requireSemicolonChars = makePredicate("( [ + * / - , .");
|
||||||
|
|
||||||
function print(str) {
|
var print = options.beautify
|
||||||
str = String(str);
|
|| options.comments
|
||||||
|
|| options.max_line_len
|
||||||
|
|| options.preserve_line
|
||||||
|
|| options.shebang
|
||||||
|
|| !options.semicolons
|
||||||
|
|| options.source_map
|
||||||
|
|| options.width ? function(str) {
|
||||||
var ch = str.charAt(0);
|
var ch = str.charAt(0);
|
||||||
if (need_newline_indented && ch) {
|
if (need_newline_indented && ch) {
|
||||||
need_newline_indented = false;
|
need_newline_indented = false;
|
||||||
@@ -337,7 +331,6 @@ function OutputStream(options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
OUTPUT += str;
|
OUTPUT += str;
|
||||||
has_parens = str.slice(-1) == "(";
|
|
||||||
current_pos += str.length;
|
current_pos += str.length;
|
||||||
var a = str.split(/\r?\n/), n = a.length - 1;
|
var a = str.split(/\r?\n/), n = a.length - 1;
|
||||||
current_line += n;
|
current_line += n;
|
||||||
@@ -347,7 +340,30 @@ function OutputStream(options) {
|
|||||||
current_col = a[n].length;
|
current_col = a[n].length;
|
||||||
}
|
}
|
||||||
last = str;
|
last = str;
|
||||||
|
} : function(str) {
|
||||||
|
var ch = str.charAt(0);
|
||||||
|
var prev = last.slice(-1);
|
||||||
|
if (might_need_semicolon) {
|
||||||
|
might_need_semicolon = false;
|
||||||
|
if (prev == ":" && ch == "}" || (!ch || ";}".indexOf(ch) < 0) && prev != ";") {
|
||||||
|
OUTPUT += ";";
|
||||||
|
might_need_space = false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if (might_need_space) {
|
||||||
|
if (is_identifier_char(prev) && (is_identifier_char(ch) || ch == "\\")
|
||||||
|
|| (ch == "/" && ch == prev)
|
||||||
|
|| ((ch == "+" || ch == "-") && ch == last)
|
||||||
|
|| str == "--" && last == "!"
|
||||||
|
|| str == "in" && prev == "/"
|
||||||
|
|| last == "--" && ch == ">") {
|
||||||
|
OUTPUT += " ";
|
||||||
|
}
|
||||||
|
if (prev != "<" || str != "!") might_need_space = false;
|
||||||
|
}
|
||||||
|
OUTPUT += str;
|
||||||
|
last = str;
|
||||||
|
};
|
||||||
|
|
||||||
var space = options.beautify ? function() {
|
var space = options.beautify ? function() {
|
||||||
print(" ");
|
print(" ");
|
||||||
@@ -551,10 +567,10 @@ function OutputStream(options) {
|
|||||||
get : get,
|
get : get,
|
||||||
reset : reset,
|
reset : reset,
|
||||||
indent : indent,
|
indent : indent,
|
||||||
should_break : function() {
|
should_break : options.width ? function() {
|
||||||
return options.width && current_col - indentation >= options.width;
|
return current_col - indentation >= options.width;
|
||||||
},
|
} : return_false,
|
||||||
has_parens : function() { return has_parens },
|
has_parens : function() { return last.slice(-1) == "(" },
|
||||||
newline : newline,
|
newline : newline,
|
||||||
print : print,
|
print : print,
|
||||||
space : space,
|
space : space,
|
||||||
@@ -577,11 +593,8 @@ function OutputStream(options) {
|
|||||||
with_square : with_square,
|
with_square : with_square,
|
||||||
add_mapping : add_mapping,
|
add_mapping : add_mapping,
|
||||||
option : function(opt) { return options[opt] },
|
option : function(opt) { return options[opt] },
|
||||||
prepend_comments: prepend_comments,
|
prepend_comments: options.comments || options.shebang ? prepend_comments : noop,
|
||||||
append_comments : comment_filter === return_false ? noop : append_comments,
|
append_comments : options.comments ? append_comments : noop,
|
||||||
line : function() { return current_line },
|
|
||||||
col : function() { return current_col },
|
|
||||||
pos : function() { return current_pos },
|
|
||||||
push_node : function(node) { stack.push(node) },
|
push_node : function(node) { stack.push(node) },
|
||||||
pop_node : options.preserve_line ? function() {
|
pop_node : options.preserve_line ? function() {
|
||||||
var node = stack.pop();
|
var node = stack.pop();
|
||||||
@@ -628,9 +641,9 @@ function OutputStream(options) {
|
|||||||
});
|
});
|
||||||
var readonly = OutputStream({
|
var readonly = OutputStream({
|
||||||
inline_script: false,
|
inline_script: false,
|
||||||
|
shebang: false,
|
||||||
|
width: false,
|
||||||
});
|
});
|
||||||
readonly.prepend_comments = noop;
|
|
||||||
readonly.should_break = return_false;
|
|
||||||
AST_Node.DEFMETHOD("print_to_string", function(options) {
|
AST_Node.DEFMETHOD("print_to_string", function(options) {
|
||||||
if (options) {
|
if (options) {
|
||||||
var stream = OutputStream(options);
|
var stream = OutputStream(options);
|
||||||
@@ -638,9 +651,7 @@ function OutputStream(options) {
|
|||||||
return stream.get();
|
return stream.get();
|
||||||
}
|
}
|
||||||
this.print(readonly);
|
this.print(readonly);
|
||||||
var str = readonly.get();
|
return readonly.reset();
|
||||||
readonly.reset();
|
|
||||||
return str;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
/* -----[ PARENTHESES ]----- */
|
/* -----[ PARENTHESES ]----- */
|
||||||
@@ -1489,11 +1500,7 @@ function OutputStream(options) {
|
|||||||
output.print_string(prop);
|
output.print_string(prop);
|
||||||
output.print("]");
|
output.print("]");
|
||||||
} else {
|
} else {
|
||||||
if (expr instanceof AST_Number && expr.value >= 0) {
|
if (expr instanceof AST_Number && !/[ex.)]/i.test(output.last())) output.print(".");
|
||||||
if (!/[xa-f.)]/i.test(output.last())) {
|
|
||||||
output.print(".");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
output.print(".");
|
output.print(".");
|
||||||
// the name after dot would be mapped about here.
|
// the name after dot would be mapped about here.
|
||||||
output.add_mapping(self.end);
|
output.add_mapping(self.end);
|
||||||
@@ -1743,7 +1750,7 @@ function OutputStream(options) {
|
|||||||
output.print("`");
|
output.print("`");
|
||||||
});
|
});
|
||||||
DEFPRINT(AST_Constant, function(output) {
|
DEFPRINT(AST_Constant, function(output) {
|
||||||
output.print(this.value);
|
output.print("" + this.value);
|
||||||
});
|
});
|
||||||
DEFPRINT(AST_String, function(output) {
|
DEFPRINT(AST_String, function(output) {
|
||||||
output.print_string(this.value, this.quote);
|
output.print_string(this.value, this.quote);
|
||||||
@@ -1799,7 +1806,7 @@ function OutputStream(options) {
|
|||||||
function force_statement(stat, output) {
|
function force_statement(stat, output) {
|
||||||
if (output.option("braces") && !(stat instanceof AST_Const || stat instanceof AST_Let)) {
|
if (output.option("braces") && !(stat instanceof AST_Const || stat instanceof AST_Let)) {
|
||||||
make_block(stat, output);
|
make_block(stat, output);
|
||||||
} else if (!stat || stat instanceof AST_EmptyStatement) {
|
} else if (stat instanceof AST_EmptyStatement) {
|
||||||
output.force_semicolon();
|
output.force_semicolon();
|
||||||
} else {
|
} else {
|
||||||
stat.print(output);
|
stat.print(output);
|
||||||
@@ -1850,11 +1857,11 @@ function OutputStream(options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function make_block(stmt, output) {
|
function make_block(stmt, output) {
|
||||||
if (!stmt || stmt instanceof AST_EmptyStatement)
|
if (stmt instanceof AST_EmptyStatement) {
|
||||||
output.print("{}");
|
print_braced_empty(stmt, output);
|
||||||
else if (stmt instanceof AST_BlockStatement)
|
} else if (stmt instanceof AST_BlockStatement) {
|
||||||
stmt.print(output);
|
stmt.print(output);
|
||||||
else output.with_block(function() {
|
} else output.with_block(function() {
|
||||||
output.indent();
|
output.indent();
|
||||||
stmt.print(output);
|
stmt.print(output);
|
||||||
output.newline();
|
output.newline();
|
||||||
|
|||||||
Reference in New Issue
Block a user