Skip to content

Commit

Permalink
refactor: simplify tt.name logic
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Jul 23, 2020
1 parent d7a1afd commit c21bee5
Showing 1 changed file with 28 additions and 31 deletions.
59 changes: 28 additions & 31 deletions packages/babel-parser/src/parser/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -981,45 +981,42 @@ export default class ExpressionParser extends LValParser {
return this.finishNode(node, "ThisExpression");

case tt.name: {
node = this.startNode();
const containsEsc = this.state.containsEsc;
const id = this.parseIdentifier();

if (
!containsEsc &&
id.name === "async" &&
this.match(tt._function) &&
!this.canInsertSemicolon()
) {
const last = this.state.context.length - 1;
if (this.state.context[last] !== ct.functionStatement) {
// Since "async" is an identifier and normally identifiers
// can't be followed by expression, the tokenizer assumes
// that "function" starts a statement.
// Fixing it in the tokenizer would mean tracking not only the
// previous token ("async"), but also the one before to know
// its beforeExpr value.
// It's easier and more efficient to adjust the context here.
throw new Error("Internal error");
if (!containsEsc && id.name === "async" && !this.canInsertSemicolon()) {
if (this.match(tt._function)) {
const last = this.state.context.length - 1;
if (this.state.context[last] !== ct.functionStatement) {
// Since "async" is an identifier and normally identifiers
// can't be followed by expression, the tokenizer assumes
// that "function" starts a statement.
// Fixing it in the tokenizer would mean tracking not only the
// previous token ("async"), but also the one before to know
// its beforeExpr value.
// It's easier and more efficient to adjust the context here.
throw new Error("Internal error");
}
this.state.context[last] = ct.functionExpression;

this.next();
return this.parseFunction(
this.startNodeAtNode(id),
undefined,
true,
);
} else if (this.match(tt.name)) {
return this.parseAsyncArrowUnaryFunction(id);
}
this.state.context[last] = ct.functionExpression;

this.next();
return this.parseFunction(node, undefined, true);
} else if (
canBeArrow &&
!containsEsc &&
id.name === "async" &&
this.match(tt.name) &&
!this.canInsertSemicolon()
) {
return this.parseAsyncArrowUnaryFunction(id);
}

if (canBeArrow && this.match(tt.arrow) && !this.canInsertSemicolon()) {
this.next();
this.parseArrowExpression(node, [id], false);
return node;
return this.parseArrowExpression(
this.startNodeAtNode(id),
[id],
false,
);
}

return id;
Expand Down

0 comments on commit c21bee5

Please sign in to comment.