Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: assure left bracket is not consumed after dot #13695

Merged
merged 2 commits into from Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 15 additions & 3 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -701,6 +701,7 @@ export default class ExpressionParser extends LValParser {
}

let optional = false;
let computed = false;
if (this.match(tt.questionDot)) {
if (noCalls && this.lookaheadCharCode() === charCodes.leftParenthesis) {
// stop at `?.` when parsing `new a?.()`
Expand All @@ -719,8 +720,19 @@ 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 if (
(computed = this.eat(tt.bracketL)) ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this is kinda ugly. Mind breaking this else if into a regular else and perform the mutation there?

optional ||
this.eat(tt.dot)
) {
return this.parseMember(
base,
startPos,
startLoc,
state,
computed,
optional,
);
} else {
state.stop = true;
return base;
Expand All @@ -736,10 +748,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)"
}