Finish optionalChaining plugin

This commit is contained in:
Justin Ridgewell
2017-06-03 01:16:59 -04:00
parent 2dd624b44e
commit e1ec23cd3e
13 changed files with 1196 additions and 40 deletions

View File

@@ -302,18 +302,23 @@ export default class ExpressionParser extends LValParser {
node.callee = this.parseNoCallExpr();
return this.parseSubscripts(this.finishNode(node, "BindExpression"), startPos, startLoc, noCalls);
} else if (this.eat(tt.questionDot)) {
const node = this.startNodeAt(startPos, startLoc);
} else if (this.match(tt.questionDot)) {
if (!this.hasPlugin("optionalChaining")) {
this.raise(node.start, "You can only use optional-chaining when the 'optionalChaining' plugin is enabled.");
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.optional = true;
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)) {
@@ -322,6 +327,7 @@ export default class ExpressionParser extends LValParser {
base.name === "async" &&
!this.canInsertSemicolon();
node.callee = base;
node.arguments = this.parseCallExpressionArguments(tt.parenR, possibleAsync);
node.optional = true;
@@ -329,8 +335,8 @@ export default class ExpressionParser extends LValParser {
} else {
node.object = base;
node.property = this.parseIdentifier(true);
node.optional = true;
node.computed = false;
node.optional = true;
base = this.finishNode(node, "MemberExpression");
}
} else if (this.eat(tt.dot)) {
@@ -768,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);
@@ -775,6 +782,9 @@ export default class ExpressionParser extends LValParser {
} else {
node.arguments = [];
}
if (optional) {
node.optional = true;
}
return this.finishNode(node, "NewExpression");
}

View File

@@ -428,11 +428,11 @@ export default class Tokenizer extends LocationParser {
readToken_question() { // '?'
const next = this.input.charCodeAt(this.state.pos + 1);
if (next === 46) { // '.'
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 {
} else {
++this.state.pos;
return this.finishToken(tt.question);
}