Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[Experiment] feat(18535): Support 'throw' expressions #55917

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ import {
TemplateLiteralTypeSpan,
TemplateSpan,
TextRange,
ThrowExpression,
ThrowStatement,
TokenFlags,
tokenToString,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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);
Expand Down
19 changes: 19 additions & 0 deletions src/compiler/factory/nodeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ import {
TextRange,
ThisExpression,
ThisTypeNode,
ThrowExpression,
ThrowStatement,
Token,
TokenFlags,
Expand Down Expand Up @@ -679,6 +680,8 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
updateFunctionExpression,
createArrowFunction,
updateArrowFunction,
createThrowExpression,
updateThrowExpression,
createDeleteExpression,
updateDeleteExpression,
createTypeOfExpression,
Expand Down Expand Up @@ -3317,6 +3320,22 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
: node;
}

// @api
function createThrowExpression(expression: Expression) {
const node = createBaseNode<ThrowExpression>(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<DeleteExpression>(SyntaxKind.DeleteExpression);
Expand Down
66 changes: 66 additions & 0 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ import {
textToKeywordObj,
ThisExpression,
ThisTypeNode,
ThrowExpression,
ThrowStatement,
toArray,
Token,
Expand Down Expand Up @@ -991,6 +992,9 @@ const forEachChildTable: ForEachChildTable = {
return visitNode(cbNode, node.expression) ||
visitNodes(cbNode, cbNodes, node.typeArguments);
},
[SyntaxKind.ThrowExpression]: function forEachChildInThrowExpression<T>(node: ThrowExpression, cbNode: (node: Node) => T | undefined, _cbNodes?: (nodes: NodeArray<Node>) => T | undefined): T | undefined {
return visitNode(cbNode, node.expression);
},
[SyntaxKind.ExternalModuleReference]: function forEachChildInExternalModuleReference<T>(node: ExternalModuleReference, cbNode: (node: Node) => T | undefined, _cbNodes?: (nodes: NodeArray<Node>) => T | undefined): T | undefined {
return visitNode(cbNode, node.expression);
},
Expand Down Expand Up @@ -5505,6 +5509,7 @@ namespace Parser {
token() !== SyntaxKind.SemicolonToken &&
token() !== SyntaxKind.FunctionKeyword &&
token() !== SyntaxKind.ClassKeyword &&
token() !== SyntaxKind.ThrowKeyword &&
isStartOfStatement() &&
!isStartOfExpressionStatement()
) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 "+ <foo> bar".
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/compiler/transformers/esnext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
Statement,
SwitchStatement,
SyntaxKind,
ThrowExpression,
TransformationContext,
TransformFlags,
transformNamedEvaluation,
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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.
*/
Expand Down
9 changes: 9 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ export const enum SyntaxKind {
ParenthesizedExpression,
FunctionExpression,
ArrowFunction,
ThrowExpression,
DeleteExpression,
TypeOfExpression,
VoidExpression,
Expand Down Expand Up @@ -1084,6 +1085,7 @@ export type HasChildren =
| DeleteExpression
| TypeOfExpression
| VoidExpression
| ThrowExpression
| AwaitExpression
| PrefixUnaryExpression
| PostfixUnaryExpression
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/compiler/utilitiesPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions src/compiler/visitorPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions src/typingsInstallerCore/throwExpression1.errors.txt
Original file line number Diff line number Diff line change
@@ -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.
}


Loading