Allow unknown/any in TS catch clause param (#11755)
This commit is contained in:
parent
5b4b3a3e4a
commit
8a1d7e41f2
2
Makefile
2
Makefile
@ -1,6 +1,6 @@
|
||||
FLOW_COMMIT = a1f9a4c709dcebb27a5084acf47755fbae699c25
|
||||
TEST262_COMMIT = 058adfed86b1d4129996faaf50a85ea55379a66a
|
||||
TYPESCRIPT_COMMIT = 5fc917be2e4dd64c8e9504d36615cd7fbfdd4cd3
|
||||
TYPESCRIPT_COMMIT = ffa35d3272647fe48ddf173e1f0928f772c18630
|
||||
|
||||
FORCE_PUBLISH = "@babel/runtime,@babel/runtime-corejs2,@babel/runtime-corejs3,@babel/standalone"
|
||||
|
||||
|
||||
@ -636,6 +636,16 @@ export default class StatementParser extends ExpressionParser {
|
||||
return this.finishNode(node, "ThrowStatement");
|
||||
}
|
||||
|
||||
parseCatchClauseParam(): N.Identifier {
|
||||
const param = this.parseBindingAtom();
|
||||
|
||||
const simple = param.type === "Identifier";
|
||||
this.scope.enter(simple ? SCOPE_SIMPLE_CATCH : 0);
|
||||
this.checkLVal(param, BIND_LEXICAL, null, "catch clause");
|
||||
|
||||
return param;
|
||||
}
|
||||
|
||||
parseTryStatement(node: N.TryStatement): N.TryStatement {
|
||||
this.next();
|
||||
|
||||
@ -647,10 +657,7 @@ export default class StatementParser extends ExpressionParser {
|
||||
this.next();
|
||||
if (this.match(tt.parenL)) {
|
||||
this.expect(tt.parenL);
|
||||
clause.param = this.parseBindingAtom();
|
||||
const simple = clause.param.type === "Identifier";
|
||||
this.scope.enter(simple ? SCOPE_SIMPLE_CATCH : 0);
|
||||
this.checkLVal(clause.param, BIND_LEXICAL, null, "catch clause");
|
||||
clause.param = this.parseCatchClauseParam();
|
||||
this.expect(tt.parenR);
|
||||
} else {
|
||||
clause.param = null;
|
||||
|
||||
@ -2660,4 +2660,15 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
|
||||
return hasContextParam ? baseCount + 1 : baseCount;
|
||||
}
|
||||
|
||||
parseCatchClauseParam(): N.Identifier {
|
||||
const param = super.parseCatchClauseParam();
|
||||
const type = this.tsTryParseTypeAnnotation();
|
||||
|
||||
if (type) {
|
||||
param.type = type;
|
||||
}
|
||||
|
||||
return param;
|
||||
}
|
||||
};
|
||||
|
||||
9
packages/babel-parser/test/fixtures/typescript/catch-clause/unknown/input.ts
vendored
Normal file
9
packages/babel-parser/test/fixtures/typescript/catch-clause/unknown/input.ts
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
try {} catch (ex) {}
|
||||
try {} catch (ex: unknown) {}
|
||||
try {} catch (ex: any) {}
|
||||
|
||||
// The following can't be error'd at parse time
|
||||
try {} catch (ex: A) {}
|
||||
try {} catch (ex: Error) {}
|
||||
try {} catch (ex: string) {}
|
||||
try {} catch (ex: string | number) {}
|
||||
278
packages/babel-parser/test/fixtures/typescript/catch-clause/unknown/output.json
vendored
Normal file
278
packages/babel-parser/test/fixtures/typescript/catch-clause/unknown/output.json
vendored
Normal file
@ -0,0 +1,278 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start":0,"end":244,"loc":{"start":{"line":1,"column":0},"end":{"line":9,"column":37}},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start":0,"end":244,"loc":{"start":{"line":1,"column":0},"end":{"line":9,"column":37}},
|
||||
"sourceType": "module",
|
||||
"interpreter": null,
|
||||
"body": [
|
||||
{
|
||||
"type": "TryStatement",
|
||||
"start":0,"end":20,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":20}},
|
||||
"block": {
|
||||
"type": "BlockStatement",
|
||||
"start":4,"end":6,"loc":{"start":{"line":1,"column":4},"end":{"line":1,"column":6}},
|
||||
"body": [],
|
||||
"directives": []
|
||||
},
|
||||
"handler": {
|
||||
"type": "CatchClause",
|
||||
"start":7,"end":20,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":20}},
|
||||
"param": {
|
||||
"type": "Identifier",
|
||||
"start":14,"end":16,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":16},"identifierName":"ex"},
|
||||
"name": "ex"
|
||||
},
|
||||
"body": {
|
||||
"type": "BlockStatement",
|
||||
"start":18,"end":20,"loc":{"start":{"line":1,"column":18},"end":{"line":1,"column":20}},
|
||||
"body": [],
|
||||
"directives": []
|
||||
}
|
||||
},
|
||||
"finalizer": null
|
||||
},
|
||||
{
|
||||
"type": "TryStatement",
|
||||
"start":21,"end":50,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":29}},
|
||||
"block": {
|
||||
"type": "BlockStatement",
|
||||
"start":25,"end":27,"loc":{"start":{"line":2,"column":4},"end":{"line":2,"column":6}},
|
||||
"body": [],
|
||||
"directives": []
|
||||
},
|
||||
"handler": {
|
||||
"type": "CatchClause",
|
||||
"start":28,"end":50,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":29}},
|
||||
"param": {
|
||||
"type": {
|
||||
"type": "TSTypeAnnotation",
|
||||
"start":37,"end":46,"loc":{"start":{"line":2,"column":16},"end":{"line":2,"column":25}},
|
||||
"typeAnnotation": {
|
||||
"type": "TSUnknownKeyword",
|
||||
"start":39,"end":46,"loc":{"start":{"line":2,"column":18},"end":{"line":2,"column":25}}
|
||||
}
|
||||
},
|
||||
"start":35,"end":37,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":16},"identifierName":"ex"},
|
||||
"name": "ex"
|
||||
},
|
||||
"body": {
|
||||
"type": "BlockStatement",
|
||||
"start":48,"end":50,"loc":{"start":{"line":2,"column":27},"end":{"line":2,"column":29}},
|
||||
"body": [],
|
||||
"directives": []
|
||||
}
|
||||
},
|
||||
"finalizer": null
|
||||
},
|
||||
{
|
||||
"type": "TryStatement",
|
||||
"start":51,"end":76,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":25}},
|
||||
"block": {
|
||||
"type": "BlockStatement",
|
||||
"start":55,"end":57,"loc":{"start":{"line":3,"column":4},"end":{"line":3,"column":6}},
|
||||
"body": [],
|
||||
"directives": []
|
||||
},
|
||||
"handler": {
|
||||
"type": "CatchClause",
|
||||
"start":58,"end":76,"loc":{"start":{"line":3,"column":7},"end":{"line":3,"column":25}},
|
||||
"param": {
|
||||
"type": {
|
||||
"type": "TSTypeAnnotation",
|
||||
"start":67,"end":72,"loc":{"start":{"line":3,"column":16},"end":{"line":3,"column":21}},
|
||||
"typeAnnotation": {
|
||||
"type": "TSAnyKeyword",
|
||||
"start":69,"end":72,"loc":{"start":{"line":3,"column":18},"end":{"line":3,"column":21}}
|
||||
}
|
||||
},
|
||||
"start":65,"end":67,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":16},"identifierName":"ex"},
|
||||
"name": "ex"
|
||||
},
|
||||
"body": {
|
||||
"type": "BlockStatement",
|
||||
"start":74,"end":76,"loc":{"start":{"line":3,"column":23},"end":{"line":3,"column":25}},
|
||||
"body": [],
|
||||
"directives": []
|
||||
}
|
||||
},
|
||||
"finalizer": null,
|
||||
"trailingComments": [
|
||||
{
|
||||
"type": "CommentLine",
|
||||
"value": " The following can't be error'd at parse time",
|
||||
"start":78,"end":125,"loc":{"start":{"line":5,"column":0},"end":{"line":5,"column":47}}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "TryStatement",
|
||||
"start":126,"end":149,"loc":{"start":{"line":6,"column":0},"end":{"line":6,"column":23}},
|
||||
"block": {
|
||||
"type": "BlockStatement",
|
||||
"start":130,"end":132,"loc":{"start":{"line":6,"column":4},"end":{"line":6,"column":6}},
|
||||
"body": [],
|
||||
"directives": []
|
||||
},
|
||||
"handler": {
|
||||
"type": "CatchClause",
|
||||
"start":133,"end":149,"loc":{"start":{"line":6,"column":7},"end":{"line":6,"column":23}},
|
||||
"param": {
|
||||
"type": {
|
||||
"type": "TSTypeAnnotation",
|
||||
"start":142,"end":145,"loc":{"start":{"line":6,"column":16},"end":{"line":6,"column":19}},
|
||||
"typeAnnotation": {
|
||||
"type": "TSTypeReference",
|
||||
"start":144,"end":145,"loc":{"start":{"line":6,"column":18},"end":{"line":6,"column":19}},
|
||||
"typeName": {
|
||||
"type": "Identifier",
|
||||
"start":144,"end":145,"loc":{"start":{"line":6,"column":18},"end":{"line":6,"column":19},"identifierName":"A"},
|
||||
"name": "A"
|
||||
}
|
||||
}
|
||||
},
|
||||
"start":140,"end":142,"loc":{"start":{"line":6,"column":14},"end":{"line":6,"column":16},"identifierName":"ex"},
|
||||
"name": "ex"
|
||||
},
|
||||
"body": {
|
||||
"type": "BlockStatement",
|
||||
"start":147,"end":149,"loc":{"start":{"line":6,"column":21},"end":{"line":6,"column":23}},
|
||||
"body": [],
|
||||
"directives": []
|
||||
}
|
||||
},
|
||||
"finalizer": null,
|
||||
"leadingComments": [
|
||||
{
|
||||
"type": "CommentLine",
|
||||
"value": " The following can't be error'd at parse time",
|
||||
"start":78,"end":125,"loc":{"start":{"line":5,"column":0},"end":{"line":5,"column":47}}
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "TryStatement",
|
||||
"start":150,"end":177,"loc":{"start":{"line":7,"column":0},"end":{"line":7,"column":27}},
|
||||
"block": {
|
||||
"type": "BlockStatement",
|
||||
"start":154,"end":156,"loc":{"start":{"line":7,"column":4},"end":{"line":7,"column":6}},
|
||||
"body": [],
|
||||
"directives": []
|
||||
},
|
||||
"handler": {
|
||||
"type": "CatchClause",
|
||||
"start":157,"end":177,"loc":{"start":{"line":7,"column":7},"end":{"line":7,"column":27}},
|
||||
"param": {
|
||||
"type": {
|
||||
"type": "TSTypeAnnotation",
|
||||
"start":166,"end":173,"loc":{"start":{"line":7,"column":16},"end":{"line":7,"column":23}},
|
||||
"typeAnnotation": {
|
||||
"type": "TSTypeReference",
|
||||
"start":168,"end":173,"loc":{"start":{"line":7,"column":18},"end":{"line":7,"column":23}},
|
||||
"typeName": {
|
||||
"type": "Identifier",
|
||||
"start":168,"end":173,"loc":{"start":{"line":7,"column":18},"end":{"line":7,"column":23},"identifierName":"Error"},
|
||||
"name": "Error"
|
||||
}
|
||||
}
|
||||
},
|
||||
"start":164,"end":166,"loc":{"start":{"line":7,"column":14},"end":{"line":7,"column":16},"identifierName":"ex"},
|
||||
"name": "ex"
|
||||
},
|
||||
"body": {
|
||||
"type": "BlockStatement",
|
||||
"start":175,"end":177,"loc":{"start":{"line":7,"column":25},"end":{"line":7,"column":27}},
|
||||
"body": [],
|
||||
"directives": []
|
||||
}
|
||||
},
|
||||
"finalizer": null
|
||||
},
|
||||
{
|
||||
"type": "TryStatement",
|
||||
"start":178,"end":206,"loc":{"start":{"line":8,"column":0},"end":{"line":8,"column":28}},
|
||||
"block": {
|
||||
"type": "BlockStatement",
|
||||
"start":182,"end":184,"loc":{"start":{"line":8,"column":4},"end":{"line":8,"column":6}},
|
||||
"body": [],
|
||||
"directives": []
|
||||
},
|
||||
"handler": {
|
||||
"type": "CatchClause",
|
||||
"start":185,"end":206,"loc":{"start":{"line":8,"column":7},"end":{"line":8,"column":28}},
|
||||
"param": {
|
||||
"type": {
|
||||
"type": "TSTypeAnnotation",
|
||||
"start":194,"end":202,"loc":{"start":{"line":8,"column":16},"end":{"line":8,"column":24}},
|
||||
"typeAnnotation": {
|
||||
"type": "TSStringKeyword",
|
||||
"start":196,"end":202,"loc":{"start":{"line":8,"column":18},"end":{"line":8,"column":24}}
|
||||
}
|
||||
},
|
||||
"start":192,"end":194,"loc":{"start":{"line":8,"column":14},"end":{"line":8,"column":16},"identifierName":"ex"},
|
||||
"name": "ex"
|
||||
},
|
||||
"body": {
|
||||
"type": "BlockStatement",
|
||||
"start":204,"end":206,"loc":{"start":{"line":8,"column":26},"end":{"line":8,"column":28}},
|
||||
"body": [],
|
||||
"directives": []
|
||||
}
|
||||
},
|
||||
"finalizer": null
|
||||
},
|
||||
{
|
||||
"type": "TryStatement",
|
||||
"start":207,"end":244,"loc":{"start":{"line":9,"column":0},"end":{"line":9,"column":37}},
|
||||
"block": {
|
||||
"type": "BlockStatement",
|
||||
"start":211,"end":213,"loc":{"start":{"line":9,"column":4},"end":{"line":9,"column":6}},
|
||||
"body": [],
|
||||
"directives": []
|
||||
},
|
||||
"handler": {
|
||||
"type": "CatchClause",
|
||||
"start":214,"end":244,"loc":{"start":{"line":9,"column":7},"end":{"line":9,"column":37}},
|
||||
"param": {
|
||||
"type": {
|
||||
"type": "TSTypeAnnotation",
|
||||
"start":223,"end":240,"loc":{"start":{"line":9,"column":16},"end":{"line":9,"column":33}},
|
||||
"typeAnnotation": {
|
||||
"type": "TSUnionType",
|
||||
"start":225,"end":240,"loc":{"start":{"line":9,"column":18},"end":{"line":9,"column":33}},
|
||||
"types": [
|
||||
{
|
||||
"type": "TSStringKeyword",
|
||||
"start":225,"end":231,"loc":{"start":{"line":9,"column":18},"end":{"line":9,"column":24}}
|
||||
},
|
||||
{
|
||||
"type": "TSNumberKeyword",
|
||||
"start":234,"end":240,"loc":{"start":{"line":9,"column":27},"end":{"line":9,"column":33}}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"start":221,"end":223,"loc":{"start":{"line":9,"column":14},"end":{"line":9,"column":16},"identifierName":"ex"},
|
||||
"name": "ex"
|
||||
},
|
||||
"body": {
|
||||
"type": "BlockStatement",
|
||||
"start":242,"end":244,"loc":{"start":{"line":9,"column":35},"end":{"line":9,"column":37}},
|
||||
"body": [],
|
||||
"directives": []
|
||||
}
|
||||
},
|
||||
"finalizer": null
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
},
|
||||
"comments": [
|
||||
{
|
||||
"type": "CommentLine",
|
||||
"value": " The following can't be error'd at parse time",
|
||||
"start":78,"end":125,"loc":{"start":{"line":5,"column":0},"end":{"line":5,"column":47}}
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -48,6 +48,8 @@ augmentedTypesFunction.ts
|
||||
augmentedTypesInterface.ts
|
||||
augmentedTypesVar.ts
|
||||
bigintIndex.ts
|
||||
binderBinaryExpressionStress.ts
|
||||
binderBinaryExpressionStressJs.ts
|
||||
cacheResolutions.ts
|
||||
cachedModuleResolution1.ts
|
||||
cachedModuleResolution2.ts
|
||||
@ -64,6 +66,7 @@ classCannotExtendVar.ts
|
||||
classExpressionWithDecorator1.ts
|
||||
classExtendsAcrossFiles.ts
|
||||
classExtendsMultipleBaseClasses.ts
|
||||
classIndexer5.ts
|
||||
classOverloadForFunction.ts
|
||||
collisionExportsRequireAndClass.ts
|
||||
commonSourceDir5.ts
|
||||
@ -87,8 +90,10 @@ declarationEmitDestructuringOptionalBindingParametersInOverloads.ts
|
||||
declarationEmitDestructuringParameterProperties.ts
|
||||
declarationEmitDestructuringWithOptionalBindingParameters.ts
|
||||
declarationEmitExpandoPropertyPrivateName.ts
|
||||
declarationEmitExportAssignedNamespaceNoTripleSlashTypesReference.ts
|
||||
declarationEmitExportAssignment.ts
|
||||
declarationEmitExportDeclaration.ts
|
||||
declarationEmitForModuleImportingModuleAugmentationRetainsImport.ts
|
||||
declarationEmitForTypesWhichNeedImportTypes.ts
|
||||
declarationEmitInterfaceWithNonEntityNameExpressionHeritage.ts
|
||||
declarationEmitPrefersPathKindBasedOnBundling.ts
|
||||
@ -98,6 +103,7 @@ declarationImportTypeAliasInferredAndEmittable.ts
|
||||
declarationMapsMultifile.ts
|
||||
declarationMapsOutFile.ts
|
||||
declarationsForInferredTypeFromOtherFile.ts
|
||||
declarationsIndirectGeneratedAliasReference.ts
|
||||
declareModifierOnImport1.ts
|
||||
decoratorMetadataRestParameterWithImportedType.ts
|
||||
decoratorMetadataWithImportDeclarationNameCollision.ts
|
||||
@ -195,6 +201,7 @@ exportSameNameFuncVar.ts
|
||||
exportSpecifierAndExportedMemberDeclaration.ts
|
||||
exportSpecifierAndLocalMemberDeclaration.ts
|
||||
exportStarFromEmptyModule.ts
|
||||
exportStarNotElided.ts
|
||||
expressionsForbiddenInParameterInitializers.ts
|
||||
extendingClassFromAliasAndUsageInIndexer.ts
|
||||
extendsClauseAlreadySeen.ts
|
||||
@ -209,6 +216,8 @@ functionExpressionInWithBlock.ts
|
||||
functionExpressionWithResolutionOfTypeNamedArguments01.ts
|
||||
gettersAndSettersErrors.ts
|
||||
giant.ts
|
||||
globalThisDeclarationEmit.ts
|
||||
globalThisDeclarationEmit2.ts
|
||||
implementClausePrecedingExtends.ts
|
||||
implementsClauseAlreadySeen.ts
|
||||
importAndVariableDeclarationConflict1.ts
|
||||
@ -227,13 +236,24 @@ importHelpersNoHelpers.ts
|
||||
importHelpersNoModule.ts
|
||||
importHelpersOutFile.ts
|
||||
importHelpersSystem.ts
|
||||
importNonExportedMember10.ts
|
||||
importNonExportedMember11.ts
|
||||
importNonExportedMember4.ts
|
||||
importNonExportedMember5.ts
|
||||
importNonExportedMember6.ts
|
||||
importNonExportedMember7.ts
|
||||
importNonExportedMember8.ts
|
||||
importNonExportedMember9.ts
|
||||
importWithTrailingSlash.ts
|
||||
importedEnumMemberMergedWithExportedAliasIsError.ts
|
||||
importedModuleClassNameClash.ts
|
||||
indexSignatureWithAccessibilityModifier.ts
|
||||
indexSignatureWithInitializer1.ts
|
||||
indexSignatureWithTrailingComma.ts
|
||||
indexTypeCheck.ts
|
||||
indexWithoutParamType.ts
|
||||
indexerSignatureWithRestParam.ts
|
||||
inferrenceInfiniteLoopWithSubtyping.ts
|
||||
initializedParameterBeforeNonoptionalNotOptional.ts
|
||||
interfaceMayNotBeExtendedWitACall.ts
|
||||
interfaceWithImplements1.ts
|
||||
@ -242,12 +262,15 @@ isLiteral1.ts
|
||||
isLiteral2.ts
|
||||
isolatedModulesReExportType.ts
|
||||
jsEnumTagOnObjectFrozen.ts
|
||||
jsExportMemberMergedWithModuleAugmentation.ts
|
||||
jsFileCompilationBindDuplicateIdentifier.ts
|
||||
jsFileCompilationDuplicateFunctionImplementation.ts
|
||||
jsFileCompilationDuplicateFunctionImplementationFileOrderReversed.ts
|
||||
jsFileCompilationExternalPackageError.ts
|
||||
jsFileImportPreservedWhenUsed.ts
|
||||
jsNoImplicitAnyNoCascadingReferenceErrors.ts
|
||||
jsdocAccessEnumType.ts
|
||||
jsdocPropertyTagInvalid.ts
|
||||
jsxAttributeWithoutExpressionReact.tsx
|
||||
jsxIntrinsicElementsExtendsRecord.tsx
|
||||
letAndVarRedeclaration.ts
|
||||
@ -335,6 +358,8 @@ preserveUnusedImports.ts
|
||||
privacyCheckExternalModuleExportAssignmentOfGenericClass.ts
|
||||
privacyTopLevelAmbientExternalModuleImportWithExport.ts
|
||||
privacyTopLevelAmbientExternalModuleImportWithoutExport.ts
|
||||
privateFieldAssignabilityFromUnknown.ts
|
||||
privateNameWeakMapCollision.ts
|
||||
reExportGlobalDeclaration1.ts
|
||||
reExportUndefined1.ts
|
||||
reExportUndefined2.ts
|
||||
@ -388,6 +413,7 @@ unusedImports3.ts
|
||||
unusedImports4.ts
|
||||
unusedImports5.ts
|
||||
unusedInvalidTypeArguments.ts
|
||||
usedImportNotElidedInJs.ts
|
||||
varAndFunctionShareName.ts
|
||||
varArgConstructorMemberParameter.ts
|
||||
withStatement.ts
|
||||
|
||||
@ -40,7 +40,7 @@ module.exports = [
|
||||
"TS1163", // A 'yield' expression is only allowed in a generator body.
|
||||
"TS1184", // Modifiers cannot appear here.
|
||||
"TS1191", // An import declaration cannot have modifiers.
|
||||
"TS1196", // Catch clause variable cannot have a type annotation.
|
||||
"TS1196", // Catch clause variable type annotation must be 'any' or 'unknown' if specified.
|
||||
"TS1197", // Catch clause variable cannot have an initializer.
|
||||
"TS1200", // Line terminator not permitted before arrow.
|
||||
"TS1312", // '=' can only be used in an object literal property inside a destructuring assignment.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user