fix(37287): check appropriate context with enabled useDefineForClassFields (#37323)

This commit is contained in:
Alexander T
2020-03-19 02:03:07 +02:00
committed by GitHub
parent 933c294923
commit ac3dc0c4d4
5 changed files with 43 additions and 1 deletions

View File

@@ -1412,7 +1412,7 @@ namespace ts {
return true;
}
if (isUsedInFunctionOrInstanceProperty(usage, declaration, container)) {
if (compilerOptions.target === ScriptTarget.ESNext && !!compilerOptions.useDefineForClassFields) {
if (compilerOptions.target === ScriptTarget.ESNext && !!compilerOptions.useDefineForClassFields && getContainingClass(declaration)) {
return (isPropertyDeclaration(declaration) || isParameterPropertyDeclaration(declaration, declaration.parent)) &&
!isPropertyImmediatelyReferencedWithinDeclaration(declaration, usage, /*stopAtAnyPropertyDeclaration*/ true);
}

View File

@@ -0,0 +1,9 @@
//// [defineVariables_useDefineForClassFields.ts]
const a = () => b()
const b = () => null
a()
//// [defineVariables_useDefineForClassFields.js]
const a = () => b();
const b = () => null;
a();

View File

@@ -0,0 +1,11 @@
=== tests/cases/compiler/defineVariables_useDefineForClassFields.ts ===
const a = () => b()
>a : Symbol(a, Decl(defineVariables_useDefineForClassFields.ts, 0, 5))
>b : Symbol(b, Decl(defineVariables_useDefineForClassFields.ts, 1, 5))
const b = () => null
>b : Symbol(b, Decl(defineVariables_useDefineForClassFields.ts, 1, 5))
a()
>a : Symbol(a, Decl(defineVariables_useDefineForClassFields.ts, 0, 5))

View File

@@ -0,0 +1,16 @@
=== tests/cases/compiler/defineVariables_useDefineForClassFields.ts ===
const a = () => b()
>a : () => any
>() => b() : () => any
>b() : any
>b : () => any
const b = () => null
>b : () => any
>() => null : () => any
>null : null
a()
>a() : any
>a : () => any

View File

@@ -0,0 +1,6 @@
// @target: ESNext
// @useDefineForClassFields: true
const a = () => b()
const b = () => null
a()