mirror of
https://github.com/swc-project/swc.git
synced 2024-12-24 22:22:34 +03:00
fix(es/parser): Keep comments of the RHS of a binary expression (#5812)
This commit is contained in:
parent
7f6483a94c
commit
8adbe1675e
44
crates/swc/tests/fixture/issues-5xxx/5783/input/input.js
Normal file
44
crates/swc/tests/fixture/issues-5xxx/5783/input/input.js
Normal 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;
|
||||
};
|
35
crates/swc/tests/fixture/issues-5xxx/5783/output/input.js
Normal file
35
crates/swc/tests/fixture/issues-5xxx/5783/output/input.js
Normal 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;
|
||||
}
|
||||
;
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -1,2 +1,3 @@
|
||||
//// [parserGreaterThanTokenAmbiguity10.ts]
|
||||
1 >>> 2;
|
||||
1 >>> // after
|
||||
2;
|
||||
|
@ -1,2 +1,3 @@
|
||||
//// [parserGreaterThanTokenAmbiguity5.ts]
|
||||
1 >> 2;
|
||||
1 >> // after
|
||||
2;
|
||||
|
@ -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) {
|
||||
|
@ -1 +1,2 @@
|
||||
var a = !b && (!c || d) && (!e || f) && g();
|
||||
var a = !b && // should not touch this one
|
||||
(!c || d) && (!e || f) && g();
|
||||
|
@ -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,
|
||||
|
@ -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() {
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user