@@ -346,6 +346,7 @@ namespace ts {
346346 const compilerOptions = host.getCompilerOptions();
347347 const languageVersion = getEmitScriptTarget(compilerOptions);
348348 const moduleKind = getEmitModuleKind(compilerOptions);
349+ const useDefineForClassFields = getUseDefineForClassFields(compilerOptions);
349350 const allowSyntheticDefaultImports = getAllowSyntheticDefaultImports(compilerOptions);
350351 const strictNullChecks = getStrictOptionValue(compilerOptions, "strictNullChecks");
351352 const strictFunctionTypes = getStrictOptionValue(compilerOptions, "strictFunctionTypes");
@@ -1501,7 +1502,7 @@ namespace ts {
15011502 }
15021503 else if (isParameterPropertyDeclaration(declaration, declaration.parent)) {
15031504 // foo = this.bar is illegal in esnext+useDefineForClassFields when bar is a parameter property
1504- return !(compilerOptions.target === ScriptTarget.ESNext && !!compilerOptions. useDefineForClassFields
1505+ return !(compilerOptions.target === ScriptTarget.ESNext && useDefineForClassFields
15051506 && getContainingClass(declaration) === getContainingClass(usage)
15061507 && isUsedInFunctionOrInstanceProperty(usage, declaration));
15071508 }
@@ -1532,7 +1533,7 @@ namespace ts {
15321533 return true;
15331534 }
15341535 if (isUsedInFunctionOrInstanceProperty(usage, declaration)) {
1535- if (compilerOptions.target === ScriptTarget.ESNext && !!compilerOptions. useDefineForClassFields
1536+ if (compilerOptions.target === ScriptTarget.ESNext && useDefineForClassFields
15361537 && getContainingClass(declaration)
15371538 && (isPropertyDeclaration(declaration) || isParameterPropertyDeclaration(declaration, declaration.parent))) {
15381539 return !isPropertyImmediatelyReferencedWithinDeclaration(declaration, usage, /*stopAtAnyPropertyDeclaration*/ true);
@@ -1680,7 +1681,7 @@ namespace ts {
16801681 case SyntaxKind.PropertyDeclaration:
16811682 // static properties in classes introduce temporary variables
16821683 if (hasStaticModifier(node)) {
1683- return target < ScriptTarget.ESNext || !compilerOptions. useDefineForClassFields;
1684+ return target < ScriptTarget.ESNext || !useDefineForClassFields;
16841685 }
16851686 return requiresScopeChangeWorker((node as PropertyDeclaration).name);
16861687 default:
@@ -2098,7 +2099,7 @@ namespace ts {
20982099
20992100 // Perform extra checks only if error reporting was requested
21002101 if (nameNotFoundMessage) {
2101- if (propertyWithInvalidInitializer && !(compilerOptions.target === ScriptTarget.ESNext && compilerOptions. useDefineForClassFields)) {
2102+ if (propertyWithInvalidInitializer && !(compilerOptions.target === ScriptTarget.ESNext && useDefineForClassFields)) {
21022103 // We have a match, but the reference occurred within a property initializer and the identifier also binds
21032104 // to a local variable in the constructor where the code will be emitted. Note that this is actually allowed
21042105 // with ESNext+useDefineForClassFields because the scope semantics are different.
@@ -24260,7 +24261,7 @@ namespace ts {
2426024261 break;
2426124262 case SyntaxKind.PropertyDeclaration:
2426224263 case SyntaxKind.PropertySignature:
24263- if (hasSyntacticModifier(container, ModifierFlags.Static) && !(compilerOptions.target === ScriptTarget.ESNext && compilerOptions. useDefineForClassFields)) {
24264+ if (hasSyntacticModifier(container, ModifierFlags.Static) && !(compilerOptions.target === ScriptTarget.ESNext && useDefineForClassFields)) {
2426424265 error(node, Diagnostics.this_cannot_be_referenced_in_a_static_property_initializer);
2426524266 // do not return here so in case if lexical this is captured - it will be reflected in flags on NodeLinks
2426624267 }
@@ -27057,6 +27058,30 @@ namespace ts {
2705727058 if (assignmentKind && lexicallyScopedSymbol && lexicallyScopedSymbol.valueDeclaration && isMethodDeclaration(lexicallyScopedSymbol.valueDeclaration)) {
2705827059 grammarErrorOnNode(right, Diagnostics.Cannot_assign_to_private_method_0_Private_methods_are_not_writable, idText(right));
2705927060 }
27061+
27062+ if (lexicallyScopedSymbol?.valueDeclaration && (compilerOptions.target === ScriptTarget.ESNext && !useDefineForClassFields)) {
27063+ const lexicalClass = getContainingClass(lexicallyScopedSymbol.valueDeclaration);
27064+ const parentStaticFieldInitializer = findAncestor(node, (n) => {
27065+ if (n === lexicalClass) return "quit";
27066+ if (isPropertyDeclaration(n.parent) && hasStaticModifier(n.parent) && n.parent.initializer === n && n.parent.parent === lexicalClass) {
27067+ return true;
27068+ }
27069+ return false;
27070+ });
27071+ if (parentStaticFieldInitializer) {
27072+ const parentStaticFieldInitializerSymbol = getSymbolOfNode(parentStaticFieldInitializer.parent);
27073+ Debug.assert(parentStaticFieldInitializerSymbol, "Initializer without declaration symbol");
27074+ const diagnostic = error(node,
27075+ Diagnostics.Property_0_may_not_be_used_in_a_static_property_s_initializer_in_the_same_class_when_target_is_esnext_and_useDefineForClassFields_is_false,
27076+ symbolName(lexicallyScopedSymbol));
27077+ addRelatedInfo(diagnostic,
27078+ createDiagnosticForNode(parentStaticFieldInitializer.parent,
27079+ Diagnostics.Initializer_for_property_0,
27080+ symbolName(parentStaticFieldInitializerSymbol))
27081+ );
27082+ }
27083+ }
27084+
2706027085 if (isAnyLike) {
2706127086 if (lexicallyScopedSymbol) {
2706227087 return apparentType;
@@ -33134,7 +33159,7 @@ namespace ts {
3313433159 // - The constructor declares parameter properties
3313533160 // or the containing class declares instance member variables with initializers.
3313633161 const superCallShouldBeFirst =
33137- (compilerOptions.target !== ScriptTarget.ESNext || !compilerOptions. useDefineForClassFields) &&
33162+ (compilerOptions.target !== ScriptTarget.ESNext || !useDefineForClassFields) &&
3313833163 (some((<ClassDeclaration>node.parent).members, isInstancePropertyWithInitializerOrPrivateIdentifierProperty) ||
3313933164 some(node.parameters, p => hasSyntacticModifier(p, ModifierFlags.ParameterPropertyModifier)));
3314033165
@@ -37142,7 +37167,7 @@ namespace ts {
3714237167 Diagnostics._0_is_defined_as_a_property_in_class_1_but_is_overridden_here_in_2_as_an_accessor;
3714337168 error(getNameOfDeclaration(derived.valueDeclaration) || derived.valueDeclaration, errorMessage, symbolToString(base), typeToString(baseType), typeToString(type));
3714437169 }
37145- else if (compilerOptions. useDefineForClassFields) {
37170+ else if (useDefineForClassFields) {
3714637171 const uninitialized = derived.declarations?.find(d => d.kind === SyntaxKind.PropertyDeclaration && !(d as PropertyDeclaration).initializer);
3714737172 if (uninitialized
3714837173 && !(derived.flags & SymbolFlags.Transient)
0 commit comments