Skip to content

Commit

Permalink
refactor: simplify updateContext
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Aug 10, 2020
1 parent 060d0f8 commit 57a3122
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
24 changes: 11 additions & 13 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -2306,20 +2306,23 @@ export default class ExpressionParser extends LValParser {
parseIdentifierName(pos: number, liberal?: boolean): string {
let name: string;

if (this.match(tt.name)) {
const { start, type } = this.state;

if (type === tt.name) {
name = this.state.value;
} else if (this.state.type.keyword) {
name = this.state.type.keyword;
} else if (type.keyword) {
name = type.keyword;

// `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.
const context = this.state.context;
const curContext = this.curContext();
if (
(name === "class" || name === "function") &&
context[context.length - 1].token === "function"
(type === tt._class || type === tt._function) &&
(curContext === ct.functionStatement ||
curContext === ct.functionExpression)
) {
context.pop();
this.state.context.pop();
}
} else {
throw this.unexpected();
Expand All @@ -2330,12 +2333,7 @@ export default class ExpressionParser extends LValParser {
// This will prevent this.next() from throwing about unexpected escapes.
this.state.type = tt.name;
} else {
this.checkReservedWord(
name,
this.state.start,
!!this.state.type.keyword,
false,
);
this.checkReservedWord(name, start, !!type.keyword, false);
}

this.next();
Expand Down
9 changes: 1 addition & 8 deletions packages/babel-parser/src/tokenizer/context.js
Expand Up @@ -103,10 +103,7 @@ tt.incDec.updateContext = function () {
};

tt._function.updateContext = tt._class.updateContext = function (prevType) {
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 (
if (
prevType.beforeExpr &&
prevType !== tt.semi &&
prevType !== tt._else &&
Expand All @@ -132,7 +129,3 @@ tt.backQuote.updateContext = function () {
}
this.state.exprAllowed = false;
};

tt.star.updateContext = function () {
this.state.exprAllowed = false;
};
3 changes: 2 additions & 1 deletion packages/babel-parser/src/tokenizer/types.js
Expand Up @@ -155,7 +155,8 @@ export const types: { [name: string]: TokenType } = {
plusMin: new TokenType("+/-", { beforeExpr, binop: 9, prefix, startsExpr }),
// startsExpr: required by v8intrinsic plugin
modulo: new TokenType("%", { beforeExpr, binop: 10, startsExpr }),
star: createBinop("*", 10),
// unset `beforeExpr` as it can be `function *`
star: new TokenType("*", { binop: 10 }),
slash: createBinop("/", 10),
exponent: new TokenType("**", {
beforeExpr,
Expand Down

0 comments on commit 57a3122

Please sign in to comment.