fix(es/codegen): Fix codegen of optional chaining expr with a comment (#8005)

**Related issue:**

 - Closes #8004
This commit is contained in:
Donny/강동윤 2023-09-26 12:58:02 +09:00 committed by GitHub
parent b74a9f4357
commit f07bb482e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 93 additions and 0 deletions

View File

@ -0,0 +1,18 @@
{
"jsc": {
"transform": {
"decoratorVersion": "2022-03"
},
"parser": {
"syntax": "typescript",
"tsx": false
},
"target": "es2021",
"loose": false,
},
"module": {
"type": "commonjs"
},
"isModule": true,
"minify": false
}

View File

@ -0,0 +1,35 @@
const string1 = { errorCode: 'test' };
const string2 = 'test';
function evaluateWithoutComment() {
return string1.errorCode === string2;
}
function evaluateWithComment() {
return (
// This comment doesn't break return evaluation
string1?.errorCode === string2
);
}
function evaluateWithCast() {
return (
(string1 as { errorCode: string })?.errorCode === string2
);
}
function evaluateWithCastAndComment() {
return (
// This comment seems to cause wrapping parantheses to get stripped, breaking the return evaluation
(string1 as { errorCode: string })?.errorCode === string2
);
}
// Works, returns true
console.log(`evaluateWithoutComment: ${evaluateWithoutComment()}`);
// Works, returns true
console.log(`evaluateWithComment: ${evaluateWithComment()}`);
// Works, returns true
console.log(`evaluateWithCast: ${evaluateWithCast()}`);
// Breaks, returns undefined due to stripped parantheses
console.log(`evaluateWithCastAndComment: ${evaluateWithCastAndComment()}`);

View File

@ -0,0 +1,27 @@
"use strict";
const string1 = {
errorCode: 'test'
};
const string2 = 'test';
function evaluateWithoutComment() {
return string1.errorCode === string2;
}
function evaluateWithComment() {
return(// This comment doesn't break return evaluation
string1?.errorCode === string2);
}
function evaluateWithCast() {
return string1?.errorCode === string2;
}
function evaluateWithCastAndComment() {
return(// This comment seems to cause wrapping parantheses to get stripped, breaking the return evaluation
string1?.errorCode === string2);
}
// Works, returns true
console.log(`evaluateWithoutComment: ${evaluateWithoutComment()}`);
// Works, returns true
console.log(`evaluateWithComment: ${evaluateWithComment()}`);
// Works, returns true
console.log(`evaluateWithCast: ${evaluateWithCast()}`);
// Breaks, returns undefined due to stripped parantheses
console.log(`evaluateWithCastAndComment: ${evaluateWithCastAndComment()}`);

View File

@ -2941,6 +2941,19 @@ where
}
}
Expr::OptChain(e) => match &*e.base {
OptChainBase::Member(m) => {
if self.has_leading_comment(&m.obj) {
return true;
}
}
OptChainBase::Call(c) => {
if self.has_leading_comment(&c.callee) {
return true;
}
}
},
_ => {}
}