Skip to content

Commit 87d4e13

Browse files
committed
added support for ES2021 assignment operators
1 parent d867172 commit 87d4e13

File tree

4 files changed

+659
-6
lines changed

4 files changed

+659
-6
lines changed

javascript/extractor/src/com/semmle/jcorn/Parser.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -531,9 +531,14 @@ private Token readToken_question() { // '?'
531531
int next2 = charAt(this.pos + 2);
532532
if (this.options.esnext()) {
533533
if (next == '.' && !('0' <= next2 && next2 <= '9')) // '?.', but not '?.X' where X is a digit
534-
return this.finishOp(TokenType.questiondot, 2);
535-
if (next == '?') // '??'
536-
return this.finishOp(TokenType.questionquestion, 2);
534+
return this.finishOp(TokenType.questiondot, 2);
535+
if (next == '?') { // '??'
536+
if (next2 == '=') { // ??=
537+
return this.finishOp(TokenType.assign, 3);
538+
}
539+
return this.finishOp(TokenType.questionquestion, 2);
540+
}
541+
537542
}
538543
return this.finishOp(TokenType.question, 1);
539544
}
@@ -566,8 +571,11 @@ private Token readToken_mult_modulo_exp(int code) { // '%*'
566571

567572
private Token readToken_pipe_amp(int code) { // '|&'
568573
int next = charAt(this.pos + 1);
569-
if (next == code)
574+
int next2 = charAt(this.pos + 2);
575+
if (next == code) { // && ||
576+
if (next2 == 61) return this.finishOp(TokenType.assign, 3); // &&= ||=
570577
return this.finishOp(code == 124 ? TokenType.logicalOR : TokenType.logicalAND, 2);
578+
}
571579
if (next == 61) return this.finishOp(TokenType.assign, 2);
572580
return this.finishOp(code == 124 ? TokenType.bitwiseOR : TokenType.bitwiseAND, 1);
573581
}
@@ -709,8 +717,8 @@ protected Token getTokenFromCode(int code) {
709717
case 42: // '%*'
710718
return this.readToken_mult_modulo_exp(code);
711719

712-
case 124:
713-
case 38: // '|&'
720+
case 124: // '|'
721+
case 38: // '&'
714722
return this.readToken_pipe_amp(code);
715723

716724
case 94: // '^'
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var x = 1;
2+
var y = 2;
3+
var foo = x && y;
4+
var bar = x &&= y;
5+
console.log(x); // 2
6+
7+
x &&= y;
8+
x ||= y;
9+
x ??= y;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"experimental": true
3+
}

0 commit comments

Comments
 (0)