Skip to content

Commit ac3dc0c

Browse files
authored
fix(37287): check appropriate context with enabled useDefineForClassFields (#37323)
1 parent 933c294 commit ac3dc0c

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1412,7 +1412,7 @@ namespace ts {
14121412
return true;
14131413
}
14141414
if (isUsedInFunctionOrInstanceProperty(usage, declaration, container)) {
1415-
if (compilerOptions.target === ScriptTarget.ESNext && !!compilerOptions.useDefineForClassFields) {
1415+
if (compilerOptions.target === ScriptTarget.ESNext && !!compilerOptions.useDefineForClassFields && getContainingClass(declaration)) {
14161416
return (isPropertyDeclaration(declaration) || isParameterPropertyDeclaration(declaration, declaration.parent)) &&
14171417
!isPropertyImmediatelyReferencedWithinDeclaration(declaration, usage, /*stopAtAnyPropertyDeclaration*/ true);
14181418
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//// [defineVariables_useDefineForClassFields.ts]
2+
const a = () => b()
3+
const b = () => null
4+
a()
5+
6+
//// [defineVariables_useDefineForClassFields.js]
7+
const a = () => b();
8+
const b = () => null;
9+
a();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== tests/cases/compiler/defineVariables_useDefineForClassFields.ts ===
2+
const a = () => b()
3+
>a : Symbol(a, Decl(defineVariables_useDefineForClassFields.ts, 0, 5))
4+
>b : Symbol(b, Decl(defineVariables_useDefineForClassFields.ts, 1, 5))
5+
6+
const b = () => null
7+
>b : Symbol(b, Decl(defineVariables_useDefineForClassFields.ts, 1, 5))
8+
9+
a()
10+
>a : Symbol(a, Decl(defineVariables_useDefineForClassFields.ts, 0, 5))
11+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/compiler/defineVariables_useDefineForClassFields.ts ===
2+
const a = () => b()
3+
>a : () => any
4+
>() => b() : () => any
5+
>b() : any
6+
>b : () => any
7+
8+
const b = () => null
9+
>b : () => any
10+
>() => null : () => any
11+
>null : null
12+
13+
a()
14+
>a() : any
15+
>a : () => any
16+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @target: ESNext
2+
// @useDefineForClassFields: true
3+
4+
const a = () => b()
5+
const b = () => null
6+
a()

0 commit comments

Comments
 (0)