Skip to content

Commit f888b14

Browse files
Dustyn Loydacopybara-github
authored andcommitted
Simplify (void 0)?.x and null?.x to void 0 in PeepholeRemoveDeadCode.
PiperOrigin-RevId: 668106308
1 parent 9beeb52 commit f888b14

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

src/com/google/javascript/jscomp/PeepholeRemoveDeadCode.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,10 @@ Node optimizeSubtree(Node subtree) {
8686
return tryOptimizeNameDeclaration(subtree);
8787
case DEFAULT_VALUE:
8888
return tryRemoveDefaultValue(subtree);
89+
case OPTCHAIN_GETPROP:
8990
case OPTCHAIN_CALL:
90-
return tryRemoveOptionalCall(subtree);
91+
case OPTCHAIN_GETELEM:
92+
return tryRemoveOptionalChaining(subtree);
9193
default:
9294
return subtree;
9395
}
@@ -1460,23 +1462,23 @@ private void tryFoldForCondition(Node forCondition) {
14601462
}
14611463
}
14621464

1463-
private Node tryRemoveOptionalCall(Node optionalCall) {
1464-
Node callee = optionalCall.getFirstChild();
1465+
private Node tryRemoveOptionalChaining(Node optionalChain) {
1466+
Node callee = optionalChain.getFirstChild();
14651467
if (!NodeUtil.isNullOrUndefined(callee)) {
1466-
return optionalCall;
1468+
return optionalChain;
14671469
}
14681470
final Node result;
14691471
if (this.mayHaveSideEffects(callee)) {
14701472
// Simplify `(void sideEffectFunction())?.()` to `(void sideEffectFunction())`
14711473
// The optional chain call won't execute but sideEffectFunction() is still evaluated.
1472-
optionalCall.replaceWith(callee.detach());
1474+
optionalChain.replaceWith(callee.detach());
14731475
result = callee;
14741476
} else {
1475-
// Remove `(void 0)?.()` and (null)?.()
1477+
// Remove `(void 0)?.()` and (null)?.() and simplify `(void 0)?.x and null?.x` to void 0
14761478
result = NodeUtil.newUndefinedNode(callee);
1477-
optionalCall.replaceWith(result);
1479+
optionalChain.replaceWith(result);
14781480
}
1479-
this.markFunctionsDeleted(optionalCall);
1481+
this.markFunctionsDeleted(optionalChain);
14801482
this.reportChangeToEnclosingScope(result);
14811483
return result;
14821484
}

test/com/google/javascript/jscomp/PeepholeRemoveDeadCodeTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,6 +1838,12 @@ public void testRemoveUnreachableOptionalChainingCall() {
18381838
fold("(undefined)?.();", "");
18391839
fold("(void 0)?.(0)", "");
18401840
fold("(void 0)?.(function f() {})", "");
1841+
fold("(null)?.x;", "");
1842+
fold("(void 0)?.x;", "");
1843+
fold("(null)?.['x'];", "");
1844+
fold("(void 0)?.['x'];", "");
1845+
fold("(null)?.[x];", "");
1846+
fold("(void 0)?.[x];", "");
18411847
// arguments with unknown side effects are also removed
18421848
fold("(void 0)?.(f(), g())", "");
18431849

@@ -1847,6 +1853,8 @@ public void testRemoveUnreachableOptionalChainingCall() {
18471853

18481854
foldSame("(f(), null)?.()");
18491855
foldSame("f?.()");
1856+
fold("a?.x;", "");
1857+
fold("a?.['x'];", "");
18501858
}
18511859

18521860
@Test

0 commit comments

Comments
 (0)