avoid collision with HTML comments (#3625)

fixes #3624
This commit is contained in:
Alex Lam S.L
2019-12-05 02:43:25 +08:00
committed by GitHub
parent bf7e4ca1a3
commit 04fbb1f949
2 changed files with 88 additions and 55 deletions

View File

@@ -269,7 +269,7 @@ function OutputStream(options) {
} }
} }
newline_insert = -1; newline_insert = -1;
var prev = last.charAt(last.length - 1); var prev = last.slice(-1);
if (might_need_semicolon) { if (might_need_semicolon) {
might_need_semicolon = false; might_need_semicolon = false;
@@ -298,16 +298,16 @@ function OutputStream(options) {
} }
if (might_need_space) { if (might_need_space) {
if ((is_identifier_char(prev) if (is_identifier_char(prev) && (is_identifier_char(ch) || ch == "\\")
&& (is_identifier_char(ch) || ch == "\\"))
|| (ch == "/" && ch == prev) || (ch == "/" && ch == prev)
|| ((ch == "+" || ch == "-") && ch == last)) || ((ch == "+" || ch == "-") && ch == last)
{ || str == "--" && last == "!"
|| last == "--" && ch == ">") {
OUTPUT += " "; OUTPUT += " ";
current_col++; current_col++;
current_pos++; current_pos++;
} }
might_need_space = false; if (prev != "<" || str != "!") might_need_space = false;
} }
if (mapping_token) { if (mapping_token) {
@@ -322,7 +322,7 @@ function OutputStream(options) {
} }
OUTPUT += str; OUTPUT += str;
has_parens = str[str.length - 1] == "("; 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;
@@ -1254,29 +1254,10 @@ function OutputStream(options) {
output.print(self.operator); output.print(self.operator);
}); });
DEFPRINT(AST_Binary, function(self, output) { DEFPRINT(AST_Binary, function(self, output) {
var op = self.operator;
self.left.print(output); self.left.print(output);
if (op[0] == ">" /* ">>" ">>>" ">" ">=" */ output.space();
&& self.left instanceof AST_UnaryPostfix output.print(self.operator);
&& self.left.operator == "--") { output.space();
// space is mandatory to avoid outputting -->
output.print(" ");
} else {
// the space is optional depending on "beautify"
output.space();
}
output.print(op);
if ((op == "<" || op == "<<")
&& self.right instanceof AST_UnaryPrefix
&& self.right.operator == "!"
&& self.right.expression instanceof AST_UnaryPrefix
&& self.right.expression.operator == "--") {
// space is mandatory to avoid outputting <!--
output.print(" ");
} else {
// the space is optional depending on "beautify"
output.space();
}
self.right.print(output); self.right.print(output);
}); });
DEFPRINT(AST_Conditional, function(self, output) { DEFPRINT(AST_Conditional, function(self, output) {

View File

@@ -1,55 +1,107 @@
html_comment_in_expression: { html_comment_in_expression: {
input: { input: {
function f(a, b, x, y) { return a < !--b && x-- > y; } (function(a, b) {
console.log(a < !--b && a-- > b, a, b);
})(1, 2);
} }
expect_exact: "function f(a,b,x,y){return a< !--b&&x-- >y}"; expect_exact: "(function(a,b){console.log(a<! --b&&a-- >b,a,b)})(1,2);"
expect_stdout: "false 1 1"
} }
html_comment_in_less_than: { html_comment_in_less_than: {
input: { input: {
function f(a, b) { return a < !--b; } (function(a, b, c) {
console.log(
a < !--b,
a < !--b + c,
a + b < !--c,
a, b, c
);
})(1, 2, 3);
} }
expect_exact: "function f(a,b){return a< !--b}"; expect_exact: "(function(a,b,c){console.log(a<! --b,a<! --b+c,a+b<! --c,a,b,c)})(1,2,3);"
expect_stdout: "false true false 1 0 2"
} }
html_comment_in_left_shift: { html_comment_in_left_shift: {
input: { input: {
function f(a, b) { return a << !--b; } (function(a, b, c) {
console.log(
a << !--b,
a << !--b + c,
a + b << !--c,
a, b, c
);
})(1, 2, 3);
} }
expect_exact: "function f(a,b){return a<< !--b}"; expect_exact: "(function(a,b,c){console.log(a<<! --b,a<<! --b+c,a+b<<! --c,a,b,c)})(1,2,3);"
} expect_stdout: "1 16 1 1 0 2"
html_comment_in_right_shift: {
input: {
function f(a, b) { return a-- >> b; }
}
expect_exact: "function f(a,b){return a-- >>b}";
}
html_comment_in_zero_fill_right_shift: {
input: {
function f(a, b) { return a-- >>> b; }
}
expect_exact: "function f(a,b){return a-- >>>b}";
} }
html_comment_in_greater_than: { html_comment_in_greater_than: {
input: { input: {
function f(a, b) { return a-- > b; } (function(a, b, c) {
console.log(
a-- > b,
a-- > b + c,
a + b-- > c,
a, b, c
);
})(1, 2, 3);
} }
expect_exact: "function f(a,b){return a-- >b}"; expect_exact: "(function(a,b,c){console.log(a-- >b,a-- >b+c,a+b-- >c,a,b,c)})(1,2,3);"
expect_stdout: "false false false -1 1 3"
} }
html_comment_in_greater_than_or_equal: { html_comment_in_greater_than_or_equal: {
input: { input: {
function f(a, b) { return a-- >= b; } (function(a, b, c) {
console.log(
a-- >= b,
a-- >= b + c,
a + b-- >= c,
a, b, c
);
})(1, 2, 3);
} }
expect_exact: "function f(a,b){return a-- >=b}"; expect_exact: "(function(a,b,c){console.log(a-- >=b,a-- >=b+c,a+b-- >=c,a,b,c)})(1,2,3);"
expect_stdout: "false false false -1 1 3"
}
html_comment_in_right_shift: {
input: {
(function(a, b, c) {
console.log(
a-- >> b,
a-- >> b + c,
a + b-- >> c,
a, b, c
);
})(1, 2, 3);
}
expect_exact: "(function(a,b,c){console.log(a-- >>b,a-- >>b+c,a+b-- >>c,a,b,c)})(1,2,3);"
expect_stdout: "0 0 0 -1 1 3"
}
html_comment_in_zero_fill_right_shift: {
input: {
(function(a, b, c) {
console.log(
a-- >>> b,
a-- >>> b + c,
a + b-- >>> c,
a, b, c
);
})(1, 2, 3);
}
expect_exact: "(function(a,b,c){console.log(a-- >>>b,a-- >>>b+c,a+b-- >>>c,a,b,c)})(1,2,3);"
expect_stdout: "0 0 0 -1 1 3"
} }
html_comment_in_string_literal: { html_comment_in_string_literal: {
input: { input: {
function f() { return "<!--HTML-->comment in<!--string literal-->"; } console.log("<!--HTML-->comment in<!--string literal-->".length);
} }
expect_exact: 'function f(){return"\\x3c!--HTML--\\x3ecomment in\\x3c!--string literal--\\x3e"}'; expect_exact: 'console.log("\\x3c!--HTML--\\x3ecomment in\\x3c!--string literal--\\x3e".length);'
expect_stdout: "42"
} }