Skip to content

Commit b2c1019

Browse files
committed
remove redundant check of emit target
1 parent f0315b9 commit b2c1019

6 files changed

+78
-31
lines changed

src/compiler/checker.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2782,8 +2782,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
27822782
return !isPropertyImmediatelyReferencedWithinDeclaration(declaration, usage, /*stopAtAnyPropertyDeclaration*/ false);
27832783
}
27842784
else if (isParameterPropertyDeclaration(declaration, declaration.parent)) {
2785-
// foo = this.bar is illegal in es2022+useDefineForClassFields when bar is a parameter property
2786-
return !(getEmitScriptTarget(compilerOptions) >= ScriptTarget.ES2022 && useDefineForClassFields
2785+
// foo = this.bar is illegal in useDefineForClassFields when bar is a parameter property
2786+
return !(useDefineForClassFields
27872787
&& getContainingClass(declaration) === getContainingClass(usage)
27882788
&& isUsedInFunctionOrInstanceProperty(usage, declaration));
27892789
}
@@ -2814,7 +2814,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
28142814
return true;
28152815
}
28162816
if (isUsedInFunctionOrInstanceProperty(usage, declaration)) {
2817-
if (getEmitScriptTarget(compilerOptions) >= ScriptTarget.ES2022 && useDefineForClassFields
2817+
if (useDefineForClassFields
28182818
&& getContainingClass(declaration)
28192819
&& (isPropertyDeclaration(declaration) || isParameterPropertyDeclaration(declaration, declaration.parent))) {
28202820
return !isPropertyImmediatelyReferencedWithinDeclaration(declaration, usage, /*stopAtAnyPropertyDeclaration*/ true);
@@ -2971,7 +2971,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
29712971
case SyntaxKind.PropertyDeclaration:
29722972
// static properties in classes introduce temporary variables
29732973
if (hasStaticModifier(node)) {
2974-
return target < ScriptTarget.ESNext || !useDefineForClassFields;
2974+
return !useDefineForClassFields;
29752975
}
29762976
return requiresScopeChangeWorker((node as PropertyDeclaration).name);
29772977
default:
@@ -3389,10 +3389,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
33893389
// 1. When result is undefined, after checking for a missing "this."
33903390
// 2. When result is defined
33913391
function checkAndReportErrorForInvalidInitializer() {
3392-
if (propertyWithInvalidInitializer && !(useDefineForClassFields && getEmitScriptTarget(compilerOptions) >= ScriptTarget.ES2022)) {
3392+
if (propertyWithInvalidInitializer && !useDefineForClassFields) {
33933393
// We have a match, but the reference occurred within a property initializer and the identifier also binds
33943394
// to a local variable in the constructor where the code will be emitted. Note that this is actually allowed
3395-
// with ESNext+useDefineForClassFields because the scope semantics are different.
3395+
// with useDefineForClassFields because the scope semantics are different.
33963396
error(errorLocation,
33973397
errorLocation && propertyWithInvalidInitializer.type && textRangeContainsPositionInclusive(propertyWithInvalidInitializer.type, errorLocation.pos)
33983398
? Diagnostics.Type_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor
@@ -31739,7 +31739,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3173931739
&& !(isAccessExpression(node) && isAccessExpression(node.expression))
3174031740
&& !isBlockScopedNameDeclaredBeforeUse(valueDeclaration, right)
3174131741
&& !(isMethodDeclaration(valueDeclaration) && getCombinedModifierFlagsCached(valueDeclaration) & ModifierFlags.Static)
31742-
&& (compilerOptions.useDefineForClassFields || !isPropertyDeclaredInAncestorClass(prop))) {
31742+
&& (useDefineForClassFields || !isPropertyDeclaredInAncestorClass(prop))) {
3174331743
diagnosticMessage = error(right, Diagnostics.Property_0_is_used_before_its_initialization, declarationName);
3174431744
}
3174531745
else if (valueDeclaration.kind === SyntaxKind.ClassDeclaration &&
@@ -38429,7 +38429,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3842938429
case "length":
3843038430
case "caller":
3843138431
case "arguments":
38432-
if (compilerOptions.useDefineForClassFields) {
38432+
if (useDefineForClassFields) {
3843338433
break;
3843438434
}
3843538435
// fall through
@@ -38634,7 +38634,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3863438634
// or the containing class declares instance member variables with initializers.
3863538635

3863638636
const superCallShouldBeRootLevel =
38637-
(getEmitScriptTarget(compilerOptions) !== ScriptTarget.ESNext || !useDefineForClassFields) &&
38637+
(!useDefineForClassFields) &&
3863838638
(some((node.parent as ClassDeclaration).members, isInstancePropertyWithInitializerOrPrivateIdentifierProperty) ||
3863938639
some(node.parameters, p => hasSyntacticModifier(p, ModifierFlags.ParameterPropertyModifier)));
3864038640

@@ -42900,7 +42900,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4290042900
!legacyDecorators && languageVersion < ScriptTarget.ESNext &&
4290142901
classOrConstructorParameterIsDecorated(/*useLegacyDecorators*/ false, node);
4290242902
const willTransformPrivateElementsOrClassStaticBlocks = languageVersion <= ScriptTarget.ES2022;
42903-
const willTransformInitializers = !useDefineForClassFields || languageVersion < ScriptTarget.ES2022;
42903+
const willTransformInitializers = !useDefineForClassFields;
4290442904
if (willTransformStaticElementsOfDecoratedClass || willTransformPrivateElementsOrClassStaticBlocks) {
4290542905
for (const member of node.members) {
4290642906
if (willTransformStaticElementsOfDecoratedClass && classElementOrClassElementParameterIsDecorated(/*useLegacyDecorators*/ false, member, node)) {

tests/baselines/reference/classMemberInitializerScoping2(target=es2017,usedefineforclassfields=true).errors.txt

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/baselines/reference/classMemberInitializerScoping2(target=es2017,usedefineforclassfields=true).types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class C {
99
>C : C
1010

1111
p = x
12-
>p : any
13-
>x : any
12+
>p : number
13+
>x : 1
1414

1515
constructor(x: string) { }
1616
>x : string
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
defineProperty.ts(3,14): error TS2729: Property 'y' is used before its initialization.
2+
defineProperty.ts(10,14): error TS2729: Property 'y' is used before its initialization.
3+
defineProperty.ts(18,14): error TS2729: Property 'ka' is used before its initialization.
4+
defineProperty.ts(22,15): error TS2729: Property 'ka' is used before its initialization.
5+
6+
7+
==== defineProperty.ts (4 errors) ====
8+
var x: "p" = "p"
9+
class A {
10+
a = this.y
11+
~
12+
!!! error TS2729: Property 'y' is used before its initialization.
13+
!!! related TS2728 defineProperty.ts:9:17: 'y' is declared here.
14+
b
15+
public c;
16+
["computed"] = 13
17+
;[x] = 14
18+
m() { }
19+
constructor(public readonly y: number) { }
20+
z = this.y
21+
~
22+
!!! error TS2729: Property 'y' is used before its initialization.
23+
!!! related TS2728 defineProperty.ts:9:17: 'y' is declared here.
24+
declare notEmitted;
25+
}
26+
class B {
27+
public a;
28+
}
29+
class C extends B {
30+
declare public a;
31+
z = this.ka
32+
~~
33+
!!! error TS2729: Property 'ka' is used before its initialization.
34+
!!! related TS2728 defineProperty.ts:19:17: 'ka' is declared here.
35+
constructor(public ka: number) {
36+
super()
37+
}
38+
ki = this.ka
39+
~~
40+
!!! error TS2729: Property 'ka' is used before its initialization.
41+
!!! related TS2728 defineProperty.ts:19:17: 'ka' is declared here.
42+
}
43+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
initializationOrdering1.ts(11,16): error TS2729: Property 'facade' is used before its initialization.
2+
3+
4+
==== initializationOrdering1.ts (1 errors) ====
5+
class Helper {
6+
create(): boolean {
7+
return true
8+
}
9+
}
10+
11+
export class Broken {
12+
constructor(readonly facade: Helper) {
13+
console.log(this.bug)
14+
}
15+
bug = this.facade.create()
16+
~~~~~~
17+
!!! error TS2729: Property 'facade' is used before its initialization.
18+
!!! related TS2728 initializationOrdering1.ts:8:17: 'facade' is declared here.
19+
20+
}
21+
22+
new Broken(new Helper)
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
1-
privateNameBadSuperUseDefineForClassFields.ts(4,3): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties, parameter properties, or private identifiers.
21
privateNameBadSuperUseDefineForClassFields.ts(5,5): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
32

43

5-
==== privateNameBadSuperUseDefineForClassFields.ts (2 errors) ====
4+
==== privateNameBadSuperUseDefineForClassFields.ts (1 errors) ====
65
class B {};
76
class A extends B {
87
#x;
98
constructor() {
10-
~~~~~~~~~~~~~~~
119
this;
12-
~~~~~~~~~
1310
~~~~
1411
!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
1512
super();
16-
~~~~~~~~~~~~
1713
}
18-
~~~
19-
!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties, parameter properties, or private identifiers.
2014
}
2115

0 commit comments

Comments
 (0)