Merge pull request #545 from xtuc/feat-optional-chaining
Optional Chaining: Stage 1 plugin
This commit is contained in:
@@ -301,6 +301,44 @@ export default class ExpressionParser extends LValParser {
|
||||
node.object = base;
|
||||
node.callee = this.parseNoCallExpr();
|
||||
return this.parseSubscripts(this.finishNode(node, "BindExpression"), startPos, startLoc, noCalls);
|
||||
|
||||
} else if (this.match(tt.questionDot)) {
|
||||
if (!this.hasPlugin("optionalChaining")) {
|
||||
this.raise(startPos, "You can only use optional-chaining when the 'optionalChaining' plugin is enabled.");
|
||||
}
|
||||
|
||||
if (noCalls && this.lookahead().type == tt.parenL) {
|
||||
return base;
|
||||
}
|
||||
this.next();
|
||||
|
||||
const node = this.startNodeAt(startPos, startLoc);
|
||||
|
||||
if (this.eat(tt.bracketL)) {
|
||||
node.object = base;
|
||||
node.property = this.parseExpression();
|
||||
node.computed = true;
|
||||
node.optional = true;
|
||||
this.expect(tt.bracketR);
|
||||
base = this.finishNode(node, "MemberExpression");
|
||||
} else if (this.eat(tt.parenL)) {
|
||||
const possibleAsync = this.state.potentialArrowAt === base.start &&
|
||||
base.type === "Identifier" &&
|
||||
base.name === "async" &&
|
||||
!this.canInsertSemicolon();
|
||||
|
||||
node.callee = base;
|
||||
node.arguments = this.parseCallExpressionArguments(tt.parenR, possibleAsync);
|
||||
node.optional = true;
|
||||
|
||||
base = this.finishNode(node, "CallExpression");
|
||||
} else {
|
||||
node.object = base;
|
||||
node.property = this.parseIdentifier(true);
|
||||
node.computed = false;
|
||||
node.optional = true;
|
||||
base = this.finishNode(node, "MemberExpression");
|
||||
}
|
||||
} else if (this.eat(tt.dot)) {
|
||||
const node = this.startNodeAt(startPos, startLoc);
|
||||
node.object = base;
|
||||
@@ -736,6 +774,7 @@ export default class ExpressionParser extends LValParser {
|
||||
}
|
||||
|
||||
node.callee = this.parseNoCallExpr();
|
||||
const optional = this.eat(tt.questionDot);
|
||||
|
||||
if (this.eat(tt.parenL)) {
|
||||
node.arguments = this.parseExprList(tt.parenR);
|
||||
@@ -743,6 +782,9 @@ export default class ExpressionParser extends LValParser {
|
||||
} else {
|
||||
node.arguments = [];
|
||||
}
|
||||
if (optional) {
|
||||
node.optional = true;
|
||||
}
|
||||
|
||||
return this.finishNode(node, "NewExpression");
|
||||
}
|
||||
|
||||
@@ -426,6 +426,18 @@ export default class Tokenizer extends LocationParser {
|
||||
return this.finishOp(code === 61 ? tt.eq : tt.prefix, 1);
|
||||
}
|
||||
|
||||
readToken_question() { // '?'
|
||||
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)) { // '.' not followed by a number
|
||||
this.state.pos += 2;
|
||||
return this.finishToken(tt.questionDot);
|
||||
} else {
|
||||
++this.state.pos;
|
||||
return this.finishToken(tt.question);
|
||||
}
|
||||
}
|
||||
|
||||
getTokenFromCode(code: number): void {
|
||||
switch (code) {
|
||||
|
||||
@@ -469,7 +481,7 @@ export default class Tokenizer extends LocationParser {
|
||||
return this.finishToken(tt.colon);
|
||||
}
|
||||
|
||||
case 63: ++this.state.pos; return this.finishToken(tt.question);
|
||||
case 63: return this.readToken_question();
|
||||
case 64: ++this.state.pos; return this.finishToken(tt.at);
|
||||
|
||||
case 96: // '`'
|
||||
@@ -917,7 +929,7 @@ export default class Tokenizer extends LocationParser {
|
||||
const type = this.state.type;
|
||||
let update;
|
||||
|
||||
if (type.keyword && prevType === tt.dot) {
|
||||
if (type.keyword && (prevType === tt.dot || prevType === tt.questionDot)) {
|
||||
this.state.exprAllowed = false;
|
||||
} else if (update = type.updateContext) {
|
||||
update.call(this, prevType);
|
||||
|
||||
@@ -102,6 +102,7 @@ export const types: { [name: string]: TokenType } = {
|
||||
doubleColon: new TokenType("::", { beforeExpr }),
|
||||
dot: new TokenType("."),
|
||||
question: new TokenType("?", { beforeExpr }),
|
||||
questionDot: new TokenType("?."),
|
||||
arrow: new TokenType("=>", { beforeExpr }),
|
||||
template: new TokenType("template"),
|
||||
ellipsis: new TokenType("...", { beforeExpr }),
|
||||
|
||||
Reference in New Issue
Block a user