Add ??= to Logical Assignment Operators (#7623)

`??=` is being merged into the Logical Assignment Operator proposal, and the overall proposal will wait until nullish coalescing is finalized.
This commit is contained in:
Justin Ridgewell
2018-03-25 18:58:51 +01:00
committed by GitHub
parent 023f8bd1cb
commit a7bddc02ba
19 changed files with 391 additions and 9 deletions

View File

@@ -299,10 +299,6 @@ export default class ExpressionParser extends LValParser {
this.state.potentialArrowAt = startPos;
}
if (node.operator === "??") {
this.expectPlugin("nullishCoalescingOperator");
}
node.right = this.parseExprOp(
this.parseMaybeUnary(),
startPos,

View File

@@ -607,8 +607,16 @@ export default class Tokenizer extends LocationParser {
const next = this.input.charCodeAt(this.state.pos + 1);
const next2 = this.input.charCodeAt(this.state.pos + 2);
if (next === charCodes.questionMark) {
// '??'
this.finishOp(tt.nullishCoalescing, 2);
this.expectPlugin("nullishCoalescingOperator");
if (next2 === charCodes.equalsTo) {
// '??='
this.expectPlugin("logicalAssignment");
this.finishOp(tt.assign, 3);
} else {
// '??'
this.finishOp(tt.nullishCoalescing, 2);
}
} else if (
next === charCodes.dot &&
!(next2 >= charCodes.digit0 && next2 <= charCodes.digit9)

View File

@@ -0,0 +1,4 @@
{
"plugins": ["logicalAssignment"],
"throws": "This experimental syntax requires enabling the parser plugin: 'nullishCoalescingOperator' (1:2)"
}

View File

@@ -0,0 +1,2 @@
a ??= b;
obj.a ??= b;

View File

@@ -0,0 +1,4 @@
{
"plugins": ["nullishCoalescingOperator"],
"throws": "This experimental syntax requires enabling the parser plugin: 'logicalAssignment' (1:2)"
}

View File

@@ -0,0 +1,2 @@
a ??= b;
obj.a ??= b;

View File

@@ -0,0 +1,3 @@
{
"plugins": ["logicalAssignment", "nullishCoalescingOperator"]
}

View File

@@ -0,0 +1,197 @@
{
"type": "File",
"start": 0,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 12
}
},
"program": {
"type": "Program",
"start": 0,
"end": 21,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 12
}
},
"sourceType": "script",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 8,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 8
}
},
"expression": {
"type": "AssignmentExpression",
"start": 0,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 7
}
},
"operator": "??=",
"left": {
"type": "Identifier",
"start": 0,
"end": 1,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 1
},
"identifierName": "a"
},
"name": "a"
},
"right": {
"type": "Identifier",
"start": 6,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
},
"identifierName": "b"
},
"name": "b"
}
}
},
{
"type": "ExpressionStatement",
"start": 9,
"end": 21,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 12
}
},
"expression": {
"type": "AssignmentExpression",
"start": 9,
"end": 20,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 11
}
},
"operator": "??=",
"left": {
"type": "MemberExpression",
"start": 9,
"end": 14,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 5
}
},
"object": {
"type": "Identifier",
"start": 9,
"end": 12,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 3
},
"identifierName": "obj"
},
"name": "obj"
},
"property": {
"type": "Identifier",
"start": 13,
"end": 14,
"loc": {
"start": {
"line": 2,
"column": 4
},
"end": {
"line": 2,
"column": 5
},
"identifierName": "a"
},
"name": "a"
},
"computed": false
},
"right": {
"type": "Identifier",
"start": 19,
"end": 20,
"loc": {
"start": {
"line": 2,
"column": 10
},
"end": {
"line": 2,
"column": 11
},
"identifierName": "b"
},
"name": "b"
}
}
}
],
"directives": []
}
}

View File

@@ -1,3 +1,3 @@
{
"throws": "This experimental syntax requires enabling the parser plugin: 'nullishCoalescingOperator' (1:7)"
"throws": "This experimental syntax requires enabling the parser plugin: 'nullishCoalescingOperator' (1:4)"
}