Skip to content
This repository has been archived by the owner on May 19, 2018. It is now read-only.

Commit

Permalink
Merge pull request #1 from jridgewell/pr/545
Browse files Browse the repository at this point in the history
Finish optionalChaining plugin
  • Loading branch information
xtuc authored Jun 3, 2017
2 parents 2dd624b + e1ec23c commit 4628bb9
Show file tree
Hide file tree
Showing 13 changed files with 1,196 additions and 40 deletions.
22 changes: 16 additions & 6 deletions src/parser/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -322,15 +327,16 @@ export default class ExpressionParser extends LValParser {
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.optional = true;
node.computed = false;
node.optional = true;
base = this.finishNode(node, "MemberExpression");
}
} else if (this.eat(tt.dot)) {
Expand Down Expand Up @@ -768,13 +774,17 @@ 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);
this.toReferencedList(node.arguments);
} else {
node.arguments = [];
}
if (optional) {
node.optional = true;
}

return this.finishNode(node, "NewExpression");
}
Expand Down
6 changes: 3 additions & 3 deletions src/tokenizer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
new C?.()

new C?.(a, b)

new B?.C?.()

new B?.C?.(a, b)

new B?.C
Loading

0 comments on commit 4628bb9

Please sign in to comment.