Support TypeScript 4.2 abstract constructor signatures (#12628)

Co-authored-by: Huáng Jùnliàng <jlhwung@gmail.com>
Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
This commit is contained in:
Sosuke Suzuki
2021-02-22 04:07:55 +09:00
committed by GitHub
parent 74dc70aabb
commit e4588bed22
15 changed files with 210 additions and 5 deletions

View File

@@ -790,10 +790,14 @@ export default (superClass: Class<Parser>): Class<Parser> =>
tsParseFunctionOrConstructorType(
type: "TSFunctionType" | "TSConstructorType",
abstract?: boolean,
): N.TsFunctionOrConstructorType {
const node: N.TsFunctionOrConstructorType = this.startNode();
if (type === "TSConstructorType") {
this.expect(tt._new);
// $FlowIgnore
node.abstract = !!abstract;
if (abstract) this.next();
this.next(); // eat `new`
}
this.tsFillSignature(tt.arrow, node);
return this.finishNode(node, type);
@@ -1219,6 +1223,10 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return this.finishNode(node, "TSConditionalType");
}
isAbstractConstructorSignature(): boolean {
return this.isContextual("abstract") && this.lookahead().type === tt._new;
}
tsParseNonConditionalType(): N.TsType {
if (this.tsIsStartOfFunctionType()) {
return this.tsParseFunctionOrConstructorType("TSFunctionType");
@@ -1226,6 +1234,12 @@ export default (superClass: Class<Parser>): Class<Parser> =>
if (this.match(tt._new)) {
// As in `new () => Date`
return this.tsParseFunctionOrConstructorType("TSConstructorType");
} else if (this.isAbstractConstructorSignature()) {
// As in `abstract new () => Date`
return this.tsParseFunctionOrConstructorType(
"TSConstructorType",
/* abstract */ true,
);
}
return this.tsParseUnionTypeOrHigher();
}

View File

@@ -1250,6 +1250,7 @@ export type TsConstructorType = TsTypeBase &
TsSignatureDeclarationBase & {
type: "TSConstructorType",
typeAnnotation: TsTypeAnnotation,
abstract: boolean,
};
export type TsTypeReference = TsTypeBase & {