@@ -24186,7 +24186,7 @@ namespace ts {
2418624186 // To avoid that we will give an error to users if they use arguments objects in arrow function so that they
2418724187 // can explicitly bound arguments objects
2418824188 if (symbol === argumentsSymbol) {
24189- if (isInPropertyInitializer (node)) {
24189+ if (isInPropertyInitializerOrClassStaticBlock (node)) {
2419024190 error(node, Diagnostics.arguments_cannot_be_referenced_in_property_initializers);
2419124191 return errorType;
2419224192 }
@@ -24544,6 +24544,9 @@ namespace ts {
2454424544 // do not return here so in case if lexical this is captured - it will be reflected in flags on NodeLinks
2454524545 }
2454624546 break;
24547+ case SyntaxKind.ClassStaticBlockDeclaration:
24548+ error(node, Diagnostics.this_cannot_be_referenced_in_current_location);
24549+ break;
2454724550 case SyntaxKind.ComputedPropertyName:
2454824551 error(node, Diagnostics.this_cannot_be_referenced_in_a_computed_property_name);
2454924552 break;
@@ -24602,7 +24605,8 @@ namespace ts {
2460224605
2460324606 if (isClassLike(container.parent)) {
2460424607 const symbol = getSymbolOfNode(container.parent);
24605- const type = hasSyntacticModifier(container, ModifierFlags.Static) ? getTypeOfSymbol(symbol) : (getDeclaredTypeOfSymbol(symbol) as InterfaceType).thisType!;
24608+ const isStatic = hasSyntacticModifier(container, ModifierFlags.Static) || isClassStaticBlockDeclaration(container);
24609+ const type = isStatic ? getTypeOfSymbol(symbol) : (getDeclaredTypeOfSymbol(symbol) as InterfaceType).thisType!;
2460624610 return getFlowTypeOfReference(node, type);
2460724611 }
2460824612
@@ -27565,7 +27569,7 @@ namespace ts {
2756527569
2756627570 let diagnosticMessage;
2756727571 const declarationName = idText(right);
27568- if (isInPropertyInitializer (node)
27572+ if (isInPropertyInitializerOrClassStaticBlock (node)
2756927573 && !isOptionalPropertyDeclaration(valueDeclaration)
2757027574 && !(isAccessExpression(node) && isAccessExpression(node.expression))
2757127575 && !isBlockScopedNameDeclaredBeforeUse(valueDeclaration, right)
@@ -27586,7 +27590,7 @@ namespace ts {
2758627590 }
2758727591 }
2758827592
27589- function isInPropertyInitializer (node: Node): boolean {
27593+ function isInPropertyInitializerOrClassStaticBlock (node: Node): boolean {
2759027594 return !!findAncestor(node, node => {
2759127595 switch (node.kind) {
2759227596 case SyntaxKind.PropertyDeclaration:
@@ -27606,6 +27610,8 @@ namespace ts {
2760627610 case SyntaxKind.ExpressionWithTypeArguments:
2760727611 case SyntaxKind.HeritageClause:
2760827612 return false;
27613+ case SyntaxKind.ExpressionStatement:
27614+ return isBlock(node.parent) && isClassStaticBlockDeclaration(node.parent.parent) ? true : "quit";
2760927615 default:
2761027616 return isExpressionNode(node) ? false : "quit";
2761127617 }
@@ -31303,7 +31309,11 @@ namespace ts {
3130331309 function checkAwaitExpression(node: AwaitExpression): Type {
3130431310 // Grammar checking
3130531311 if (produceDiagnostics) {
31306- if (!(node.flags & NodeFlags.AwaitContext)) {
31312+ const container = getContainingFunctionOrClassStaticBlock(node);
31313+ if (container && isClassStaticBlockDeclaration(container)) {
31314+ error(node, Diagnostics.Await_expression_cannot_be_used_inside_a_class_static_block);
31315+ }
31316+ else if (!(node.flags & NodeFlags.AwaitContext)) {
3130731317 if (isInTopLevelContext(node)) {
3130831318 const sourceFile = getSourceFileOfNode(node);
3130931319 if (!hasParseDiagnostics(sourceFile)) {
@@ -31328,9 +31338,8 @@ namespace ts {
3132831338 if (!hasParseDiagnostics(sourceFile)) {
3132931339 const span = getSpanOfTokenAtPosition(sourceFile, node.pos);
3133031340 const diagnostic = createFileDiagnostic(sourceFile, span.start, span.length, Diagnostics.await_expressions_are_only_allowed_within_async_functions_and_at_the_top_levels_of_modules);
31331- const func = getContainingFunction(node);
31332- if (func && func.kind !== SyntaxKind.Constructor && (getFunctionFlags(func) & FunctionFlags.Async) === 0) {
31333- const relatedInfo = createDiagnosticForNode(func, Diagnostics.Did_you_mean_to_mark_this_function_as_async);
31341+ if (container && container.kind !== SyntaxKind.Constructor && (getFunctionFlags(container) & FunctionFlags.Async) === 0) {
31342+ const relatedInfo = createDiagnosticForNode(container, Diagnostics.Did_you_mean_to_mark_this_function_as_async);
3133431343 addRelatedInfo(diagnostic, relatedInfo);
3133531344 }
3133631345 diagnostics.add(diagnostic);
@@ -33489,6 +33498,12 @@ namespace ts {
3348933498 }
3349033499 }
3349133500
33501+ function checkClassStaticBlockDeclaration(node: ClassStaticBlockDeclaration) {
33502+ checkGrammarDecoratorsAndModifiers(node);
33503+
33504+ forEachChild(node, checkSourceElement);
33505+ }
33506+
3349233507 function checkConstructorDeclaration(node: ConstructorDeclaration) {
3349333508 // Grammar check on signature of constructor and modifier of the constructor is done in checkSignatureDeclaration function.
3349433509 checkSignatureDeclaration(node);
@@ -35096,10 +35111,11 @@ namespace ts {
3509635111 break;
3509735112 case SyntaxKind.IndexSignature:
3509835113 case SyntaxKind.SemicolonClassElement:
35114+ case SyntaxKind.ClassStaticBlockDeclaration:
3509935115 // Can't be private
3510035116 break;
3510135117 default:
35102- Debug.fail();
35118+ Debug.fail("Unexpected class member" );
3510335119 }
3510435120 }
3510535121 }
@@ -35531,6 +35547,7 @@ namespace ts {
3553135547 if (!node.name) {
3553235548 return;
3553335549 }
35550+
3553435551 // For a computed property, just check the initializer and exit
3553535552 // Do not use hasDynamicName here, because that returns false for well known symbols.
3553635553 // We want to perform checkComputedPropertyName for all computed properties, including
@@ -35907,11 +35924,17 @@ namespace ts {
3590735924 function checkForOfStatement(node: ForOfStatement): void {
3590835925 checkGrammarForInOrForOfStatement(node);
3590935926
35927+ const container = getContainingFunctionOrClassStaticBlock(node);
3591035928 if (node.awaitModifier) {
35911- const functionFlags = getFunctionFlags(getContainingFunction(node));
35912- if ((functionFlags & (FunctionFlags.Invalid | FunctionFlags.Async)) === FunctionFlags.Async && languageVersion < ScriptTarget.ESNext) {
35913- // for..await..of in an async function or async generator function prior to ESNext requires the __asyncValues helper
35914- checkExternalEmitHelpers(node, ExternalEmitHelpers.ForAwaitOfIncludes);
35929+ if (container && isClassStaticBlockDeclaration(container)) {
35930+ grammarErrorOnNode(node.awaitModifier, Diagnostics.For_await_loops_cannot_be_used_inside_a_class_static_block);
35931+ }
35932+ else {
35933+ const functionFlags = getFunctionFlags(container);
35934+ if ((functionFlags & (FunctionFlags.Invalid | FunctionFlags.Async)) === FunctionFlags.Async && languageVersion < ScriptTarget.ESNext) {
35935+ // for..await..of in an async function or async generator function prior to ESNext requires the __asyncValues helper
35936+ checkExternalEmitHelpers(node, ExternalEmitHelpers.ForAwaitOfIncludes);
35937+ }
3591535938 }
3591635939 }
3591735940 else if (compilerOptions.downlevelIteration && languageVersion < ScriptTarget.ES2015) {
@@ -36791,28 +36814,33 @@ namespace ts {
3679136814 return;
3679236815 }
3679336816
36794- const func = getContainingFunction(node);
36795- if (!func) {
36817+ const container = getContainingFunctionOrClassStaticBlock(node);
36818+ if(container && isClassStaticBlockDeclaration(container)) {
36819+ grammarErrorOnFirstToken(node, Diagnostics.A_return_statement_cannot_be_used_inside_a_class_static_block);
36820+ return;
36821+ }
36822+
36823+ if (!container) {
3679636824 grammarErrorOnFirstToken(node, Diagnostics.A_return_statement_can_only_be_used_within_a_function_body);
3679736825 return;
3679836826 }
3679936827
36800- const signature = getSignatureFromDeclaration(func );
36828+ const signature = getSignatureFromDeclaration(container );
3680136829 const returnType = getReturnTypeOfSignature(signature);
36802- const functionFlags = getFunctionFlags(func );
36830+ const functionFlags = getFunctionFlags(container );
3680336831 if (strictNullChecks || node.expression || returnType.flags & TypeFlags.Never) {
3680436832 const exprType = node.expression ? checkExpressionCached(node.expression) : undefinedType;
36805- if (func .kind === SyntaxKind.SetAccessor) {
36833+ if (container .kind === SyntaxKind.SetAccessor) {
3680636834 if (node.expression) {
3680736835 error(node, Diagnostics.Setters_cannot_return_a_value);
3680836836 }
3680936837 }
36810- else if (func .kind === SyntaxKind.Constructor) {
36838+ else if (container .kind === SyntaxKind.Constructor) {
3681136839 if (node.expression && !checkTypeAssignableToAndOptionallyElaborate(exprType, returnType, node, node.expression)) {
3681236840 error(node, Diagnostics.Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class);
3681336841 }
3681436842 }
36815- else if (getReturnTypeFromAnnotation(func )) {
36843+ else if (getReturnTypeFromAnnotation(container )) {
3681636844 const unwrappedReturnType = unwrapReturnType(returnType, functionFlags) ?? returnType;
3681736845 const unwrappedExprType = functionFlags & FunctionFlags.Async
3681836846 ? checkAwaitedType(exprType, node, Diagnostics.The_return_type_of_an_async_function_must_either_be_a_valid_promise_or_must_not_contain_a_callable_then_member)
@@ -36825,7 +36853,7 @@ namespace ts {
3682536853 }
3682636854 }
3682736855 }
36828- else if (func .kind !== SyntaxKind.Constructor && compilerOptions.noImplicitReturns && !isUnwrappedReturnTypeVoidOrAny(func , returnType)) {
36856+ else if (container .kind !== SyntaxKind.Constructor && compilerOptions.noImplicitReturns && !isUnwrappedReturnTypeVoidOrAny(container , returnType)) {
3682936857 // The function has a return type, but the return statement doesn't have an expression.
3683036858 error(node, Diagnostics.Not_all_code_paths_return_a_value);
3683136859 }
@@ -38655,6 +38683,8 @@ namespace ts {
3865538683 case SyntaxKind.MethodDeclaration:
3865638684 case SyntaxKind.MethodSignature:
3865738685 return checkMethodDeclaration(node as MethodDeclaration | MethodSignature);
38686+ case SyntaxKind.ClassStaticBlockDeclaration:
38687+ return checkClassStaticBlockDeclaration(node as ClassStaticBlockDeclaration);
3865838688 case SyntaxKind.Constructor:
3865938689 return checkConstructorDeclaration(node as ConstructorDeclaration);
3866038690 case SyntaxKind.GetAccessor:
@@ -41132,6 +41162,7 @@ namespace ts {
4113241162 case SyntaxKind.InterfaceDeclaration:
4113341163 case SyntaxKind.VariableStatement:
4113441164 case SyntaxKind.TypeAliasDeclaration:
41165+ case SyntaxKind.ClassStaticBlockDeclaration:
4113541166 return true;
4113641167 case SyntaxKind.EnumDeclaration:
4113741168 return nodeHasAnyModifiersExcept(node, SyntaxKind.ConstKeyword);
@@ -41869,7 +41900,7 @@ namespace ts {
4186941900 function checkGrammarBreakOrContinueStatement(node: BreakOrContinueStatement): boolean {
4187041901 let current: Node = node;
4187141902 while (current) {
41872- if (isFunctionLike (current)) {
41903+ if (isFunctionLikeOrClassStaticBlockDeclaration (current)) {
4187341904 return grammarErrorOnNode(node, Diagnostics.Jump_target_cannot_cross_function_boundary);
4187441905 }
4187541906
0 commit comments