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

Throw a syntax error for a constructor with type parameters #12065

Merged
merged 3 commits into from Sep 17, 2020
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Makefile
@@ -1,6 +1,6 @@
FLOW_COMMIT = a1f9a4c709dcebb27a5084acf47755fbae699c25
TEST262_COMMIT = 058adfed86b1d4129996faaf50a85ea55379a66a
TYPESCRIPT_COMMIT = ffa35d3272647fe48ddf173e1f0928f772c18630
TYPESCRIPT_COMMIT = d779a190535e52896cfe5100101173c00b6b8625

FORCE_PUBLISH = "@babel/runtime,@babel/runtime-corejs2,@babel/runtime-corejs3,@babel/standalone"

Expand Down
5 changes: 5 additions & 0 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -65,6 +65,8 @@ type ParsingContext =
const TSErrors = Object.freeze({
ClassMethodHasDeclare: "Class methods cannot have the 'declare' modifier",
ClassMethodHasReadonly: "Class methods cannot have the 'readonly' modifier",
ConstructorHasTypeParameters:
"Type parameters cannot appear on a constructor declaration.",
DeclareClassFieldHasInitializer:
"'declare' class fields cannot have an initializer",
DuplicateModifier: "Duplicate modifier: '%0'",
Expand Down Expand Up @@ -2291,6 +2293,9 @@ export default (superClass: Class<Parser>): Class<Parser> =>
allowsDirectSuper: boolean,
): void {
const typeParameters = this.tsTryParseTypeParameters();
if (typeParameters && isConstructor) {
this.raise(typeParameters.start, TSErrors.ConstructorHasTypeParameters);
}
if (typeParameters) method.typeParameters = typeParameters;
super.pushClassMethod(
classBody,
Expand Down
@@ -0,0 +1,3 @@
class C {
constructor<T>(foo: T) {}
}
@@ -0,0 +1,84 @@
{
"type": "File",
"start":0,"end":39,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"errors": [
"SyntaxError: Type parameters cannot appear on a constructor declaration. (2:13)"
],
"program": {
"type": "Program",
"start":0,"end":39,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ClassDeclaration",
"start":0,"end":39,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"id": {
"type": "Identifier",
"start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7},"identifierName":"C"},
"name": "C"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start":8,"end":39,"loc":{"start":{"line":1,"column":8},"end":{"line":3,"column":1}},
"body": [
{
"type": "ClassMethod",
"start":12,"end":37,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":27}},
"static": false,
"key": {
"type": "Identifier",
"start":12,"end":23,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":13},"identifierName":"constructor"},
"name": "constructor"
},
"computed": false,
"kind": "constructor",
"typeParameters": {
"type": "TSTypeParameterDeclaration",
"start":23,"end":26,"loc":{"start":{"line":2,"column":13},"end":{"line":2,"column":16}},
"params": [
{
"type": "TSTypeParameter",
"start":24,"end":25,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":15}},
"name": "T"
}
]
},
"id": null,
"generator": false,
"async": false,
"params": [
{
"type": "Identifier",
"start":27,"end":33,"loc":{"start":{"line":2,"column":17},"end":{"line":2,"column":23},"identifierName":"foo"},
"name": "foo",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":30,"end":33,"loc":{"start":{"line":2,"column":20},"end":{"line":2,"column":23}},
"typeAnnotation": {
"type": "TSTypeReference",
"start":32,"end":33,"loc":{"start":{"line":2,"column":22},"end":{"line":2,"column":23}},
"typeName": {
"type": "Identifier",
"start":32,"end":33,"loc":{"start":{"line":2,"column":22},"end":{"line":2,"column":23},"identifierName":"T"},
"name": "T"
}
}
}
}
],
"body": {
"type": "BlockStatement",
"start":35,"end":37,"loc":{"start":{"line":2,"column":25},"end":{"line":2,"column":27}},
"body": [],
"directives": []
}
}
]
}
}
],
"directives": []
}
}
8 changes: 8 additions & 0 deletions scripts/parser-tests/typescript/allowlist.txt
Expand Up @@ -50,6 +50,7 @@ augmentedTypesVar.ts
bigintIndex.ts
binderBinaryExpressionStress.ts
binderBinaryExpressionStressJs.ts
bundledDtsLateExportRenaming.ts
cacheResolutions.ts
cachedModuleResolution1.ts
cachedModuleResolution2.ts
Expand Down Expand Up @@ -79,6 +80,7 @@ constDeclarations-validContexts.ts
constEnumNoEmitReexport.ts
constEnumNoPreserveDeclarationReexport.ts
constEnumPreserveEmitReexport.ts
controlFlowPrivateClassField.ts
convertKeywordsYes.ts
declarationEmitAmdModuleNameDirective.ts
declarationEmitComputedNameCausesImportToBePainted.ts
Expand All @@ -93,6 +95,7 @@ declarationEmitExpandoPropertyPrivateName.ts
declarationEmitExportAssignedNamespaceNoTripleSlashTypesReference.ts
declarationEmitExportAssignment.ts
declarationEmitExportDeclaration.ts
declarationEmitExpressionInExtends6.ts
declarationEmitForModuleImportingModuleAugmentationRetainsImport.ts
declarationEmitForTypesWhichNeedImportTypes.ts
declarationEmitInterfaceWithNonEntityNameExpressionHeritage.ts
Expand Down Expand Up @@ -180,6 +183,7 @@ esModuleInterop.ts
esModuleInteropImportTSLibHasImport.ts
esModuleInteropNamedDefaultImports.ts
esModuleInteropTslibHelpers.ts
expandoFunctionContextualTypesNoValue.ts
exportAssignClassAndModule.ts
exportAssignmentImportMergeNoCrash.ts
exportAssignmentMembersVisibleInAugmentation.ts
Expand Down Expand Up @@ -229,6 +233,7 @@ importDeclWithClassModifiers.ts
importDeclWithDeclareModifierInAmbientContext.ts
importHelpers.ts
importHelpersAmd.ts
importHelpersES6.ts
importHelpersInAmbientContext.ts
importHelpersInIsolatedModules.ts
importHelpersInTsx.tsx
Expand Down Expand Up @@ -273,6 +278,7 @@ jsdocAccessEnumType.ts
jsdocPropertyTagInvalid.ts
jsxAttributeWithoutExpressionReact.tsx
jsxIntrinsicElementsExtendsRecord.tsx
jsxIntrinsicElementsTypeArgumentErrors.tsx
letAndVarRedeclaration.ts
letAsIdentifier.ts
letAsIdentifierInStrictMode.ts
Expand Down Expand Up @@ -352,6 +358,7 @@ outModuleTripleSlashRefs.ts
parameterInitializerBeforeDestructuringEmit.ts
parameterPropertyOutsideConstructor.ts
parseGenericArrowRatherThanLeftShift.ts
parserConstructorDeclaration12.ts
pathMappingBasedModuleResolution3_classic.ts
pathMappingBasedModuleResolution3_node.ts
preserveUnusedImports.ts
Expand Down Expand Up @@ -405,6 +412,7 @@ typeReferenceDirectives5.ts
typeReferenceDirectives7.ts
typeReferenceDirectives8.ts
typeReferenceDirectives9.ts
uniqueSymbolPropertyDeclarationEmit.ts
unusedImports1.ts
unusedImports11.ts
unusedImports12.ts
Expand Down