Skip to content

Commit

Permalink
fix: assure left bracket is not consumed after dot (#13695)
Browse files Browse the repository at this point in the history
* fix: assure left bracket is not consumed after dot

* address review comments
  • Loading branch information
JLHwung committed Aug 20, 2021
1 parent e0c3969 commit 976bfbb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
21 changes: 16 additions & 5 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -701,6 +701,7 @@ export default class ExpressionParser extends LValParser {
}

let optional = false;

if (this.match(tt.questionDot)) {
if (noCalls && this.lookaheadCharCode() === charCodes.leftParenthesis) {
// stop at `?.` when parsing `new a?.()`
Expand All @@ -719,11 +720,21 @@ export default class ExpressionParser extends LValParser {
state,
optional,
);
} else if (optional || this.match(tt.bracketL) || this.eat(tt.dot)) {
return this.parseMember(base, startPos, startLoc, state, optional);
} else {
state.stop = true;
return base;
const computed = this.eat(tt.bracketL);
if (computed || optional || this.eat(tt.dot)) {
return this.parseMember(
base,
startPos,
startLoc,
state,
computed,
optional,
);
} else {
state.stop = true;
return base;
}
}
}

Expand All @@ -736,10 +747,10 @@ export default class ExpressionParser extends LValParser {
startPos: number,
startLoc: Position,
state: N.ParseSubscriptState,
computed: boolean,
optional: boolean,
): N.OptionalMemberExpression | N.MemberExpression {
const node = this.startNodeAt(startPos, startLoc);
const computed = this.eat(tt.bracketL);
node.object = base;
node.computed = computed;
const privateName =
Expand Down
@@ -0,0 +1 @@
a.[b]
@@ -0,0 +1,3 @@
{
"throws": "Unexpected token (1:2)"
}

0 comments on commit 976bfbb

Please sign in to comment.