Skip to content

Commit 8928039

Browse files
committed
post PR review improvements:
- simplify obtaining private fields's static or instance type - make visitor node test more specific - clean up warnings about static class fields in tests Signed-off-by: Ashley Claymore <[email protected]>
1 parent 02f59a3 commit 8928039

File tree

7 files changed

+13
-28
lines changed

7 files changed

+13
-28
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23245,16 +23245,15 @@ namespace ts {
2324523245
}
2324623246
const classSymbol = symbol.parent!;
2324723247
const classType = <InterfaceType>getTypeOfSymbol(classSymbol);
23248-
const firstDecl = symbol.declarations?.[0];
23249-
Debug.assert(firstDecl, "should always have a declaration");
23248+
const classDecl = symbol.valueDeclaration;
23249+
Debug.assert(classDecl, "should always have a declaration");
2325023250
let targetType: Type;
23251-
if (hasSyntacticModifier(firstDecl, ModifierFlags.Static)) {
23251+
if (hasStaticModifier(classDecl)) {
2325223252
targetType = classType;
2325323253
}
2325423254
else {
23255-
const ctorSigs = getSignaturesOfType(classType, SignatureKind.Construct);
23256-
Debug.assert(ctorSigs.length > 0, "should always have a constructor");
23257-
targetType = getReturnTypeOfSignature(ctorSigs[0]);
23255+
const classInstanceType = getDeclaredTypeOfSymbol(classSymbol);
23256+
targetType = classInstanceType;
2325823257
}
2325923258
return getNarrowedType(type, targetType, assumeTrue, isTypeDerivedFrom);
2326023259
}

src/compiler/transformers/classFields.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ namespace ts {
220220
}
221221

222222
// Private name has not been declared. Subsequent transformers will handle this error
223-
// TODO(aclaymore): confirm this is how we want to handle the error
224223
return visitEachChild(node, visitor, context);
225224
}
226225

src/compiler/utilities.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3580,9 +3580,6 @@ namespace ts {
35803580
return getBinaryOperatorPrecedence(operatorKind);
35813581
}
35823582

3583-
case SyntaxKind.PrivateIdentifierInInExpression:
3584-
return OperatorPrecedence.Relational;
3585-
35863583
// TODO: Should prefix `++` and `--` be moved to the `Update` precedence?
35873584
case SyntaxKind.TypeAssertionExpression:
35883585
case SyntaxKind.NonNullExpression:
@@ -3609,6 +3606,7 @@ namespace ts {
36093606
return OperatorPrecedence.Member;
36103607

36113608
case SyntaxKind.AsExpression:
3609+
case SyntaxKind.PrivateIdentifierInInExpression:
36123610
return OperatorPrecedence.Relational;
36133611

36143612
case SyntaxKind.ThisKeyword:

src/compiler/visitorPublic.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ namespace ts {
795795

796796
case SyntaxKind.PrivateIdentifierInInExpression:
797797
return factory.updatePrivateIdentifierInInExpression(<PrivateIdentifierInInExpression>node,
798-
nodeVisitor((<PrivateIdentifierInInExpression>node).name, visitor, isExpression),
798+
nodeVisitor((<PrivateIdentifierInInExpression>node).name, visitor, isMemberName),
799799
nodeVisitor((<PrivateIdentifierInInExpression>node).inToken, tokenVisitor, isToken),
800800
nodeVisitor((<PrivateIdentifierInInExpression>node).expression, visitor, isExpression));
801801

tests/baselines/reference/privateNameInInExpression.errors.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
tests/cases/conformance/classes/members/privateNames/privateNameInInExpression.ts(3,27): error TS2805: Static fields with private names can't have initializers when the '--useDefineForClassFields' flag is not specified with a '--target' of 'esnext'. Consider adding the '--useDefineForClassFields' flag.
21
tests/cases/conformance/classes/members/privateNames/privateNameInInExpression.ts(21,29): error TS2571: Object is of type 'unknown'.
32
tests/cases/conformance/classes/members/privateNames/privateNameInInExpression.ts(23,19): error TS2552: Cannot find name '#fiel'. Did you mean '#field'?
43
tests/cases/conformance/classes/members/privateNames/privateNameInInExpression.ts(25,20): error TS2304: Cannot find name '#field'.
@@ -10,12 +9,10 @@ tests/cases/conformance/classes/members/privateNames/privateNameInInExpression.t
109
tests/cases/conformance/classes/members/privateNames/privateNameInInExpression.ts(108,12): error TS18016: Private identifiers are not allowed outside class bodies.
1110

1211

13-
==== tests/cases/conformance/classes/members/privateNames/privateNameInInExpression.ts (10 errors) ====
12+
==== tests/cases/conformance/classes/members/privateNames/privateNameInInExpression.ts (9 errors) ====
1413
class Foo {
1514
#field = 1;
1615
static #staticField = 2;
17-
~
18-
!!! error TS2805: Static fields with private names can't have initializers when the '--useDefineForClassFields' flag is not specified with a '--target' of 'esnext'. Consider adding the '--useDefineForClassFields' flag.
1916
#method() {}
2017
static #staticMethod() {}
2118

tests/baselines/reference/privateNameInInExpression.js

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,8 @@ function badSyntax(v: Foo) {
113113
//// [privateNameInInExpression.js]
114114
"use strict";
115115
class Foo {
116-
constructor() {
117-
this.#field = 1;
118-
}
119-
#field;
120-
static #staticField;
116+
#field = 1;
117+
static #staticField = 2;
121118
#method() { }
122119
static #staticMethod() { }
123120
goodRhs(v) {
@@ -202,17 +199,11 @@ class Foo {
202199
}
203200
}
204201
}
205-
Foo.#staticField = 2;
206202
class FooSub extends Foo {
207-
constructor() {
208-
super(...arguments);
209-
this.subTypeOfFoo = true;
210-
}
203+
subTypeOfFoo = true;
211204
}
212205
class Bar {
213-
constructor() {
214-
this.notFoo = true;
215-
}
206+
notFoo = true;
216207
}
217208
function badSyntax(v) {
218209
return #field in v; // Bad - outside of class

tests/cases/conformance/classes/members/privateNames/privateNameInInExpression.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @strict: true
22
// @target: esnext
3+
// @useDefineForClassFields: true
34

45
class Foo {
56
#field = 1;

0 commit comments

Comments
 (0)