Skip to content

Commit

Permalink
fix: do not push new token context when function is following dot/que…
Browse files Browse the repository at this point in the history
…stionDot
  • Loading branch information
JLHwung committed Apr 8, 2020
1 parent ce6cc4e commit cfd11e4
Show file tree
Hide file tree
Showing 5 changed files with 537 additions and 10 deletions.
13 changes: 4 additions & 9 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -2125,17 +2125,12 @@ export default class ExpressionParser extends LValParser {
} else if (this.state.type.keyword) {
name = this.state.type.keyword;

// `class` and `function` keywords push new context into this.context.
// `class` and `function` keywords push function-type token context into this.context.
// But there is no chance to pop the context if the keyword is consumed
// as an identifier such as a property name.
// If the previous token is a dot, this does not apply because the
// context-managing code already ignored the keyword
if (
(name === "class" || name === "function") &&
(this.state.lastTokEnd !== this.state.lastTokStart + 1 ||
this.input.charCodeAt(this.state.lastTokStart) !== charCodes.dot)
) {
this.state.context.pop();
const context = this.state.context;
if (context[context.length - 1].token === "function") {
context.pop();
}
} else {
throw this.unexpected();
Expand Down
5 changes: 4 additions & 1 deletion packages/babel-parser/src/tokenizer/context.js
Expand Up @@ -101,7 +101,10 @@ tt.incDec.updateContext = function() {
};

tt._function.updateContext = tt._class.updateContext = function(prevType) {
if (
if (prevType === tt.dot || prevType === tt.questionDot) {
// when function/class follows dot/questionDot, it is part of
// (optional)MemberExpression, then we don't need to push new token context
} else if (
prevType.beforeExpr &&
prevType !== tt.semi &&
prevType !== tt._else &&
Expand Down
@@ -0,0 +1 @@
<div>{(this?.class, this.class, this?.function, this.function)}</div>
@@ -0,0 +1,3 @@
{
"plugins": ["jsx", "flow"]
}

0 comments on commit cfd11e4

Please sign in to comment.