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

Do not use lookahead when parsing construct signature declarations in TS #9995

Merged
merged 1 commit into from May 21, 2019
Merged
Changes from all commits
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
40 changes: 20 additions & 20 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -397,13 +397,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>

tsParseSignatureMember(
kind: "TSCallSignatureDeclaration" | "TSConstructSignatureDeclaration",
node: N.TsCallSignatureDeclaration | N.TsConstructSignatureDeclaration,
): N.TsCallSignatureDeclaration | N.TsConstructSignatureDeclaration {
const node:
| N.TsCallSignatureDeclaration
| N.TsConstructSignatureDeclaration = this.startNode();
if (kind === "TSConstructSignatureDeclaration") {
this.expect(tt._new);
}
this.tsFillSignature(tt.colon, node);
this.tsParseTypeMemberSemicolon();
return this.finishNode(node, kind);
Expand Down Expand Up @@ -442,7 +437,6 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node: N.TsPropertySignature | N.TsMethodSignature,
readonly: boolean,
): N.TsPropertySignature | N.TsMethodSignature {
this.parsePropertyName(node);
if (this.eat(tt.question)) node.optional = true;
const nodeAny: any = node;

Expand All @@ -462,30 +456,36 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}

tsParseTypeMember(): N.TsTypeElement {
const node: any = this.startNode();

if (this.match(tt.parenL) || this.isRelational("<")) {
return this.tsParseSignatureMember("TSCallSignatureDeclaration");
return this.tsParseSignatureMember("TSCallSignatureDeclaration", node);
}
if (
this.match(tt._new) &&
this.tsLookAhead(this.tsIsStartOfConstructSignature.bind(this))
) {
return this.tsParseSignatureMember("TSConstructSignatureDeclaration");

if (this.match(tt._new)) {
const id: N.Identifier = this.startNode();
this.next();
if (this.match(tt.parenL) || this.isRelational("<")) {
return this.tsParseSignatureMember(
"TSConstructSignatureDeclaration",
node,
);
} else {
node.key = this.createIdentifier(id, "new");
return this.tsParsePropertyOrMethodSignature(node, false);
}
}
// Instead of fullStart, we create a node here.
const node: any = this.startNode();

const readonly = !!this.tsParseModifier(["readonly"]);

const idx = this.tsTryParseIndexSignature(node);
if (idx) {
if (readonly) node.readonly = true;
return idx;
}
return this.tsParsePropertyOrMethodSignature(node, readonly);
}

tsIsStartOfConstructSignature() {
this.next();
return this.match(tt.parenL) || this.isRelational("<");
this.parsePropertyName(node);
return this.tsParsePropertyOrMethodSignature(node, readonly);
}

tsParseTypeLiteral(): N.TsTypeLiteral {
Expand Down