Skip to content

Commit

Permalink
Support ES2021 Logical Assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
ljqx committed Sep 9, 2021
1 parent 512cd66 commit ef2e509
Show file tree
Hide file tree
Showing 7 changed files with 311 additions and 106 deletions.
5 changes: 4 additions & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,10 @@ export class Parser {
op === '>>>=' ||
op === '&=' ||
op === '^=' ||
op === '|=';
op === '|=' ||
op === '&&=' ||
op === '||=' ||
op === '??=';
}

// Cover grammar support.
Expand Down
12 changes: 9 additions & 3 deletions src/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,12 @@ export class Scanner {
++this.index;
if (this.source[this.index] === '?') {
++this.index;
str = '??';
if (this.source[this.index] === '=') {
++this.index;
str = '??=';
} else {
str = '??';
}
} if (this.source[this.index] === '.' && !/^\d$/.test(this.source[this.index + 1])) {
// "?." in "foo?.3:0" should not be treated as optional chaining.
// See https://github.com/tc39/proposal-optional-chaining#notes
Expand Down Expand Up @@ -642,13 +647,14 @@ export class Scanner {
// 3-character punctuators.
str = str.substr(0, 3);
if (str === '===' || str === '!==' || str === '>>>' ||
str === '<<=' || str === '>>=' || str === '**=') {
str === '<<=' || str === '>>=' || str === '**=' ||
str === '&&=' || str === '||=') {
this.index += 3;
} else {

// 2-character punctuators.
str = str.substr(0, 2);
if (str === '&&' || str === '||' || str === '??' ||
if (str === '&&' || str === '||' ||
str === '==' || str === '!=' ||
str === '+=' || str === '-=' || str === '*=' || str === '/=' ||
str === '++' || str === '--' ||
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo &&= true
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
{
"type": "Program",
"body": [
{
"type": "ExpressionStatement",
"expression": {
"type": "AssignmentExpression",
"operator": "&&=",
"left": {
"type": "Identifier",
"name": "foo",
"range": [
0,
3
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 3
}
}
},
"right": {
"type": "Literal",
"value": true,
"raw": "true",
"range": [
8,
12
],
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 1,
"column": 12
}
}
},
"range": [
0,
12
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 12
}
}
},
"range": [
0,
12
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 12
}
}
}
],
"sourceType": "script",
"range": [
0,
12
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 12
}
},
"tokens": [
{
"type": "Identifier",
"value": "foo",
"range": [
0,
3
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 3
}
}
},
{
"type": "Punctuator",
"value": "&&=",
"range": [
4,
7
],
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 7
}
}
},
{
"type": "Boolean",
"value": "true",
"range": [
8,
12
],
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 1,
"column": 12
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo ??= true
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
{
"type": "Program",
"body": [
{
"type": "ExpressionStatement",
"expression": {
"type": "AssignmentExpression",
"operator": "??=",
"left": {
"type": "Identifier",
"name": "foo",
"range": [
0,
3
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 3
}
}
},
"right": {
"type": "Literal",
"value": true,
"raw": "true",
"range": [
8,
12
],
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 1,
"column": 12
}
}
},
"range": [
0,
12
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 12
}
}
},
"range": [
0,
12
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 12
}
}
}
],
"sourceType": "script",
"range": [
0,
12
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 12
}
},
"tokens": [
{
"type": "Identifier",
"value": "foo",
"range": [
0,
3
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 3
}
}
},
{
"type": "Punctuator",
"value": "??=",
"range": [
4,
7
],
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 7
}
}
},
{
"type": "Boolean",
"value": "true",
"range": [
8,
12
],
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 1,
"column": 12
}
}
}
]
}

0 comments on commit ef2e509

Please sign in to comment.