Implement nullish coalescing operator in parser (#761)
* Implement nullish coalescing operator in parser * Add ?? to AST
This commit is contained in:
@@ -150,6 +150,7 @@ require("babylon").parse("code", {
|
||||
| `optionalCatchBinding` ([proposal](https://github.com/babel/proposals/issues/7)) | `try {throw 0;} catch{do();}` |
|
||||
| `throwExpressions` ([proposal](https://github.com/babel/proposals/issues/23)) | `() => throw new Error("")` |
|
||||
| `pipelineOperator` ([proposal](https://github.com/babel/proposals/issues/29)) | `a \|> b` |
|
||||
| `nullishCoalescingOperator` ([proposal](https://github.com/babel/proposals/issues/14)) | `a ?? b` |
|
||||
|
||||
### FAQ
|
||||
|
||||
|
||||
@@ -791,6 +791,7 @@ enum BinaryOperator {
|
||||
| "|" | "^" | "&" | "in"
|
||||
| "instanceof"
|
||||
| "|>"
|
||||
| "??"
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -297,6 +297,10 @@ export default class ExpressionParser extends LValParser {
|
||||
this.state.potentialArrowAt = startPos;
|
||||
}
|
||||
|
||||
if (node.operator === "??") {
|
||||
this.expectPlugin("nullishCoalescingOperator");
|
||||
}
|
||||
|
||||
node.right = this.parseExprOp(
|
||||
this.parseMaybeUnary(),
|
||||
startPos,
|
||||
|
||||
@@ -581,7 +581,10 @@ 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 === 46 && !(next2 >= 48 && next2 <= 57)) {
|
||||
if (next === 63) {
|
||||
// '??'
|
||||
this.finishOp(tt.nullishCoalescing, 2);
|
||||
} else if (next === 46 && !(next2 >= 48 && next2 <= 57)) {
|
||||
// '.' not followed by a number
|
||||
this.state.pos += 2;
|
||||
this.finishToken(tt.questionDot);
|
||||
|
||||
@@ -132,6 +132,7 @@ export const types: { [name: string]: TokenType } = {
|
||||
bang: new TokenType("!", { beforeExpr, prefix, startsExpr }),
|
||||
tilde: new TokenType("~", { beforeExpr, prefix, startsExpr }),
|
||||
pipeline: new BinopTokenType("|>", 0),
|
||||
nullishCoalescing: new BinopTokenType("??", 1),
|
||||
logicalOR: new BinopTokenType("||", 1),
|
||||
logicalAND: new BinopTokenType("&&", 2),
|
||||
bitwiseOR: new BinopTokenType("|", 3),
|
||||
|
||||
1
test/fixtures/experimental/nullish-coalescing-operator/expression/actual.js
vendored
Normal file
1
test/fixtures/experimental/nullish-coalescing-operator/expression/actual.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
foo ?? 1;
|
||||
102
test/fixtures/experimental/nullish-coalescing-operator/expression/expected.json
vendored
Normal file
102
test/fixtures/experimental/nullish-coalescing-operator/expression/expected.json
vendored
Normal file
@@ -0,0 +1,102 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start": 0,
|
||||
"end": 9,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 9
|
||||
}
|
||||
},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start": 0,
|
||||
"end": 9,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 9
|
||||
}
|
||||
},
|
||||
"sourceType": "script",
|
||||
"body": [
|
||||
{
|
||||
"type": "ExpressionStatement",
|
||||
"start": 0,
|
||||
"end": 9,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 9
|
||||
}
|
||||
},
|
||||
"expression": {
|
||||
"type": "BinaryExpression",
|
||||
"start": 0,
|
||||
"end": 8,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 8
|
||||
}
|
||||
},
|
||||
"left": {
|
||||
"type": "Identifier",
|
||||
"start": 0,
|
||||
"end": 3,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 3
|
||||
},
|
||||
"identifierName": "foo"
|
||||
},
|
||||
"name": "foo"
|
||||
},
|
||||
"operator": "??",
|
||||
"right": {
|
||||
"type": "NumericLiteral",
|
||||
"start": 7,
|
||||
"end": 8,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 7
|
||||
},
|
||||
"end": {
|
||||
"line": 1,
|
||||
"column": 8
|
||||
}
|
||||
},
|
||||
"extra": {
|
||||
"rawValue": 1,
|
||||
"raw": "1"
|
||||
},
|
||||
"value": 1
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
}
|
||||
}
|
||||
3
test/fixtures/experimental/nullish-coalescing-operator/expression/options.json
vendored
Normal file
3
test/fixtures/experimental/nullish-coalescing-operator/expression/options.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["nullishCoalescingOperator"]
|
||||
}
|
||||
1
test/fixtures/experimental/nullish-coalescing-operator/no-plugin-error/actual.js
vendored
Normal file
1
test/fixtures/experimental/nullish-coalescing-operator/no-plugin-error/actual.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
foo ?? 1;
|
||||
3
test/fixtures/experimental/nullish-coalescing-operator/no-plugin-error/options.json
vendored
Normal file
3
test/fixtures/experimental/nullish-coalescing-operator/no-plugin-error/options.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "This experimental syntax requires enabling the parser plugin: 'nullishCoalescingOperator' (1:7)"
|
||||
}
|
||||
Reference in New Issue
Block a user