Skip to content

Commit 7b6081b

Browse files
ljqxZhibin Liu
authored and
Zhibin Liu
committed
Support ES2021 Logical Assignment
1 parent 512cd66 commit 7b6081b

7 files changed

+338
-66
lines changed

src/parser.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,10 @@ export class Parser {
524524
op === '>>>=' ||
525525
op === '&=' ||
526526
op === '^=' ||
527-
op === '|=';
527+
op === '|=' ||
528+
op === '&&=' ||
529+
op === '||=' ||
530+
op === '??=';
528531
}
529532

530533
// Cover grammar support.

src/scanner.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,12 @@ export class Scanner {
613613
++this.index;
614614
if (this.source[this.index] === '?') {
615615
++this.index;
616-
str = '??';
616+
if (this.source[this.index] === '=') {
617+
++this.index;
618+
str = '??=';
619+
} else {
620+
str = '??';
621+
}
617622
} if (this.source[this.index] === '.' && !/^\d$/.test(this.source[this.index + 1])) {
618623
// "?." in "foo?.3:0" should not be treated as optional chaining.
619624
// See https://github.com/tc39/proposal-optional-chaining#notes
@@ -642,13 +647,14 @@ export class Scanner {
642647
// 3-character punctuators.
643648
str = str.substr(0, 3);
644649
if (str === '===' || str === '!==' || str === '>>>' ||
645-
str === '<<=' || str === '>>=' || str === '**=') {
650+
str === '<<=' || str === '>>=' || str === '**=' ||
651+
str === '&&=' || str === '||=') {
646652
this.index += 3;
647653
} else {
648654

649655
// 2-character punctuators.
650656
str = str.substr(0, 2);
651-
if (str === '&&' || str === '||' || str === '??' ||
657+
if (str === '&&' || str === '||' ||
652658
str === '==' || str === '!=' ||
653659
str === '+=' || str === '-=' || str === '*=' || str === '/=' ||
654660
str === '++' || str === '--' ||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo &&= true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
{
2+
"type": "Program",
3+
"body": [
4+
{
5+
"type": "ExpressionStatement",
6+
"expression": {
7+
"type": "AssignmentExpression",
8+
"operator": "&&=",
9+
"left": {
10+
"type": "Identifier",
11+
"name": "foo",
12+
"range": [
13+
0,
14+
3
15+
],
16+
"loc": {
17+
"start": {
18+
"line": 1,
19+
"column": 0
20+
},
21+
"end": {
22+
"line": 1,
23+
"column": 3
24+
}
25+
}
26+
},
27+
"right": {
28+
"type": "Literal",
29+
"value": true,
30+
"raw": "true",
31+
"range": [
32+
8,
33+
12
34+
],
35+
"loc": {
36+
"start": {
37+
"line": 1,
38+
"column": 8
39+
},
40+
"end": {
41+
"line": 1,
42+
"column": 12
43+
}
44+
}
45+
},
46+
"range": [
47+
0,
48+
12
49+
],
50+
"loc": {
51+
"start": {
52+
"line": 1,
53+
"column": 0
54+
},
55+
"end": {
56+
"line": 1,
57+
"column": 12
58+
}
59+
}
60+
},
61+
"range": [
62+
0,
63+
12
64+
],
65+
"loc": {
66+
"start": {
67+
"line": 1,
68+
"column": 0
69+
},
70+
"end": {
71+
"line": 1,
72+
"column": 12
73+
}
74+
}
75+
}
76+
],
77+
"sourceType": "script",
78+
"range": [
79+
0,
80+
12
81+
],
82+
"loc": {
83+
"start": {
84+
"line": 1,
85+
"column": 0
86+
},
87+
"end": {
88+
"line": 1,
89+
"column": 12
90+
}
91+
},
92+
"tokens": [
93+
{
94+
"type": "Identifier",
95+
"value": "foo",
96+
"range": [
97+
0,
98+
3
99+
],
100+
"loc": {
101+
"start": {
102+
"line": 1,
103+
"column": 0
104+
},
105+
"end": {
106+
"line": 1,
107+
"column": 3
108+
}
109+
}
110+
},
111+
{
112+
"type": "Punctuator",
113+
"value": "&&=",
114+
"range": [
115+
4,
116+
7
117+
],
118+
"loc": {
119+
"start": {
120+
"line": 1,
121+
"column": 4
122+
},
123+
"end": {
124+
"line": 1,
125+
"column": 7
126+
}
127+
}
128+
},
129+
{
130+
"type": "Boolean",
131+
"value": "true",
132+
"range": [
133+
8,
134+
12
135+
],
136+
"loc": {
137+
"start": {
138+
"line": 1,
139+
"column": 8
140+
},
141+
"end": {
142+
"line": 1,
143+
"column": 12
144+
}
145+
}
146+
}
147+
]
148+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo ??= true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
{
2+
"type": "Program",
3+
"body": [
4+
{
5+
"type": "ExpressionStatement",
6+
"expression": {
7+
"type": "AssignmentExpression",
8+
"operator": "??=",
9+
"left": {
10+
"type": "Identifier",
11+
"name": "foo",
12+
"range": [
13+
0,
14+
3
15+
],
16+
"loc": {
17+
"start": {
18+
"line": 1,
19+
"column": 0
20+
},
21+
"end": {
22+
"line": 1,
23+
"column": 3
24+
}
25+
}
26+
},
27+
"right": {
28+
"type": "Literal",
29+
"value": true,
30+
"raw": "true",
31+
"range": [
32+
8,
33+
12
34+
],
35+
"loc": {
36+
"start": {
37+
"line": 1,
38+
"column": 8
39+
},
40+
"end": {
41+
"line": 1,
42+
"column": 12
43+
}
44+
}
45+
},
46+
"range": [
47+
0,
48+
12
49+
],
50+
"loc": {
51+
"start": {
52+
"line": 1,
53+
"column": 0
54+
},
55+
"end": {
56+
"line": 1,
57+
"column": 12
58+
}
59+
}
60+
},
61+
"range": [
62+
0,
63+
12
64+
],
65+
"loc": {
66+
"start": {
67+
"line": 1,
68+
"column": 0
69+
},
70+
"end": {
71+
"line": 1,
72+
"column": 12
73+
}
74+
}
75+
}
76+
],
77+
"sourceType": "script",
78+
"range": [
79+
0,
80+
12
81+
],
82+
"loc": {
83+
"start": {
84+
"line": 1,
85+
"column": 0
86+
},
87+
"end": {
88+
"line": 1,
89+
"column": 12
90+
}
91+
},
92+
"tokens": [
93+
{
94+
"type": "Identifier",
95+
"value": "foo",
96+
"range": [
97+
0,
98+
3
99+
],
100+
"loc": {
101+
"start": {
102+
"line": 1,
103+
"column": 0
104+
},
105+
"end": {
106+
"line": 1,
107+
"column": 3
108+
}
109+
}
110+
},
111+
{
112+
"type": "Punctuator",
113+
"value": "??=",
114+
"range": [
115+
4,
116+
7
117+
],
118+
"loc": {
119+
"start": {
120+
"line": 1,
121+
"column": 4
122+
},
123+
"end": {
124+
"line": 1,
125+
"column": 7
126+
}
127+
}
128+
},
129+
{
130+
"type": "Boolean",
131+
"value": "true",
132+
"range": [
133+
8,
134+
12
135+
],
136+
"loc": {
137+
"start": {
138+
"line": 1,
139+
"column": 8
140+
},
141+
"end": {
142+
"line": 1,
143+
"column": 12
144+
}
145+
}
146+
}
147+
]
148+
}

0 commit comments

Comments
 (0)