fix(es/parser): Keep comments of the RHS of a binary expression (#5812)

This commit is contained in:
RiESAEX 2022-09-11 10:58:06 +08:00 committed by GitHub
parent 7f6483a94c
commit 8adbe1675e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 103 additions and 8 deletions

View File

@ -0,0 +1,44 @@
function x1(y) {
return a
// leading of &&
&& c;
};
function x2(y) {
return a
&&
// leading of c
c;
};
function x3(y)
{
return a // trailing of a
&&
c;
};
function x4(y)
{
return a && // trailing of &&
c;
};
function x5(y) {
return a
/* leading of && */
&& c;
};
function x2(y) {
return a
&&
/* leading of c */
c;
};
function x3(y)
{
return a /* trailing of a */
&&
c;
};
function x4(y)
{
return a && /* trailing of && */
c;
};

View File

@ -0,0 +1,35 @@
function x1(y) {
return a && c;
}
;
function x2(y) {
return a && // leading of c
c;
}
;
function x3(y) {
return a // trailing of a
&& c;
}
;
function x4(y) {
return a && // trailing of &&
c;
}
;
function x5(y) {
return a && c;
}
;
function x2(y) {
return a && /* leading of c */ c;
}
;
function x3(y) {
return a /* trailing of a */ && c;
}
;
function x4(y) {
return a && /* trailing of && */ c;
}
;

View File

@ -478,7 +478,8 @@ export async function renderToHTML(req, res, pathname, query, renderOpts) {
props.pageProps = Object.assign({}, props.pageProps, data1.props);
renderOpts.pageData = props;
}
if (!isSSG && !getServerSideProps && process.env.NODE_ENV !== "production" && Object.keys(props?.pageProps || {}).includes("url")) {
if (!isSSG && // we only show this warning for legacy pages
!getServerSideProps && process.env.NODE_ENV !== "production" && Object.keys(props?.pageProps || {}).includes("url")) {
console.warn(`The prop \`url\` is a reserved prop in Next.js for legacy reasons and will be overridden on page ${pathname}\n` + `See more info here: https://nextjs.org/docs/messages/reserved-page-prop`);
}
// Avoid rendering page un-necessarily for getServerSideProps data request

View File

@ -16,7 +16,10 @@
var one = 1;
var _float = -4 / 3;
var a = new Array(false, undefined, null, "0", obj, -1.3333333333333, "str", -0, true, +0, one, 1, 0, false, _float, -4 / 3);
if (a.indexOf(-4 / 3) === 14 && a.indexOf(0) === 7 && a.indexOf(-0) === 7 && a.indexOf(1) === 10) {
if (a.indexOf(-4 / 3) === 14 && // a[14]=_float===-(4/3)
a.indexOf(0) === 7 && // a[7] = +0, 0===+0
a.indexOf(-0) === 7 && // a[7] = +0, -0===+0
a.indexOf(1) === 10) {
return true;
}
}

View File

@ -1,2 +1,3 @@
//// [parserGreaterThanTokenAmbiguity10.ts]
1 >>> 2;
1 >>> // after
2;

View File

@ -1,2 +1,3 @@
//// [parserGreaterThanTokenAmbiguity5.ts]
1 >> 2;
1 >> // after
2;

View File

@ -37,7 +37,8 @@ function foo7(x) {
}
function foo8(x) {
var b;
return typeof x === "string" ? x === "hello" : (b = x) && (typeof x === "boolean" ? x // boolean
return typeof x === "string" ? x === "hello" : (b = x) && // number | boolean
(typeof x === "boolean" ? x // boolean
: x == 10); // boolean
}
function foo9(x) {

View File

@ -1 +1,2 @@
var a = !b && (!c || d) && (!e || f) && g();
var a = !b && // should not touch this one
(!c || d) && (!e || f) && g();

View File

@ -371,6 +371,13 @@ impl State {
.unwrap_or(false)
}
pub fn can_have_trailing_line_comment(&self) -> bool {
match self.token_type {
Some(TokenType::BinOp(..)) => false,
_ => true,
}
}
pub fn can_have_trailing_comment(&self) -> bool {
match self.token_type {
Some(TokenType::Keyword(..)) => false,

View File

@ -237,7 +237,7 @@ impl<'a, I: Input> Lexer<'a, I> {
// // comment for bar
// bar
//
let is_for_next = self.state.had_line_break;
let is_for_next = self.state.had_line_break || !self.state.can_have_trailing_line_comment();
let mut end = self.cur_pos();
while let Some(c) = self.cur() {

View File

@ -487,7 +487,8 @@ export async function renderToHTML(req, res, pathname, query, renderOpts) {
props.pageProps = Object.assign({}, props.pageProps, data1.props);
renderOpts.pageData = props;
}
if (!isSSG && !getServerSideProps && process.env.NODE_ENV !== "production" && Object.keys(props?.pageProps || {}).includes("url")) {
if (!isSSG && // we only show this warning for legacy pages
!getServerSideProps && process.env.NODE_ENV !== "production" && Object.keys(props?.pageProps || {}).includes("url")) {
console.warn(`The prop \`url\` is a reserved prop in Next.js for legacy reasons and will be overridden on page ${pathname}\n` + `See more info here: https://nextjs.org/docs/messages/reserved-page-prop`);
}
// Avoid rendering page un-necessarily for getServerSideProps data request