parse LF & comment correctly (#2653)

fixes #2652
This commit is contained in:
Alex Lam S.L
2017-12-26 01:38:01 +08:00
committed by GitHub
parent 8701a99a15
commit 0fe259e9c5
2 changed files with 47 additions and 9 deletions

View File

@@ -774,10 +774,15 @@ function parse($TEXT, options) {
function expect(punc) { return expect_token("punc", punc); }; function expect(punc) { return expect_token("punc", punc); };
function has_newline_before(token) {
return token.nlb || !all(token.comments_before, function(comment) {
return !comment.nlb;
});
}
function can_insert_semicolon() { function can_insert_semicolon() {
return !options.strict && ( return !options.strict
S.token.nlb || is("eof") || is("punc", "}") && (is("eof") || is("punc", "}") || has_newline_before(S.token));
);
}; };
function semicolon(optional) { function semicolon(optional) {
@@ -817,10 +822,10 @@ function parse($TEXT, options) {
if (S.in_directives) { if (S.in_directives) {
var token = peek(); var token = peek();
if (S.token.raw.indexOf("\\") == -1 if (S.token.raw.indexOf("\\") == -1
&& (token.nlb && (is_token(token, "punc", ";")
|| is_token(token, "eof") || is_token(token, "punc", "}")
|| is_token(token, "punc", ";") || has_newline_before(token)
|| is_token(token, "punc", "}"))) { || is_token(token, "eof"))) {
S.input.add_directive(S.token.value); S.input.add_directive(S.token.value);
} else { } else {
S.in_directives = false; S.in_directives = false;
@@ -927,7 +932,7 @@ function parse($TEXT, options) {
case "throw": case "throw":
next(); next();
if (S.token.nlb) if (has_newline_before(S.token))
croak("Illegal newline after 'throw'"); croak("Illegal newline after 'throw'");
var value = expression(true); var value = expression(true);
semicolon(); semicolon();
@@ -1503,7 +1508,7 @@ function parse($TEXT, options) {
return ex; return ex;
} }
var val = expr_atom(allow_calls); var val = expr_atom(allow_calls);
while (is("operator") && UNARY_POSTFIX(S.token.value) && !S.token.nlb) { while (is("operator") && UNARY_POSTFIX(S.token.value) && !has_newline_before(S.token)) {
val = make_unary(AST_UnaryPostfix, S.token, val); val = make_unary(AST_UnaryPostfix, S.token, val);
val.start = start; val.start = start;
val.end = S.token; val.end = S.token;

View File

@@ -0,0 +1,33 @@
insert_semicolon: {
beautify = {
beautify: true,
comments: "all",
}
input: {
var a
/* foo */ var b
}
expect_exact: [
"var a",
"/* foo */;",
"",
"var b;",
]
}
unary_postfix: {
beautify = {
beautify: true,
comments: "all",
}
input: {
a
/* foo */++b
}
expect_exact: [
"a",
"/* foo */;",
"",
"++b;",
]
}