Skip to content

Commit

Permalink
fix(un-optional-chaining): handle edge case when optional chaining ar…
Browse files Browse the repository at this point in the history
…e concated
  • Loading branch information
pionxzh committed Mar 30, 2024
1 parent 976d80f commit 3c8190f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ class C {
var _o$obj6;
return !!((_o$obj6 = o.obj) !== null && _o$obj6 !== void 0 && (_o$obj6 = _o$obj6.a.b) !== null && _o$obj6 !== void 0 && _o$obj6.c.d);
}
static testLogicalInIf(o) {
var _o$a$b4, _o$a;
if (o !== null && o !== void 0 && (_o$a$b4 = o.a.b) !== null && _o$a$b4 !== void 0 && _o$a$b4.c.d
&& o !== null && o !== void 0 && (_o$a = o.a) !== null && _o$a !== void 0 && _o$a.b.c.d) {
return true;
}
return false;
}
static testLogicalInReturn(o) {
var _o$a$b5, _o$a2;
return (o === null || o === void 0 || (_o$a$b5 = o.a.b) === null || _o$a$b5 === void 0 ? void 0 : _o$a$b5.c.d) && (o === null || o === void 0 || (_o$a2 = o.a) === null || _o$a2 === void 0 ? void 0 : _o$a2.b.c.d);
Expand Down Expand Up @@ -232,6 +240,13 @@ class C {
static testNegateDeep(o) {
return !!(o.obj?.a.b?.c.d);
}
static testLogicalInIf(o) {
var _o$a$b4, _o$a;
if (o?.a.b?.c.d && o?.a?.b.c.d) {
return true;
}
return false;
}
static testLogicalInReturn(o) {
return (o?.a.b?.c.d) && (o?.a?.b.c.d);
}
Expand All @@ -246,27 +261,6 @@ class C {
`,
)

inlineTest.fixme('Babel - Cast to boolean - Failed cases',
`
function testLogicalInIf(o) {
var _o$a$b4, _o$a;
if (o !== null && o !== void 0 && (_o$a$b4 = o.a.b) !== null && _o$a$b4 !== void 0 && _o$a$b4.c.d && o !== null && o !== void 0 && (_o$a = o.a) !== null && _o$a !== void 0 && _o$a.b.c.d) {
return true;
}
return false;
}
`,
`
function testLogicalInIf(o) {
var _o$a$b4, _o$a;
if (o?.a.b?.c.d && o?.a?.b.c.d) {
return true;
}
return false;
}
`,
)

inlineTest('Babel / SWC - Container',
`
var _user$address, _user$address2, _a, _a2, _a3;
Expand Down
20 changes: 15 additions & 5 deletions packages/unminify/src/transformations/un-optional-chaining.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ function convertOptionalChaining(j: JSCodeshift, path: ASTPath<ConditionalExpres
// renderDebugDecisionTree(j, decisionTree)

const _result = constructOptionalChaining(j, path, decisionTree, 0)
const result = _result && isNotNull ? negateCondition(j, _result) : _result
if (result) {
// console.log('<<<', `${picocolors.cyan(j(result).toSource())}`)
mergeComments(result, expression.comments)
}
if (!_result) return null

const result = isNotNull ? negateCondition(j, _result) : _result
// console.log('<<<', `${picocolors.cyan(j(result).toSource())}`)
mergeComments(result, expression.comments)

return result
}

Expand Down Expand Up @@ -142,6 +143,15 @@ function constructOptionalChaining(
return applyOptionalChaining(j, cond, left, undefined)
}
}

if (falseBranch) {
const cond = constructOptionalChaining(j, path, falseBranch, 0)
if (!cond) return null

const result = applyOptionalChaining(j, cond, condition as Identifier, undefined)
// combine the condition with the original condition
return j.logicalExpression('||', condition, result)
}
}
else if (flag === 1) {
if (!falseBranch) return null
Expand Down

0 comments on commit 3c8190f

Please sign in to comment.