@@ -24186,7 +24186,7 @@ namespace ts {
24186
24186
// To avoid that we will give an error to users if they use arguments objects in arrow function so that they
24187
24187
// can explicitly bound arguments objects
24188
24188
if (symbol === argumentsSymbol) {
24189
- if (isInPropertyInitializer (node)) {
24189
+ if (isInPropertyInitializerOrClassStaticBlock (node)) {
24190
24190
error(node, Diagnostics.arguments_cannot_be_referenced_in_property_initializers);
24191
24191
return errorType;
24192
24192
}
@@ -24544,6 +24544,9 @@ namespace ts {
24544
24544
// do not return here so in case if lexical this is captured - it will be reflected in flags on NodeLinks
24545
24545
}
24546
24546
break;
24547
+ case SyntaxKind.ClassStaticBlockDeclaration:
24548
+ error(node, Diagnostics.this_cannot_be_referenced_in_current_location);
24549
+ break;
24547
24550
case SyntaxKind.ComputedPropertyName:
24548
24551
error(node, Diagnostics.this_cannot_be_referenced_in_a_computed_property_name);
24549
24552
break;
@@ -24602,7 +24605,8 @@ namespace ts {
24602
24605
24603
24606
if (isClassLike(container.parent)) {
24604
24607
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!;
24606
24610
return getFlowTypeOfReference(node, type);
24607
24611
}
24608
24612
@@ -27565,7 +27569,7 @@ namespace ts {
27565
27569
27566
27570
let diagnosticMessage;
27567
27571
const declarationName = idText(right);
27568
- if (isInPropertyInitializer (node)
27572
+ if (isInPropertyInitializerOrClassStaticBlock (node)
27569
27573
&& !isOptionalPropertyDeclaration(valueDeclaration)
27570
27574
&& !(isAccessExpression(node) && isAccessExpression(node.expression))
27571
27575
&& !isBlockScopedNameDeclaredBeforeUse(valueDeclaration, right)
@@ -27586,7 +27590,7 @@ namespace ts {
27586
27590
}
27587
27591
}
27588
27592
27589
- function isInPropertyInitializer (node: Node): boolean {
27593
+ function isInPropertyInitializerOrClassStaticBlock (node: Node): boolean {
27590
27594
return !!findAncestor(node, node => {
27591
27595
switch (node.kind) {
27592
27596
case SyntaxKind.PropertyDeclaration:
@@ -27606,6 +27610,8 @@ namespace ts {
27606
27610
case SyntaxKind.ExpressionWithTypeArguments:
27607
27611
case SyntaxKind.HeritageClause:
27608
27612
return false;
27613
+ case SyntaxKind.ExpressionStatement:
27614
+ return isBlock(node.parent) && isClassStaticBlockDeclaration(node.parent.parent) ? true : "quit";
27609
27615
default:
27610
27616
return isExpressionNode(node) ? false : "quit";
27611
27617
}
@@ -31303,7 +31309,11 @@ namespace ts {
31303
31309
function checkAwaitExpression(node: AwaitExpression): Type {
31304
31310
// Grammar checking
31305
31311
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)) {
31307
31317
if (isInTopLevelContext(node)) {
31308
31318
const sourceFile = getSourceFileOfNode(node);
31309
31319
if (!hasParseDiagnostics(sourceFile)) {
@@ -31328,9 +31338,8 @@ namespace ts {
31328
31338
if (!hasParseDiagnostics(sourceFile)) {
31329
31339
const span = getSpanOfTokenAtPosition(sourceFile, node.pos);
31330
31340
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);
31334
31343
addRelatedInfo(diagnostic, relatedInfo);
31335
31344
}
31336
31345
diagnostics.add(diagnostic);
@@ -33489,6 +33498,12 @@ namespace ts {
33489
33498
}
33490
33499
}
33491
33500
33501
+ function checkClassStaticBlockDeclaration(node: ClassStaticBlockDeclaration) {
33502
+ checkGrammarDecoratorsAndModifiers(node);
33503
+
33504
+ forEachChild(node, checkSourceElement);
33505
+ }
33506
+
33492
33507
function checkConstructorDeclaration(node: ConstructorDeclaration) {
33493
33508
// Grammar check on signature of constructor and modifier of the constructor is done in checkSignatureDeclaration function.
33494
33509
checkSignatureDeclaration(node);
@@ -35096,10 +35111,11 @@ namespace ts {
35096
35111
break;
35097
35112
case SyntaxKind.IndexSignature:
35098
35113
case SyntaxKind.SemicolonClassElement:
35114
+ case SyntaxKind.ClassStaticBlockDeclaration:
35099
35115
// Can't be private
35100
35116
break;
35101
35117
default:
35102
- Debug.fail();
35118
+ Debug.fail("Unexpected class member" );
35103
35119
}
35104
35120
}
35105
35121
}
@@ -35531,6 +35547,7 @@ namespace ts {
35531
35547
if (!node.name) {
35532
35548
return;
35533
35549
}
35550
+
35534
35551
// For a computed property, just check the initializer and exit
35535
35552
// Do not use hasDynamicName here, because that returns false for well known symbols.
35536
35553
// We want to perform checkComputedPropertyName for all computed properties, including
@@ -35907,11 +35924,17 @@ namespace ts {
35907
35924
function checkForOfStatement(node: ForOfStatement): void {
35908
35925
checkGrammarForInOrForOfStatement(node);
35909
35926
35927
+ const container = getContainingFunctionOrClassStaticBlock(node);
35910
35928
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
+ }
35915
35938
}
35916
35939
}
35917
35940
else if (compilerOptions.downlevelIteration && languageVersion < ScriptTarget.ES2015) {
@@ -36791,28 +36814,33 @@ namespace ts {
36791
36814
return;
36792
36815
}
36793
36816
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) {
36796
36824
grammarErrorOnFirstToken(node, Diagnostics.A_return_statement_can_only_be_used_within_a_function_body);
36797
36825
return;
36798
36826
}
36799
36827
36800
- const signature = getSignatureFromDeclaration(func );
36828
+ const signature = getSignatureFromDeclaration(container );
36801
36829
const returnType = getReturnTypeOfSignature(signature);
36802
- const functionFlags = getFunctionFlags(func );
36830
+ const functionFlags = getFunctionFlags(container );
36803
36831
if (strictNullChecks || node.expression || returnType.flags & TypeFlags.Never) {
36804
36832
const exprType = node.expression ? checkExpressionCached(node.expression) : undefinedType;
36805
- if (func .kind === SyntaxKind.SetAccessor) {
36833
+ if (container .kind === SyntaxKind.SetAccessor) {
36806
36834
if (node.expression) {
36807
36835
error(node, Diagnostics.Setters_cannot_return_a_value);
36808
36836
}
36809
36837
}
36810
- else if (func .kind === SyntaxKind.Constructor) {
36838
+ else if (container .kind === SyntaxKind.Constructor) {
36811
36839
if (node.expression && !checkTypeAssignableToAndOptionallyElaborate(exprType, returnType, node, node.expression)) {
36812
36840
error(node, Diagnostics.Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class);
36813
36841
}
36814
36842
}
36815
- else if (getReturnTypeFromAnnotation(func )) {
36843
+ else if (getReturnTypeFromAnnotation(container )) {
36816
36844
const unwrappedReturnType = unwrapReturnType(returnType, functionFlags) ?? returnType;
36817
36845
const unwrappedExprType = functionFlags & FunctionFlags.Async
36818
36846
? 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 {
36825
36853
}
36826
36854
}
36827
36855
}
36828
- else if (func .kind !== SyntaxKind.Constructor && compilerOptions.noImplicitReturns && !isUnwrappedReturnTypeVoidOrAny(func , returnType)) {
36856
+ else if (container .kind !== SyntaxKind.Constructor && compilerOptions.noImplicitReturns && !isUnwrappedReturnTypeVoidOrAny(container , returnType)) {
36829
36857
// The function has a return type, but the return statement doesn't have an expression.
36830
36858
error(node, Diagnostics.Not_all_code_paths_return_a_value);
36831
36859
}
@@ -38655,6 +38683,8 @@ namespace ts {
38655
38683
case SyntaxKind.MethodDeclaration:
38656
38684
case SyntaxKind.MethodSignature:
38657
38685
return checkMethodDeclaration(node as MethodDeclaration | MethodSignature);
38686
+ case SyntaxKind.ClassStaticBlockDeclaration:
38687
+ return checkClassStaticBlockDeclaration(node as ClassStaticBlockDeclaration);
38658
38688
case SyntaxKind.Constructor:
38659
38689
return checkConstructorDeclaration(node as ConstructorDeclaration);
38660
38690
case SyntaxKind.GetAccessor:
@@ -41132,6 +41162,7 @@ namespace ts {
41132
41162
case SyntaxKind.InterfaceDeclaration:
41133
41163
case SyntaxKind.VariableStatement:
41134
41164
case SyntaxKind.TypeAliasDeclaration:
41165
+ case SyntaxKind.ClassStaticBlockDeclaration:
41135
41166
return true;
41136
41167
case SyntaxKind.EnumDeclaration:
41137
41168
return nodeHasAnyModifiersExcept(node, SyntaxKind.ConstKeyword);
@@ -41869,7 +41900,7 @@ namespace ts {
41869
41900
function checkGrammarBreakOrContinueStatement(node: BreakOrContinueStatement): boolean {
41870
41901
let current: Node = node;
41871
41902
while (current) {
41872
- if (isFunctionLike (current)) {
41903
+ if (isFunctionLikeOrClassStaticBlockDeclaration (current)) {
41873
41904
return grammarErrorOnNode(node, Diagnostics.Jump_target_cannot_cross_function_boundary);
41874
41905
}
41875
41906
0 commit comments