Skip to content

Commit

Permalink
Use lookaheadCharCode
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Oct 8, 2019
1 parent 0524164 commit c7b0d4a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
4 changes: 2 additions & 2 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -1291,7 +1291,7 @@ export default class ExpressionParser extends LValParser {
),
);

this.checkCommaAfterRest(tt.parenR);
this.checkCommaAfterRest(charCodes.rightParenthesis);

break;
} else {
Expand Down Expand Up @@ -1578,7 +1578,7 @@ export default class ExpressionParser extends LValParser {
this.next();
// Don't use parseRestBinding() as we only allow Identifier here.
prop.argument = this.parseIdentifier();
this.checkCommaAfterRest(tt.braceR);
this.checkCommaAfterRest(charCodes.rightCurlyBrace);
return this.finishNode(prop, "RestElement");
}

Expand Down
15 changes: 10 additions & 5 deletions packages/babel-parser/src/parser/lval.js
@@ -1,5 +1,6 @@
// @flow

import * as charCodes from "charcodes";
import { types as tt, type TokenType } from "../tokenizer/types";
import type {
TSParameterProperty,
Expand Down Expand Up @@ -248,7 +249,11 @@ export default class LValParser extends NodeUtils {
case tt.bracketL: {
const node = this.startNode();
this.next();
node.elements = this.parseBindingList(tt.bracketR, true);
node.elements = this.parseBindingList(
tt.bracketR,
charCodes.rightSquareBracket,
true,
);
return this.finishNode(node, "ArrayPattern");
}

Expand All @@ -261,6 +266,7 @@ export default class LValParser extends NodeUtils {

parseBindingList(
close: TokenType,
closeCharCode: $Values<typeof charCodes>,
allowEmpty?: boolean,
allowModifiers?: boolean,
): $ReadOnlyArray<Pattern | TSParameterProperty> {
Expand All @@ -279,7 +285,7 @@ export default class LValParser extends NodeUtils {
break;
} else if (this.match(tt.ellipsis)) {
elts.push(this.parseAssignableListItemTypes(this.parseRestBinding()));
this.checkCommaAfterRest(close);
this.checkCommaAfterRest(closeCharCode);
this.expect(close);
break;
} else {
Expand Down Expand Up @@ -469,10 +475,9 @@ export default class LValParser extends NodeUtils {
}
}

checkCommaAfterRest(close: TokenType): void {
checkCommaAfterRest(close: $Values<typeof charCodes>): void {
if (this.match(tt.comma)) {
// TODO: Use lookaheadCharCode after that https://github.com/babel/babel/pull/10371 is merged
if (this.lookahead().type === close) {
if (this.lookaheadCharCode() === close) {
this.raiseTrailingCommaAfterRest(this.state.start);
} else {
this.raiseRestNotLast(this.state.start);
Expand Down
1 change: 1 addition & 0 deletions packages/babel-parser/src/parser/statement.js
Expand Up @@ -1105,6 +1105,7 @@ export default class StatementParser extends ExpressionParser {
this.expect(tt.parenL);
node.params = this.parseBindingList(
tt.parenR,
charCodes.rightParenthesis,
/* allowEmpty */ false,
allowModifiers,
);
Expand Down
34 changes: 18 additions & 16 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -362,21 +362,23 @@ export default (superClass: Class<Parser>): Class<Parser> =>
tsParseBindingListForSignature(): $ReadOnlyArray<
N.Identifier | N.RestElement | N.ObjectPattern | N.ArrayPattern,
> {
return this.parseBindingList(tt.parenR).map(pattern => {
if (
pattern.type !== "Identifier" &&
pattern.type !== "RestElement" &&
pattern.type !== "ObjectPattern" &&
pattern.type !== "ArrayPattern"
) {
throw this.unexpected(
pattern.start,
"Name in a signature must be an Identifier, ObjectPattern or ArrayPattern," +
`instead got ${pattern.type}`,
);
}
return pattern;
});
return this.parseBindingList(tt.parenR, charCodes.rightParenthesis).map(
pattern => {
if (
pattern.type !== "Identifier" &&
pattern.type !== "RestElement" &&
pattern.type !== "ObjectPattern" &&
pattern.type !== "ArrayPattern"
) {
throw this.unexpected(
pattern.start,
"Name in a signature must be an Identifier, ObjectPattern or ArrayPattern," +
`instead got ${pattern.type}`,
);
}
return pattern;
},
);
}

tsParseTypeMemberSemicolon(): void {
Expand Down Expand Up @@ -585,7 +587,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
const restNode: N.TsRestType = this.startNode();
this.next(); // skips ellipsis
restNode.typeAnnotation = this.tsParseType();
this.checkCommaAfterRest(tt.braketR);
this.checkCommaAfterRest(charCodes.rightSquareBracket);
return this.finishNode(restNode, "TSRestType");
}

Expand Down

0 comments on commit c7b0d4a

Please sign in to comment.