From ef5c51531b6bb434c2d174a2f86bb7e2d366f622 Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Fri, 29 Sep 2023 22:27:20 +0300 Subject: [PATCH] feat(18535): add support throw expressions --- src/compiler/emitter.ts | 9 + src/compiler/factory/nodeFactory.ts | 19 ++ src/compiler/parser.ts | 66 ++++ src/compiler/transformers/esnext.ts | 10 + src/compiler/types.ts | 9 + src/compiler/utilitiesPublic.ts | 1 + src/compiler/visitorPublic.ts | 7 + .../throwExpression1.errors.txt | 11 + tests/baselines/reference/api/typescript.d.ts | 313 +++++++++--------- tests/baselines/reference/throwExpression1.js | 26 ++ .../reference/throwExpression1.symbols | 17 + .../reference/throwExpression1.types | 21 ++ tests/baselines/reference/throwExpression2.js | 23 ++ .../reference/throwExpression2.symbols | 23 ++ .../reference/throwExpression2.types | 29 ++ tests/baselines/reference/throwExpression3.js | 32 ++ .../reference/throwExpression3.symbols | 33 ++ .../reference/throwExpression3.types | 48 +++ tests/baselines/reference/throwExpression4.js | 10 + .../reference/throwExpression4.symbols | 6 + .../reference/throwExpression4.types | 9 + .../reference/throwExpression5.errors.txt | 35 ++ tests/baselines/reference/throwExpression5.js | 50 +++ .../reference/throwExpression5.symbols | 53 +++ .../reference/throwExpression5.types | 68 ++++ .../throwExpression/throwExpression1.ts | 5 + .../throwExpression/throwExpression2.ts | 7 + .../throwExpression/throwExpression3.ts | 11 + .../throwExpression/throwExpression4.ts | 1 + .../throwExpression/throwExpression5.ts | 19 ++ 30 files changed, 818 insertions(+), 153 deletions(-) create mode 100644 src/typingsInstallerCore/throwExpression1.errors.txt create mode 100644 tests/baselines/reference/throwExpression1.js create mode 100644 tests/baselines/reference/throwExpression1.symbols create mode 100644 tests/baselines/reference/throwExpression1.types create mode 100644 tests/baselines/reference/throwExpression2.js create mode 100644 tests/baselines/reference/throwExpression2.symbols create mode 100644 tests/baselines/reference/throwExpression2.types create mode 100644 tests/baselines/reference/throwExpression3.js create mode 100644 tests/baselines/reference/throwExpression3.symbols create mode 100644 tests/baselines/reference/throwExpression3.types create mode 100644 tests/baselines/reference/throwExpression4.js create mode 100644 tests/baselines/reference/throwExpression4.symbols create mode 100644 tests/baselines/reference/throwExpression4.types create mode 100644 tests/baselines/reference/throwExpression5.errors.txt create mode 100644 tests/baselines/reference/throwExpression5.js create mode 100644 tests/baselines/reference/throwExpression5.symbols create mode 100644 tests/baselines/reference/throwExpression5.types create mode 100644 tests/cases/conformance/throwExpression/throwExpression1.ts create mode 100644 tests/cases/conformance/throwExpression/throwExpression2.ts create mode 100644 tests/cases/conformance/throwExpression/throwExpression3.ts create mode 100644 tests/cases/conformance/throwExpression/throwExpression4.ts create mode 100644 tests/cases/conformance/throwExpression/throwExpression5.ts diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 3d035053761af..668f620cb1766 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -422,6 +422,7 @@ import { TemplateLiteralTypeSpan, TemplateSpan, TextRange, + ThrowExpression, ThrowStatement, TokenFlags, tokenToString, @@ -2278,6 +2279,8 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri return emitSpreadElement(node as SpreadElement); case SyntaxKind.ClassExpression: return emitClassExpression(node as ClassExpression); + case SyntaxKind.ThrowExpression: + return emitThrowExpression(node as ThrowExpression); case SyntaxKind.OmittedExpression: return; case SyntaxKind.AsExpression: @@ -3367,6 +3370,12 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri emitClassDeclarationOrExpression(node); } + function emitThrowExpression(node: ThrowExpression) { + emitTokenWithComment(SyntaxKind.ThrowKeyword, node.pos, writeKeyword, node); + writeSpace(); + emitExpression(node.expression, parenthesizer.parenthesizeOperandOfPrefixUnary); + } + function emitExpressionWithTypeArguments(node: ExpressionWithTypeArguments) { emitExpression(node.expression, parenthesizer.parenthesizeLeftSideOfAccess); emitTypeArguments(node, node.typeArguments); diff --git a/src/compiler/factory/nodeFactory.ts b/src/compiler/factory/nodeFactory.ts index c01dee21917dd..bfd0762356025 100644 --- a/src/compiler/factory/nodeFactory.ts +++ b/src/compiler/factory/nodeFactory.ts @@ -438,6 +438,7 @@ import { TextRange, ThisExpression, ThisTypeNode, + ThrowExpression, ThrowStatement, Token, TokenFlags, @@ -679,6 +680,8 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode updateFunctionExpression, createArrowFunction, updateArrowFunction, + createThrowExpression, + updateThrowExpression, createDeleteExpression, updateDeleteExpression, createTypeOfExpression, @@ -3317,6 +3320,22 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode : node; } + // @api + function createThrowExpression(expression: Expression) { + const node = createBaseNode(SyntaxKind.ThrowExpression); + node.expression = parenthesizerRules().parenthesizeOperandOfPrefixUnary(expression); + node.transformFlags |= propagateChildFlags(node.expression); + node.transformFlags |= TransformFlags.ContainsESNext; + return node; + } + + // @api + function updateThrowExpression(node: ThrowExpression, expression: Expression) { + return node.expression !== expression + ? update(createThrowExpression(expression), node) + : node; + } + // @api function createDeleteExpression(expression: Expression) { const node = createBaseNode(SyntaxKind.DeleteExpression); diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 7511678393d2b..353f33ea088f6 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -360,6 +360,7 @@ import { textToKeywordObj, ThisExpression, ThisTypeNode, + ThrowExpression, ThrowStatement, toArray, Token, @@ -991,6 +992,9 @@ const forEachChildTable: ForEachChildTable = { return visitNode(cbNode, node.expression) || visitNodes(cbNode, cbNodes, node.typeArguments); }, + [SyntaxKind.ThrowExpression]: function forEachChildInThrowExpression(node: ThrowExpression, cbNode: (node: Node) => T | undefined, _cbNodes?: (nodes: NodeArray) => T | undefined): T | undefined { + return visitNode(cbNode, node.expression); + }, [SyntaxKind.ExternalModuleReference]: function forEachChildInExternalModuleReference(node: ExternalModuleReference, cbNode: (node: Node) => T | undefined, _cbNodes?: (nodes: NodeArray) => T | undefined): T | undefined { return visitNode(cbNode, node.expression); }, @@ -5505,6 +5509,7 @@ namespace Parser { token() !== SyntaxKind.SemicolonToken && token() !== SyntaxKind.FunctionKeyword && token() !== SyntaxKind.ClassKeyword && + token() !== SyntaxKind.ThrowKeyword && isStartOfStatement() && !isStartOfExpressionStatement() ) { @@ -5658,6 +5663,64 @@ namespace Parser { return finishNode(factory.createPrefixUnaryExpression(token() as PrefixUnaryOperator, nextTokenAnd(parseSimpleUnaryExpression)), pos); } + function parseThrowExpression() { + const pos = getNodePos(); + const expression = nextTokenAnd(parseSimpleUnaryExpression); + if (isInfixPunctuationToken()) { + parseErrorAtCurrentToken(Diagnostics._0_expected, tokenToString(SyntaxKind.SemicolonToken)); + } + return finishNode(factory.createThrowExpression(expression), pos); + } + + function isInfixPunctuationToken() { + switch (token()) { + case SyntaxKind.CommaToken: + case SyntaxKind.LessThanToken: + case SyntaxKind.GreaterThanToken: + case SyntaxKind.EqualsToken: + case SyntaxKind.LessThanEqualsToken: + case SyntaxKind.GreaterThanEqualsToken: + case SyntaxKind.EqualsEqualsToken: + case SyntaxKind.ExclamationEqualsToken: + case SyntaxKind.EqualsEqualsEqualsToken: + case SyntaxKind.ExclamationEqualsEqualsToken: + case SyntaxKind.PlusToken: + case SyntaxKind.MinusToken: + case SyntaxKind.AsteriskToken: + case SyntaxKind.SlashToken: + case SyntaxKind.PercentToken: + case SyntaxKind.AmpersandToken: + case SyntaxKind.BarToken: + case SyntaxKind.CaretToken: + case SyntaxKind.AsteriskAsteriskToken: + case SyntaxKind.LessThanLessThanToken: + case SyntaxKind.GreaterThanGreaterThanToken: + case SyntaxKind.GreaterThanGreaterThanGreaterThanToken: + case SyntaxKind.AmpersandAmpersandToken: + case SyntaxKind.BarBarToken: + case SyntaxKind.QuestionQuestionToken: + case SyntaxKind.PlusEqualsToken: + case SyntaxKind.MinusEqualsToken: + case SyntaxKind.AsteriskEqualsToken: + case SyntaxKind.SlashEqualsToken: + case SyntaxKind.PercentEqualsToken: + case SyntaxKind.AmpersandEqualsToken: + case SyntaxKind.BarEqualsToken: + case SyntaxKind.CaretEqualsToken: + case SyntaxKind.AsteriskAsteriskEqualsToken: + case SyntaxKind.LessThanLessThanEqualsToken: + case SyntaxKind.GreaterThanGreaterThanEqualsToken: + case SyntaxKind.GreaterThanGreaterThanGreaterThanEqualsToken: + case SyntaxKind.AmpersandAmpersandEqualsToken: + case SyntaxKind.BarBarEqualsToken: + case SyntaxKind.QuestionQuestionEqualsToken: + case SyntaxKind.QuestionToken: + return true; + default: + return false; + } + } + function parseDeleteExpression() { const pos = getNodePos(); return finishNode(factory.createDeleteExpression(nextTokenAnd(parseSimpleUnaryExpression)), pos); @@ -5769,6 +5832,8 @@ namespace Parser { return parseTypeOfExpression(); case SyntaxKind.VoidKeyword: return parseVoidExpression(); + case SyntaxKind.ThrowKeyword: + return parseThrowExpression(); case SyntaxKind.LessThanToken: // Just like in parseUpdateExpression, we need to avoid parsing type assertions when // in JSX and we see an expression like "+ bar". @@ -5811,6 +5876,7 @@ namespace Parser { case SyntaxKind.TypeOfKeyword: case SyntaxKind.VoidKeyword: case SyntaxKind.AwaitKeyword: + case SyntaxKind.ThrowKeyword: return false; case SyntaxKind.LessThanToken: // If we are not in JSX context, we are parsing TypeAssertion which is an UnaryExpression diff --git a/src/compiler/transformers/esnext.ts b/src/compiler/transformers/esnext.ts index 04b79d250eaad..eb0eb206c51dd 100644 --- a/src/compiler/transformers/esnext.ts +++ b/src/compiler/transformers/esnext.ts @@ -50,6 +50,7 @@ import { Statement, SwitchStatement, SyntaxKind, + ThrowExpression, TransformationContext, TransformFlags, transformNamedEvaluation, @@ -120,6 +121,9 @@ export function transformESNext(context: TransformationContext): (x: SourceFile case SyntaxKind.SwitchStatement: return visitSwitchStatement(node as SwitchStatement); + case SyntaxKind.ThrowExpression: + return visitThrowExpression(node as ThrowExpression); + default: return visitEachChild(node, visitor, context); } @@ -403,6 +407,12 @@ export function transformESNext(context: TransformationContext): (x: SourceFile return visitEachChild(node, visitor, context); } + function visitThrowExpression(node: ThrowExpression): Expression { + return factory.createImmediatelyInvokedArrowFunction([ + factory.createThrowStatement(node.expression), + ]); + } + /** * Transform `using` declarations in a statement list. */ diff --git a/src/compiler/types.ts b/src/compiler/types.ts index d4860ec7621a8..443b5cbb34dfb 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -286,6 +286,7 @@ export const enum SyntaxKind { ParenthesizedExpression, FunctionExpression, ArrowFunction, + ThrowExpression, DeleteExpression, TypeOfExpression, VoidExpression, @@ -1084,6 +1085,7 @@ export type HasChildren = | DeleteExpression | TypeOfExpression | VoidExpression + | ThrowExpression | AwaitExpression | PrefixUnaryExpression | PostfixUnaryExpression @@ -2453,6 +2455,11 @@ export interface AwaitExpression extends UnaryExpression { readonly expression: UnaryExpression; } +export interface ThrowExpression extends UnaryExpression { + readonly kind: SyntaxKind.ThrowExpression; + readonly expression: UnaryExpression; +} + export interface YieldExpression extends Expression { readonly kind: SyntaxKind.YieldExpression; readonly asteriskToken?: AsteriskToken; @@ -8501,6 +8508,8 @@ export interface NodeFactory { updateFunctionExpression(node: FunctionExpression, modifiers: readonly Modifier[] | undefined, asteriskToken: AsteriskToken | undefined, name: Identifier | undefined, typeParameters: readonly TypeParameterDeclaration[] | undefined, parameters: readonly ParameterDeclaration[], type: TypeNode | undefined, body: Block): FunctionExpression; createArrowFunction(modifiers: readonly Modifier[] | undefined, typeParameters: readonly TypeParameterDeclaration[] | undefined, parameters: readonly ParameterDeclaration[], type: TypeNode | undefined, equalsGreaterThanToken: EqualsGreaterThanToken | undefined, body: ConciseBody): ArrowFunction; updateArrowFunction(node: ArrowFunction, modifiers: readonly Modifier[] | undefined, typeParameters: readonly TypeParameterDeclaration[] | undefined, parameters: readonly ParameterDeclaration[], type: TypeNode | undefined, equalsGreaterThanToken: EqualsGreaterThanToken, body: ConciseBody): ArrowFunction; + createThrowExpression(expression: Expression): ThrowExpression; + updateThrowExpression(node: ThrowExpression, expression: Expression): ThrowExpression; createDeleteExpression(expression: Expression): DeleteExpression; updateDeleteExpression(node: DeleteExpression, expression: Expression): DeleteExpression; createTypeOfExpression(expression: Expression): TypeOfExpression; diff --git a/src/compiler/utilitiesPublic.ts b/src/compiler/utilitiesPublic.ts index 5762af2c2cbcb..fd26b2117b2d7 100644 --- a/src/compiler/utilitiesPublic.ts +++ b/src/compiler/utilitiesPublic.ts @@ -2006,6 +2006,7 @@ function isUnaryExpressionKind(kind: SyntaxKind): boolean { case SyntaxKind.VoidExpression: case SyntaxKind.AwaitExpression: case SyntaxKind.TypeAssertionExpression: + case SyntaxKind.ThrowExpression: return true; default: return isLeftHandSideExpressionKind(kind); diff --git a/src/compiler/visitorPublic.ts b/src/compiler/visitorPublic.ts index c5e99d170911c..b47db230d319e 100644 --- a/src/compiler/visitorPublic.ts +++ b/src/compiler/visitorPublic.ts @@ -1163,6 +1163,13 @@ const visitEachChildTable: VisitEachChildTable = { ); }, + [SyntaxKind.ThrowExpression]: function visitEachChildThrowExpression(node, visitor, context, _nodesVisitor, nodeVisitor, _tokenVisitor) { + return context.factory.updateThrowExpression( + node, + Debug.checkDefined(nodeVisitor(node.expression, visitor, isExpression)), + ); + }, + [SyntaxKind.BinaryExpression]: function visitEachChildOfBinaryExpression(node, visitor, context, _nodesVisitor, nodeVisitor, tokenVisitor) { return context.factory.updateBinaryExpression( node, diff --git a/src/typingsInstallerCore/throwExpression1.errors.txt b/src/typingsInstallerCore/throwExpression1.errors.txt new file mode 100644 index 0000000000000..7cc3132c078cc --- /dev/null +++ b/src/typingsInstallerCore/throwExpression1.errors.txt @@ -0,0 +1,11 @@ +throwExpression1.ts(2,14): error TS1005: ';' expected. + + +==== throwExpression1.ts (1 errors) ==== + function t1(a: any, b: any, c: any) { + (throw a ? b : c); + ~ +!!! error TS1005: ';' expected. + } + + \ No newline at end of file diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index 8f70f7cfa107a..395e45e3d8b38 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -4384,154 +4384,155 @@ declare namespace ts { ParenthesizedExpression = 217, FunctionExpression = 218, ArrowFunction = 219, - DeleteExpression = 220, - TypeOfExpression = 221, - VoidExpression = 222, - AwaitExpression = 223, - PrefixUnaryExpression = 224, - PostfixUnaryExpression = 225, - BinaryExpression = 226, - ConditionalExpression = 227, - TemplateExpression = 228, - YieldExpression = 229, - SpreadElement = 230, - ClassExpression = 231, - OmittedExpression = 232, - ExpressionWithTypeArguments = 233, - AsExpression = 234, - NonNullExpression = 235, - MetaProperty = 236, - SyntheticExpression = 237, - SatisfiesExpression = 238, - TemplateSpan = 239, - SemicolonClassElement = 240, - Block = 241, - EmptyStatement = 242, - VariableStatement = 243, - ExpressionStatement = 244, - IfStatement = 245, - DoStatement = 246, - WhileStatement = 247, - ForStatement = 248, - ForInStatement = 249, - ForOfStatement = 250, - ContinueStatement = 251, - BreakStatement = 252, - ReturnStatement = 253, - WithStatement = 254, - SwitchStatement = 255, - LabeledStatement = 256, - ThrowStatement = 257, - TryStatement = 258, - DebuggerStatement = 259, - VariableDeclaration = 260, - VariableDeclarationList = 261, - FunctionDeclaration = 262, - ClassDeclaration = 263, - InterfaceDeclaration = 264, - TypeAliasDeclaration = 265, - EnumDeclaration = 266, - ModuleDeclaration = 267, - ModuleBlock = 268, - CaseBlock = 269, - NamespaceExportDeclaration = 270, - ImportEqualsDeclaration = 271, - ImportDeclaration = 272, - ImportClause = 273, - NamespaceImport = 274, - NamedImports = 275, - ImportSpecifier = 276, - ExportAssignment = 277, - ExportDeclaration = 278, - NamedExports = 279, - NamespaceExport = 280, - ExportSpecifier = 281, - MissingDeclaration = 282, - ExternalModuleReference = 283, - JsxElement = 284, - JsxSelfClosingElement = 285, - JsxOpeningElement = 286, - JsxClosingElement = 287, - JsxFragment = 288, - JsxOpeningFragment = 289, - JsxClosingFragment = 290, - JsxAttribute = 291, - JsxAttributes = 292, - JsxSpreadAttribute = 293, - JsxExpression = 294, - JsxNamespacedName = 295, - CaseClause = 296, - DefaultClause = 297, - HeritageClause = 298, - CatchClause = 299, - ImportAttributes = 300, - ImportAttribute = 301, - /** @deprecated */ AssertClause = 300, - /** @deprecated */ AssertEntry = 301, - /** @deprecated */ ImportTypeAssertionContainer = 302, - PropertyAssignment = 303, - ShorthandPropertyAssignment = 304, - SpreadAssignment = 305, - EnumMember = 306, - /** @deprecated */ UnparsedPrologue = 307, - /** @deprecated */ UnparsedPrepend = 308, - /** @deprecated */ UnparsedText = 309, - /** @deprecated */ UnparsedInternalText = 310, - /** @deprecated */ UnparsedSyntheticReference = 311, - SourceFile = 312, - Bundle = 313, - /** @deprecated */ UnparsedSource = 314, - /** @deprecated */ InputFiles = 315, - JSDocTypeExpression = 316, - JSDocNameReference = 317, - JSDocMemberName = 318, - JSDocAllType = 319, - JSDocUnknownType = 320, - JSDocNullableType = 321, - JSDocNonNullableType = 322, - JSDocOptionalType = 323, - JSDocFunctionType = 324, - JSDocVariadicType = 325, - JSDocNamepathType = 326, - JSDoc = 327, + ThrowExpression = 220, + DeleteExpression = 221, + TypeOfExpression = 222, + VoidExpression = 223, + AwaitExpression = 224, + PrefixUnaryExpression = 225, + PostfixUnaryExpression = 226, + BinaryExpression = 227, + ConditionalExpression = 228, + TemplateExpression = 229, + YieldExpression = 230, + SpreadElement = 231, + ClassExpression = 232, + OmittedExpression = 233, + ExpressionWithTypeArguments = 234, + AsExpression = 235, + NonNullExpression = 236, + MetaProperty = 237, + SyntheticExpression = 238, + SatisfiesExpression = 239, + TemplateSpan = 240, + SemicolonClassElement = 241, + Block = 242, + EmptyStatement = 243, + VariableStatement = 244, + ExpressionStatement = 245, + IfStatement = 246, + DoStatement = 247, + WhileStatement = 248, + ForStatement = 249, + ForInStatement = 250, + ForOfStatement = 251, + ContinueStatement = 252, + BreakStatement = 253, + ReturnStatement = 254, + WithStatement = 255, + SwitchStatement = 256, + LabeledStatement = 257, + ThrowStatement = 258, + TryStatement = 259, + DebuggerStatement = 260, + VariableDeclaration = 261, + VariableDeclarationList = 262, + FunctionDeclaration = 263, + ClassDeclaration = 264, + InterfaceDeclaration = 265, + TypeAliasDeclaration = 266, + EnumDeclaration = 267, + ModuleDeclaration = 268, + ModuleBlock = 269, + CaseBlock = 270, + NamespaceExportDeclaration = 271, + ImportEqualsDeclaration = 272, + ImportDeclaration = 273, + ImportClause = 274, + NamespaceImport = 275, + NamedImports = 276, + ImportSpecifier = 277, + ExportAssignment = 278, + ExportDeclaration = 279, + NamedExports = 280, + NamespaceExport = 281, + ExportSpecifier = 282, + MissingDeclaration = 283, + ExternalModuleReference = 284, + JsxElement = 285, + JsxSelfClosingElement = 286, + JsxOpeningElement = 287, + JsxClosingElement = 288, + JsxFragment = 289, + JsxOpeningFragment = 290, + JsxClosingFragment = 291, + JsxAttribute = 292, + JsxAttributes = 293, + JsxSpreadAttribute = 294, + JsxExpression = 295, + JsxNamespacedName = 296, + CaseClause = 297, + DefaultClause = 298, + HeritageClause = 299, + CatchClause = 300, + ImportAttributes = 301, + ImportAttribute = 302, + /** @deprecated */ AssertClause = 301, + /** @deprecated */ AssertEntry = 302, + /** @deprecated */ ImportTypeAssertionContainer = 303, + PropertyAssignment = 304, + ShorthandPropertyAssignment = 305, + SpreadAssignment = 306, + EnumMember = 307, + /** @deprecated */ UnparsedPrologue = 308, + /** @deprecated */ UnparsedPrepend = 309, + /** @deprecated */ UnparsedText = 310, + /** @deprecated */ UnparsedInternalText = 311, + /** @deprecated */ UnparsedSyntheticReference = 312, + SourceFile = 313, + Bundle = 314, + /** @deprecated */ UnparsedSource = 315, + /** @deprecated */ InputFiles = 316, + JSDocTypeExpression = 317, + JSDocNameReference = 318, + JSDocMemberName = 319, + JSDocAllType = 320, + JSDocUnknownType = 321, + JSDocNullableType = 322, + JSDocNonNullableType = 323, + JSDocOptionalType = 324, + JSDocFunctionType = 325, + JSDocVariadicType = 326, + JSDocNamepathType = 327, + JSDoc = 328, /** @deprecated Use SyntaxKind.JSDoc */ - JSDocComment = 327, - JSDocText = 328, - JSDocTypeLiteral = 329, - JSDocSignature = 330, - JSDocLink = 331, - JSDocLinkCode = 332, - JSDocLinkPlain = 333, - JSDocTag = 334, - JSDocAugmentsTag = 335, - JSDocImplementsTag = 336, - JSDocAuthorTag = 337, - JSDocDeprecatedTag = 338, - JSDocClassTag = 339, - JSDocPublicTag = 340, - JSDocPrivateTag = 341, - JSDocProtectedTag = 342, - JSDocReadonlyTag = 343, - JSDocOverrideTag = 344, - JSDocCallbackTag = 345, - JSDocOverloadTag = 346, - JSDocEnumTag = 347, - JSDocParameterTag = 348, - JSDocReturnTag = 349, - JSDocThisTag = 350, - JSDocTypeTag = 351, - JSDocTemplateTag = 352, - JSDocTypedefTag = 353, - JSDocSeeTag = 354, - JSDocPropertyTag = 355, - JSDocThrowsTag = 356, - JSDocSatisfiesTag = 357, - SyntaxList = 358, - NotEmittedStatement = 359, - PartiallyEmittedExpression = 360, - CommaListExpression = 361, - SyntheticReferenceExpression = 362, - Count = 363, + JSDocComment = 328, + JSDocText = 329, + JSDocTypeLiteral = 330, + JSDocSignature = 331, + JSDocLink = 332, + JSDocLinkCode = 333, + JSDocLinkPlain = 334, + JSDocTag = 335, + JSDocAugmentsTag = 336, + JSDocImplementsTag = 337, + JSDocAuthorTag = 338, + JSDocDeprecatedTag = 339, + JSDocClassTag = 340, + JSDocPublicTag = 341, + JSDocPrivateTag = 342, + JSDocProtectedTag = 343, + JSDocReadonlyTag = 344, + JSDocOverrideTag = 345, + JSDocCallbackTag = 346, + JSDocOverloadTag = 347, + JSDocEnumTag = 348, + JSDocParameterTag = 349, + JSDocReturnTag = 350, + JSDocThisTag = 351, + JSDocTypeTag = 352, + JSDocTemplateTag = 353, + JSDocTypedefTag = 354, + JSDocSeeTag = 355, + JSDocPropertyTag = 356, + JSDocThrowsTag = 357, + JSDocSatisfiesTag = 358, + SyntaxList = 359, + NotEmittedStatement = 360, + PartiallyEmittedExpression = 361, + CommaListExpression = 362, + SyntheticReferenceExpression = 363, + Count = 364, FirstAssignment = 64, LastAssignment = 79, FirstCompoundAssignment = 65, @@ -4556,13 +4557,13 @@ declare namespace ts { LastTemplateToken = 18, FirstBinaryOperator = 30, LastBinaryOperator = 79, - FirstStatement = 243, - LastStatement = 259, + FirstStatement = 244, + LastStatement = 260, FirstNode = 166, - FirstJSDocNode = 316, - LastJSDocNode = 357, - FirstJSDocTagNode = 334, - LastJSDocTagNode = 357, + FirstJSDocNode = 317, + LastJSDocNode = 358, + FirstJSDocTagNode = 335, + LastJSDocTagNode = 358, } type TriviaSyntaxKind = SyntaxKind.SingleLineCommentTrivia | SyntaxKind.MultiLineCommentTrivia | SyntaxKind.NewLineTrivia | SyntaxKind.WhitespaceTrivia | SyntaxKind.ShebangTrivia | SyntaxKind.ConflictMarkerTrivia; type LiteralSyntaxKind = SyntaxKind.NumericLiteral | SyntaxKind.BigIntLiteral | SyntaxKind.StringLiteral | SyntaxKind.JsxText | SyntaxKind.JsxTextAllWhiteSpaces | SyntaxKind.RegularExpressionLiteral | SyntaxKind.NoSubstitutionTemplateLiteral; @@ -5413,6 +5414,10 @@ declare namespace ts { readonly kind: SyntaxKind.AwaitExpression; readonly expression: UnaryExpression; } + interface ThrowExpression extends UnaryExpression { + readonly kind: SyntaxKind.ThrowExpression; + readonly expression: UnaryExpression; + } interface YieldExpression extends Expression { readonly kind: SyntaxKind.YieldExpression; readonly asteriskToken?: AsteriskToken; @@ -8073,6 +8078,8 @@ declare namespace ts { updateFunctionExpression(node: FunctionExpression, modifiers: readonly Modifier[] | undefined, asteriskToken: AsteriskToken | undefined, name: Identifier | undefined, typeParameters: readonly TypeParameterDeclaration[] | undefined, parameters: readonly ParameterDeclaration[], type: TypeNode | undefined, body: Block): FunctionExpression; createArrowFunction(modifiers: readonly Modifier[] | undefined, typeParameters: readonly TypeParameterDeclaration[] | undefined, parameters: readonly ParameterDeclaration[], type: TypeNode | undefined, equalsGreaterThanToken: EqualsGreaterThanToken | undefined, body: ConciseBody): ArrowFunction; updateArrowFunction(node: ArrowFunction, modifiers: readonly Modifier[] | undefined, typeParameters: readonly TypeParameterDeclaration[] | undefined, parameters: readonly ParameterDeclaration[], type: TypeNode | undefined, equalsGreaterThanToken: EqualsGreaterThanToken, body: ConciseBody): ArrowFunction; + createThrowExpression(expression: Expression): ThrowExpression; + updateThrowExpression(node: ThrowExpression, expression: Expression): ThrowExpression; createDeleteExpression(expression: Expression): DeleteExpression; updateDeleteExpression(node: DeleteExpression, expression: Expression): DeleteExpression; createTypeOfExpression(expression: Expression): TypeOfExpression; diff --git a/tests/baselines/reference/throwExpression1.js b/tests/baselines/reference/throwExpression1.js new file mode 100644 index 0000000000000..0a31d634190a9 --- /dev/null +++ b/tests/baselines/reference/throwExpression1.js @@ -0,0 +1,26 @@ +//// [tests/cases/conformance/throwExpression/throwExpression1.ts] //// + +//// [throwExpression1.ts] +function f(foo = throw new TypeError("Argument required")) {} + +class C { + m(foo = throw new TypeError("Argument required")) {} +} + + +//// [throwExpression1.js] +function f(foo) { + if (foo === void 0) { foo = (function () { + throw new TypeError("Argument required"); + })(); } +} +var C = /** @class */ (function () { + function C() { + } + C.prototype.m = function (foo) { + if (foo === void 0) { foo = (function () { + throw new TypeError("Argument required"); + })(); } + }; + return C; +}()); diff --git a/tests/baselines/reference/throwExpression1.symbols b/tests/baselines/reference/throwExpression1.symbols new file mode 100644 index 0000000000000..046159dc71e69 --- /dev/null +++ b/tests/baselines/reference/throwExpression1.symbols @@ -0,0 +1,17 @@ +//// [tests/cases/conformance/throwExpression/throwExpression1.ts] //// + +=== throwExpression1.ts === +function f(foo = throw new TypeError("Argument required")) {} +>f : Symbol(f, Decl(throwExpression1.ts, 0, 0)) +>foo : Symbol(foo, Decl(throwExpression1.ts, 0, 11)) +>TypeError : Symbol(TypeError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) + +class C { +>C : Symbol(C, Decl(throwExpression1.ts, 0, 61)) + + m(foo = throw new TypeError("Argument required")) {} +>m : Symbol(C.m, Decl(throwExpression1.ts, 2, 9)) +>foo : Symbol(foo, Decl(throwExpression1.ts, 3, 6)) +>TypeError : Symbol(TypeError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +} + diff --git a/tests/baselines/reference/throwExpression1.types b/tests/baselines/reference/throwExpression1.types new file mode 100644 index 0000000000000..bbad2434fafc4 --- /dev/null +++ b/tests/baselines/reference/throwExpression1.types @@ -0,0 +1,21 @@ +//// [tests/cases/conformance/throwExpression/throwExpression1.ts] //// + +=== throwExpression1.ts === +function f(foo = throw new TypeError("Argument required")) {} +>f : (foo?: any) => void +>foo : error +>new TypeError("Argument required") : TypeError +>TypeError : TypeErrorConstructor +>"Argument required" : "Argument required" + +class C { +>C : C + + m(foo = throw new TypeError("Argument required")) {} +>m : (foo?: any) => void +>foo : error +>new TypeError("Argument required") : TypeError +>TypeError : TypeErrorConstructor +>"Argument required" : "Argument required" +} + diff --git a/tests/baselines/reference/throwExpression2.js b/tests/baselines/reference/throwExpression2.js new file mode 100644 index 0000000000000..259e8f6ff2f69 --- /dev/null +++ b/tests/baselines/reference/throwExpression2.js @@ -0,0 +1,23 @@ +//// [tests/cases/conformance/throwExpression/throwExpression2.ts] //// + +//// [throwExpression2.ts] +function t1(value: number | undefined) { + const a = value || throw new Error("Unexpected value"); +} + +function t2(value: number | undefined) { + const a = value && throw new Error("Unexpected value"); +} + + +//// [throwExpression2.js] +function t1(value) { + var a = value || (function () { + throw new Error("Unexpected value"); + })(); +} +function t2(value) { + var a = value && (function () { + throw new Error("Unexpected value"); + })(); +} diff --git a/tests/baselines/reference/throwExpression2.symbols b/tests/baselines/reference/throwExpression2.symbols new file mode 100644 index 0000000000000..6e9afb6529751 --- /dev/null +++ b/tests/baselines/reference/throwExpression2.symbols @@ -0,0 +1,23 @@ +//// [tests/cases/conformance/throwExpression/throwExpression2.ts] //// + +=== throwExpression2.ts === +function t1(value: number | undefined) { +>t1 : Symbol(t1, Decl(throwExpression2.ts, 0, 0)) +>value : Symbol(value, Decl(throwExpression2.ts, 0, 12)) + + const a = value || throw new Error("Unexpected value"); +>a : Symbol(a, Decl(throwExpression2.ts, 1, 9)) +>value : Symbol(value, Decl(throwExpression2.ts, 0, 12)) +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +} + +function t2(value: number | undefined) { +>t2 : Symbol(t2, Decl(throwExpression2.ts, 2, 1)) +>value : Symbol(value, Decl(throwExpression2.ts, 4, 12)) + + const a = value && throw new Error("Unexpected value"); +>a : Symbol(a, Decl(throwExpression2.ts, 5, 9)) +>value : Symbol(value, Decl(throwExpression2.ts, 4, 12)) +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +} + diff --git a/tests/baselines/reference/throwExpression2.types b/tests/baselines/reference/throwExpression2.types new file mode 100644 index 0000000000000..9f0a6b72d08b3 --- /dev/null +++ b/tests/baselines/reference/throwExpression2.types @@ -0,0 +1,29 @@ +//// [tests/cases/conformance/throwExpression/throwExpression2.ts] //// + +=== throwExpression2.ts === +function t1(value: number | undefined) { +>t1 : (value: number | undefined) => void +>value : number + + const a = value || throw new Error("Unexpected value"); +>a : any +>value || throw new Error("Unexpected value") : any +>value : number +>new Error("Unexpected value") : Error +>Error : ErrorConstructor +>"Unexpected value" : "Unexpected value" +} + +function t2(value: number | undefined) { +>t2 : (value: number | undefined) => void +>value : number + + const a = value && throw new Error("Unexpected value"); +>a : any +>value && throw new Error("Unexpected value") : any +>value : number +>new Error("Unexpected value") : Error +>Error : ErrorConstructor +>"Unexpected value" : "Unexpected value" +} + diff --git a/tests/baselines/reference/throwExpression3.js b/tests/baselines/reference/throwExpression3.js new file mode 100644 index 0000000000000..ca68b4e63b9a2 --- /dev/null +++ b/tests/baselines/reference/throwExpression3.js @@ -0,0 +1,32 @@ +//// [tests/cases/conformance/throwExpression/throwExpression3.ts] //// + +//// [throwExpression3.ts] +function t1(foo: number) { + const a = foo === 1 ? 1 : throw new Error("Unexpected value"); +} + +function t2(foo: number) { + const a = foo === 1 ? throw new Error("Unexpected value") : 1; +} + +function t3(foo: number | undefined) { + const a = foo ?? throw new Error("Unexpected value"); +} + + +//// [throwExpression3.js] +function t1(foo) { + var a = foo === 1 ? 1 : (function () { + throw new Error("Unexpected value"); + })(); +} +function t2(foo) { + var a = foo === 1 ? (function () { + throw new Error("Unexpected value"); + })() : 1; +} +function t3(foo) { + var a = foo !== null && foo !== void 0 ? foo : (function () { + throw new Error("Unexpected value"); + })(); +} diff --git a/tests/baselines/reference/throwExpression3.symbols b/tests/baselines/reference/throwExpression3.symbols new file mode 100644 index 0000000000000..a8ea387d9b0ed --- /dev/null +++ b/tests/baselines/reference/throwExpression3.symbols @@ -0,0 +1,33 @@ +//// [tests/cases/conformance/throwExpression/throwExpression3.ts] //// + +=== throwExpression3.ts === +function t1(foo: number) { +>t1 : Symbol(t1, Decl(throwExpression3.ts, 0, 0)) +>foo : Symbol(foo, Decl(throwExpression3.ts, 0, 12)) + + const a = foo === 1 ? 1 : throw new Error("Unexpected value"); +>a : Symbol(a, Decl(throwExpression3.ts, 1, 9)) +>foo : Symbol(foo, Decl(throwExpression3.ts, 0, 12)) +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +} + +function t2(foo: number) { +>t2 : Symbol(t2, Decl(throwExpression3.ts, 2, 1)) +>foo : Symbol(foo, Decl(throwExpression3.ts, 4, 12)) + + const a = foo === 1 ? throw new Error("Unexpected value") : 1; +>a : Symbol(a, Decl(throwExpression3.ts, 5, 9)) +>foo : Symbol(foo, Decl(throwExpression3.ts, 4, 12)) +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +} + +function t3(foo: number | undefined) { +>t3 : Symbol(t3, Decl(throwExpression3.ts, 6, 1)) +>foo : Symbol(foo, Decl(throwExpression3.ts, 8, 12)) + + const a = foo ?? throw new Error("Unexpected value"); +>a : Symbol(a, Decl(throwExpression3.ts, 9, 9)) +>foo : Symbol(foo, Decl(throwExpression3.ts, 8, 12)) +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) +} + diff --git a/tests/baselines/reference/throwExpression3.types b/tests/baselines/reference/throwExpression3.types new file mode 100644 index 0000000000000..ca23e06eebb90 --- /dev/null +++ b/tests/baselines/reference/throwExpression3.types @@ -0,0 +1,48 @@ +//// [tests/cases/conformance/throwExpression/throwExpression3.ts] //// + +=== throwExpression3.ts === +function t1(foo: number) { +>t1 : (foo: number) => void +>foo : number + + const a = foo === 1 ? 1 : throw new Error("Unexpected value"); +>a : any +>foo === 1 ? 1 : throw new Error("Unexpected value") : any +>foo === 1 : boolean +>foo : number +>1 : 1 +>1 : 1 +>new Error("Unexpected value") : Error +>Error : ErrorConstructor +>"Unexpected value" : "Unexpected value" +} + +function t2(foo: number) { +>t2 : (foo: number) => void +>foo : number + + const a = foo === 1 ? throw new Error("Unexpected value") : 1; +>a : any +>foo === 1 ? throw new Error("Unexpected value") : 1 : any +>foo === 1 : boolean +>foo : number +>1 : 1 +>new Error("Unexpected value") : Error +>Error : ErrorConstructor +>"Unexpected value" : "Unexpected value" +>1 : 1 +} + +function t3(foo: number | undefined) { +>t3 : (foo: number | undefined) => void +>foo : number + + const a = foo ?? throw new Error("Unexpected value"); +>a : any +>foo ?? throw new Error("Unexpected value") : any +>foo : number +>new Error("Unexpected value") : Error +>Error : ErrorConstructor +>"Unexpected value" : "Unexpected value" +} + diff --git a/tests/baselines/reference/throwExpression4.js b/tests/baselines/reference/throwExpression4.js new file mode 100644 index 0000000000000..91fbbc8ef525a --- /dev/null +++ b/tests/baselines/reference/throwExpression4.js @@ -0,0 +1,10 @@ +//// [tests/cases/conformance/throwExpression/throwExpression4.ts] //// + +//// [throwExpression4.ts] +() => throw new Error("Unexpected value"); + + +//// [throwExpression4.js] +(function () { return (function () { + throw new Error("Unexpected value"); +})(); }); diff --git a/tests/baselines/reference/throwExpression4.symbols b/tests/baselines/reference/throwExpression4.symbols new file mode 100644 index 0000000000000..a773ef3217db2 --- /dev/null +++ b/tests/baselines/reference/throwExpression4.symbols @@ -0,0 +1,6 @@ +//// [tests/cases/conformance/throwExpression/throwExpression4.ts] //// + +=== throwExpression4.ts === +() => throw new Error("Unexpected value"); +>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) + diff --git a/tests/baselines/reference/throwExpression4.types b/tests/baselines/reference/throwExpression4.types new file mode 100644 index 0000000000000..e3e131acd5543 --- /dev/null +++ b/tests/baselines/reference/throwExpression4.types @@ -0,0 +1,9 @@ +//// [tests/cases/conformance/throwExpression/throwExpression4.ts] //// + +=== throwExpression4.ts === +() => throw new Error("Unexpected value"); +>() => throw new Error("Unexpected value") : () => any +>new Error("Unexpected value") : Error +>Error : ErrorConstructor +>"Unexpected value" : "Unexpected value" + diff --git a/tests/baselines/reference/throwExpression5.errors.txt b/tests/baselines/reference/throwExpression5.errors.txt new file mode 100644 index 0000000000000..78fa138cfaa64 --- /dev/null +++ b/tests/baselines/reference/throwExpression5.errors.txt @@ -0,0 +1,35 @@ +throwExpression5.ts(2,14): error TS1005: ';' expected. +throwExpression5.ts(6,13): error TS1005: ';' expected. +throwExpression5.ts(10,14): error TS1005: ';' expected. +throwExpression5.ts(14,14): error TS1005: ';' expected. + + +==== throwExpression5.ts (4 errors) ==== + function t1(a: number, b: number, c: number) { + (throw a ? b : c); + ~ +!!! error TS1005: ';' expected. + } + + function t2(a: number, b: number) { + (throw a, b); + ~ +!!! error TS1005: ';' expected. + } + + function t3(a: number, b: number) { + (throw a && b); + ~~ +!!! error TS1005: ';' expected. + } + + function t4(a: number, b: number) { + (throw a || b); + ~~ +!!! error TS1005: ';' expected. + } + + function t5(a: number, b: number, c: number) { + (throw (a ? b : c)); + } + \ No newline at end of file diff --git a/tests/baselines/reference/throwExpression5.js b/tests/baselines/reference/throwExpression5.js new file mode 100644 index 0000000000000..79f10b45a4d1d --- /dev/null +++ b/tests/baselines/reference/throwExpression5.js @@ -0,0 +1,50 @@ +//// [tests/cases/conformance/throwExpression/throwExpression5.ts] //// + +//// [throwExpression5.ts] +function t1(a: number, b: number, c: number) { + (throw a ? b : c); +} + +function t2(a: number, b: number) { + (throw a, b); +} + +function t3(a: number, b: number) { + (throw a && b); +} + +function t4(a: number, b: number) { + (throw a || b); +} + +function t5(a: number, b: number, c: number) { + (throw (a ? b : c)); +} + + +//// [throwExpression5.js] +function t1(a, b, c) { + ((function () { + throw a; + })() ? b : c); +} +function t2(a, b) { + ((function () { + throw a; + })(), b); +} +function t3(a, b) { + ((function () { + throw a; + })() && b); +} +function t4(a, b) { + ((function () { + throw a; + })() || b); +} +function t5(a, b, c) { + ((function () { + throw (a ? b : c); + })()); +} diff --git a/tests/baselines/reference/throwExpression5.symbols b/tests/baselines/reference/throwExpression5.symbols new file mode 100644 index 0000000000000..73bd3ed860a3e --- /dev/null +++ b/tests/baselines/reference/throwExpression5.symbols @@ -0,0 +1,53 @@ +//// [tests/cases/conformance/throwExpression/throwExpression5.ts] //// + +=== throwExpression5.ts === +function t1(a: number, b: number, c: number) { +>t1 : Symbol(t1, Decl(throwExpression5.ts, 0, 0)) +>a : Symbol(a, Decl(throwExpression5.ts, 0, 12)) +>b : Symbol(b, Decl(throwExpression5.ts, 0, 22)) +>c : Symbol(c, Decl(throwExpression5.ts, 0, 33)) + + (throw a ? b : c); +>b : Symbol(b, Decl(throwExpression5.ts, 0, 22)) +>c : Symbol(c, Decl(throwExpression5.ts, 0, 33)) +} + +function t2(a: number, b: number) { +>t2 : Symbol(t2, Decl(throwExpression5.ts, 2, 1)) +>a : Symbol(a, Decl(throwExpression5.ts, 4, 12)) +>b : Symbol(b, Decl(throwExpression5.ts, 4, 22)) + + (throw a, b); +>b : Symbol(b, Decl(throwExpression5.ts, 4, 22)) +} + +function t3(a: number, b: number) { +>t3 : Symbol(t3, Decl(throwExpression5.ts, 6, 1)) +>a : Symbol(a, Decl(throwExpression5.ts, 8, 12)) +>b : Symbol(b, Decl(throwExpression5.ts, 8, 22)) + + (throw a && b); +>b : Symbol(b, Decl(throwExpression5.ts, 8, 22)) +} + +function t4(a: number, b: number) { +>t4 : Symbol(t4, Decl(throwExpression5.ts, 10, 1)) +>a : Symbol(a, Decl(throwExpression5.ts, 12, 12)) +>b : Symbol(b, Decl(throwExpression5.ts, 12, 22)) + + (throw a || b); +>b : Symbol(b, Decl(throwExpression5.ts, 12, 22)) +} + +function t5(a: number, b: number, c: number) { +>t5 : Symbol(t5, Decl(throwExpression5.ts, 14, 1)) +>a : Symbol(a, Decl(throwExpression5.ts, 16, 12)) +>b : Symbol(b, Decl(throwExpression5.ts, 16, 22)) +>c : Symbol(c, Decl(throwExpression5.ts, 16, 33)) + + (throw (a ? b : c)); +>a : Symbol(a, Decl(throwExpression5.ts, 16, 12)) +>b : Symbol(b, Decl(throwExpression5.ts, 16, 22)) +>c : Symbol(c, Decl(throwExpression5.ts, 16, 33)) +} + diff --git a/tests/baselines/reference/throwExpression5.types b/tests/baselines/reference/throwExpression5.types new file mode 100644 index 0000000000000..6ff735c5bbdd7 --- /dev/null +++ b/tests/baselines/reference/throwExpression5.types @@ -0,0 +1,68 @@ +//// [tests/cases/conformance/throwExpression/throwExpression5.ts] //// + +=== throwExpression5.ts === +function t1(a: number, b: number, c: number) { +>t1 : (a: number, b: number, c: number) => void +>a : number +>b : number +>c : number + + (throw a ? b : c); +>(throw a ? b : c) : number +>throw a ? b : c : number +>a : any +>b : number +>c : number +} + +function t2(a: number, b: number) { +>t2 : (a: number, b: number) => void +>a : number +>b : number + + (throw a, b); +>(throw a, b) : number +>throw a, b : number +>a : any +>b : number +} + +function t3(a: number, b: number) { +>t3 : (a: number, b: number) => void +>a : number +>b : number + + (throw a && b); +>(throw a && b) : number +>throw a && b : number +>a : any +>b : number +} + +function t4(a: number, b: number) { +>t4 : (a: number, b: number) => void +>a : number +>b : number + + (throw a || b); +>(throw a || b) : any +>throw a || b : any +>a : any +>b : number +} + +function t5(a: number, b: number, c: number) { +>t5 : (a: number, b: number, c: number) => void +>a : number +>b : number +>c : number + + (throw (a ? b : c)); +>(throw (a ? b : c)) : any +>(a ? b : c) : number +>a ? b : c : number +>a : number +>b : number +>c : number +} + diff --git a/tests/cases/conformance/throwExpression/throwExpression1.ts b/tests/cases/conformance/throwExpression/throwExpression1.ts new file mode 100644 index 0000000000000..8b81da5a10f5b --- /dev/null +++ b/tests/cases/conformance/throwExpression/throwExpression1.ts @@ -0,0 +1,5 @@ +function f(foo = throw new TypeError("Argument required")) {} + +class C { + m(foo = throw new TypeError("Argument required")) {} +} diff --git a/tests/cases/conformance/throwExpression/throwExpression2.ts b/tests/cases/conformance/throwExpression/throwExpression2.ts new file mode 100644 index 0000000000000..4b3e176ccc52e --- /dev/null +++ b/tests/cases/conformance/throwExpression/throwExpression2.ts @@ -0,0 +1,7 @@ +function t1(value: number | undefined) { + const a = value || throw new Error("Unexpected value"); +} + +function t2(value: number | undefined) { + const a = value && throw new Error("Unexpected value"); +} diff --git a/tests/cases/conformance/throwExpression/throwExpression3.ts b/tests/cases/conformance/throwExpression/throwExpression3.ts new file mode 100644 index 0000000000000..3691c2c40c555 --- /dev/null +++ b/tests/cases/conformance/throwExpression/throwExpression3.ts @@ -0,0 +1,11 @@ +function t1(foo: number) { + const a = foo === 1 ? 1 : throw new Error("Unexpected value"); +} + +function t2(foo: number) { + const a = foo === 1 ? throw new Error("Unexpected value") : 1; +} + +function t3(foo: number | undefined) { + const a = foo ?? throw new Error("Unexpected value"); +} diff --git a/tests/cases/conformance/throwExpression/throwExpression4.ts b/tests/cases/conformance/throwExpression/throwExpression4.ts new file mode 100644 index 0000000000000..653f77894e7b0 --- /dev/null +++ b/tests/cases/conformance/throwExpression/throwExpression4.ts @@ -0,0 +1 @@ +() => throw new Error("Unexpected value"); diff --git a/tests/cases/conformance/throwExpression/throwExpression5.ts b/tests/cases/conformance/throwExpression/throwExpression5.ts new file mode 100644 index 0000000000000..12853bbb4754a --- /dev/null +++ b/tests/cases/conformance/throwExpression/throwExpression5.ts @@ -0,0 +1,19 @@ +function t1(a: number, b: number, c: number) { + (throw a ? b : c); +} + +function t2(a: number, b: number) { + (throw a, b); +} + +function t3(a: number, b: number) { + (throw a && b); +} + +function t4(a: number, b: number) { + (throw a || b); +} + +function t5(a: number, b: number, c: number) { + (throw (a ? b : c)); +}