Skip to content

Commit 5235e9e

Browse files
JohnLuck77copybara-github
authored andcommitted
Update PeepholeFoldConstants to fold unary operators with bigint literal operands
PiperOrigin-RevId: 325365271
1 parent 796c5a7 commit 5235e9e

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.javascript.rhino.Node;
2525
import com.google.javascript.rhino.Token;
2626
import com.google.javascript.rhino.jstype.TernaryValue;
27+
import java.math.BigInteger;
2728

2829
/**
2930
* Peephole optimization to fold constants (e.g. x + 1 + 7 --> x + 8).
@@ -411,6 +412,12 @@ private Node tryFoldUnaryOperator(Node n) {
411412
report(FRACTIONAL_BITWISE_OPERAND, left);
412413
return n;
413414
}
415+
} else if (left.isBigInt()) {
416+
BigInteger val = left.getBigInt();
417+
Node notValNode = IR.bigint(val.not());
418+
parent.replaceChild(n, notValNode);
419+
reportChangeToEnclosingScope(parent);
420+
return notValNode;
414421
} else {
415422
return n;
416423
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,14 @@ public void testUnaryOps() {
522522
testSame("a=~.5", PeepholeFoldConstants.FRACTIONAL_BITWISE_OPERAND);
523523
}
524524

525+
@Test
526+
public void testUnaryOpsWithBigInt() {
527+
fold("-(1n)", "-1n");
528+
fold("- -1n", "1n");
529+
fold("!1n", "false");
530+
fold("~0n", "-1n");
531+
}
532+
525533
@Test
526534
public void testUnaryOpsStringCompare() {
527535
foldSame("a = -1");

0 commit comments

Comments
 (0)