From 7028a14c7f8d0d6ae8954ff9020ec48c241293de Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Fri, 18 Sep 2020 02:03:33 +0900 Subject: [PATCH] fix: throw for constructors with type parameters (#12065) * Throw a syntax error for a constructor with type parameters * Modify to match error location with TypeScript * Update typescript parser tests Update allowlist.txt --- Makefile | 2 +- .../src/plugins/typescript/index.js | 5 ++ .../constructor-with-type-parameters/input.ts | 3 + .../output.json | 84 +++++++++++++++++++ scripts/parser-tests/typescript/allowlist.txt | 8 ++ 5 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 packages/babel-parser/test/fixtures/typescript/class/constructor-with-type-parameters/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/class/constructor-with-type-parameters/output.json diff --git a/Makefile b/Makefile index 9b28f23d1a..6537e089b8 100644 --- a/Makefile +++ b/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" diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index f18b78f109..785639ead9 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -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'", @@ -2296,6 +2298,9 @@ export default (superClass: Class): Class => allowsDirectSuper: boolean, ): void { const typeParameters = this.tsTryParseTypeParameters(); + if (typeParameters && isConstructor) { + this.raise(typeParameters.start, TSErrors.ConstructorHasTypeParameters); + } if (typeParameters) method.typeParameters = typeParameters; super.pushClassMethod( classBody, diff --git a/packages/babel-parser/test/fixtures/typescript/class/constructor-with-type-parameters/input.ts b/packages/babel-parser/test/fixtures/typescript/class/constructor-with-type-parameters/input.ts new file mode 100644 index 0000000000..0c51dfc5eb --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/class/constructor-with-type-parameters/input.ts @@ -0,0 +1,3 @@ +class C { + constructor(foo: T) {} +} diff --git a/packages/babel-parser/test/fixtures/typescript/class/constructor-with-type-parameters/output.json b/packages/babel-parser/test/fixtures/typescript/class/constructor-with-type-parameters/output.json new file mode 100644 index 0000000000..38964cd9c2 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/class/constructor-with-type-parameters/output.json @@ -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": [] + } +} \ No newline at end of file diff --git a/scripts/parser-tests/typescript/allowlist.txt b/scripts/parser-tests/typescript/allowlist.txt index ad5eb837ec..8bacc1165c 100644 --- a/scripts/parser-tests/typescript/allowlist.txt +++ b/scripts/parser-tests/typescript/allowlist.txt @@ -50,6 +50,7 @@ augmentedTypesVar.ts bigintIndex.ts binderBinaryExpressionStress.ts binderBinaryExpressionStressJs.ts +bundledDtsLateExportRenaming.ts cacheResolutions.ts cachedModuleResolution1.ts cachedModuleResolution2.ts @@ -79,6 +80,7 @@ constDeclarations-validContexts.ts constEnumNoEmitReexport.ts constEnumNoPreserveDeclarationReexport.ts constEnumPreserveEmitReexport.ts +controlFlowPrivateClassField.ts convertKeywordsYes.ts declarationEmitAmdModuleNameDirective.ts declarationEmitComputedNameCausesImportToBePainted.ts @@ -93,6 +95,7 @@ declarationEmitExpandoPropertyPrivateName.ts declarationEmitExportAssignedNamespaceNoTripleSlashTypesReference.ts declarationEmitExportAssignment.ts declarationEmitExportDeclaration.ts +declarationEmitExpressionInExtends6.ts declarationEmitForModuleImportingModuleAugmentationRetainsImport.ts declarationEmitForTypesWhichNeedImportTypes.ts declarationEmitInterfaceWithNonEntityNameExpressionHeritage.ts @@ -180,6 +183,7 @@ esModuleInterop.ts esModuleInteropImportTSLibHasImport.ts esModuleInteropNamedDefaultImports.ts esModuleInteropTslibHelpers.ts +expandoFunctionContextualTypesNoValue.ts exportAssignClassAndModule.ts exportAssignmentImportMergeNoCrash.ts exportAssignmentMembersVisibleInAugmentation.ts @@ -229,6 +233,7 @@ importDeclWithClassModifiers.ts importDeclWithDeclareModifierInAmbientContext.ts importHelpers.ts importHelpersAmd.ts +importHelpersES6.ts importHelpersInAmbientContext.ts importHelpersInIsolatedModules.ts importHelpersInTsx.tsx @@ -273,6 +278,7 @@ jsdocAccessEnumType.ts jsdocPropertyTagInvalid.ts jsxAttributeWithoutExpressionReact.tsx jsxIntrinsicElementsExtendsRecord.tsx +jsxIntrinsicElementsTypeArgumentErrors.tsx letAndVarRedeclaration.ts letAsIdentifier.ts letAsIdentifierInStrictMode.ts @@ -352,6 +358,7 @@ outModuleTripleSlashRefs.ts parameterInitializerBeforeDestructuringEmit.ts parameterPropertyOutsideConstructor.ts parseGenericArrowRatherThanLeftShift.ts +parserConstructorDeclaration12.ts pathMappingBasedModuleResolution3_classic.ts pathMappingBasedModuleResolution3_node.ts preserveUnusedImports.ts @@ -405,6 +412,7 @@ typeReferenceDirectives5.ts typeReferenceDirectives7.ts typeReferenceDirectives8.ts typeReferenceDirectives9.ts +uniqueSymbolPropertyDeclarationEmit.ts unusedImports1.ts unusedImports11.ts unusedImports12.ts