Skip to content

Commit 4dd7d85

Browse files
Max HeiberJoseph Watts
Max Heiber
authored and
Joseph Watts
committed
begin update checker for private names
- [x] treat private names as unique: - case 1: cannot say that a variable is of a class type unless the variable points to an instance of the class - see [test](https://github.com/mheiber/TypeScript/tree/checker/tests/cases/conformance/classes/members/privateNames/privateNamesUnique.ts) - case 2: private names in class hierarchies do not conflict - see [test](https://github.com/mheiber/TypeScript/tree/checker/tests/cases/conformance/classes/members/privateNames/privateNamesNoConflictWhenInheriting.ts) - [x] `#constructor` is reserved - see [test](https://github.com/mheiber/TypeScript/tree/checker/tests/cases/conformance/classes/members/privateNames/privateNameConstructorReserved.ts) - check in `bindWorker`, where every node is visited - [x] Accessibility modifiers can't be used with private names - see [test](https://github.com/mheiber/TypeScript/tree/checker/tests/cases/conformance/classes/members/privateNames/privateNamesNoAccessibilityModifiers.ts) - implemented in `checkAccessibilityModifiers`, using `ModifierFlags.AccessibilityModifier` - [x] `delete #foo` not allowed - [x] Private name accesses not allowed outside of the defining class - see test: https://github.com/mheiber/TypeScript/tree/checker/tests/cases/conformance/classes/members/privateNames/privateNameNotAccessibleOutsideDefiningClass.ts - see [test](https://github.com/mheiber/TypeScript/tree/checker/tests/cases/conformance/classes/members/privateNames/privateNamesNoDelete.ts) - implemented in `checkDeleteExpression` - [x] Do [the right thing](https://gist.github.com/mheiber/b6fc7adb426c2e1cdaceb5d7786fc630) for nested classes Incorporate checker PR feedback mv private name tests together more checker tests for private names update naming and cleanup for check private names for private name support in the checker: - make names more consistent - remove unnecessary checks - add utility function to public API - other small cleanup Move getPropertyNameForUniqueESSymbol to utility for consistency with other calculation of special property names (starting with __), move the calculation of property names for unique es symbols to `utilities.ts`. private name tests strict+es6 Update private name tests to use 'strict' type checking and to target es6 instead of default. Makes the js output easier to read and tests more surface area with other checker features. error message for private names in obj literals Disallow decorating private-named properties Disallow decorating private-named properties because the spec is still in flux. Signed-off-by: Max Heiber <[email protected]>
1 parent 9f10880 commit 4dd7d85

File tree

6 files changed

+97
-2
lines changed

6 files changed

+97
-2
lines changed

src/compiler/utilities.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,10 @@ namespace ts {
14971497
export function nodeCanBeDecorated(node: ClassElement, parent: Node): boolean;
14981498
export function nodeCanBeDecorated(node: Node, parent: Node, grandparent: Node): boolean;
14991499
export function nodeCanBeDecorated(node: Node, parent?: Node, grandparent?: Node): boolean {
1500+
// private names cannot be used with decorators yet
1501+
if (isNamedDeclaration(node) && node.name.kind === SyntaxKind.PrivateName) {
1502+
return false;
1503+
}
15001504
switch (node.kind) {
15011505
case SyntaxKind.ClassDeclaration:
15021506
// classes are valid targets
@@ -1510,8 +1514,8 @@ namespace ts {
15101514
case SyntaxKind.SetAccessor:
15111515
case SyntaxKind.MethodDeclaration:
15121516
// if this method has a body and its parent is a class declaration, this is a valid target.
1513-
return (<FunctionLikeDeclaration>node).body !== undefined
1514-
&& parent!.kind === SyntaxKind.ClassDeclaration;
1517+
return (node as FunctionLikeDeclaration).body !== undefined
1518+
&& parent!.kind === SyntaxKind.ClassDeclaration
15151519

15161520
case SyntaxKind.Parameter:
15171521
// if the parameter's parent has a body and its grandparent is a class declaration, this is a valid target;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
tests/cases/conformance/classes/members/privateNames/privateNamesAndDecorators.ts(4,5): error TS1206: Decorators are not valid here.
2+
tests/cases/conformance/classes/members/privateNames/privateNamesAndDecorators.ts(6,5): error TS1206: Decorators are not valid here.
3+
4+
5+
==== tests/cases/conformance/classes/members/privateNames/privateNamesAndDecorators.ts (2 errors) ====
6+
declare function dec<T>(target: T): T;
7+
8+
class A {
9+
@dec
10+
~
11+
!!! error TS1206: Decorators are not valid here.
12+
#foo = 1;
13+
@dec
14+
~
15+
!!! error TS1206: Decorators are not valid here.
16+
#bar(): void { }
17+
}
18+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//// [privateNamesAndDecorators.ts]
2+
declare function dec<T>(target: T): T;
3+
4+
class A {
5+
@dec
6+
#foo = 1;
7+
@dec
8+
#bar(): void { }
9+
}
10+
11+
12+
//// [privateNamesAndDecorators.js]
13+
var A = /** @class */ (function () {
14+
function A() {
15+
this.#foo = 1;
16+
}
17+
A.prototype.#bar = function () { };
18+
return A;
19+
}());
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
=== tests/cases/conformance/classes/members/privateNames/privateNamesAndDecorators.ts ===
2+
declare function dec<T>(target: T): T;
3+
>dec : Symbol(dec, Decl(privateNamesAndDecorators.ts, 0, 0))
4+
>T : Symbol(T, Decl(privateNamesAndDecorators.ts, 0, 21))
5+
>target : Symbol(target, Decl(privateNamesAndDecorators.ts, 0, 24))
6+
>T : Symbol(T, Decl(privateNamesAndDecorators.ts, 0, 21))
7+
>T : Symbol(T, Decl(privateNamesAndDecorators.ts, 0, 21))
8+
9+
class A {
10+
>A : Symbol(A, Decl(privateNamesAndDecorators.ts, 0, 38))
11+
12+
@dec
13+
>dec : Symbol(dec, Decl(privateNamesAndDecorators.ts, 0, 0))
14+
15+
#foo = 1;
16+
>#foo : Symbol(A.#foo, Decl(privateNamesAndDecorators.ts, 2, 9))
17+
18+
@dec
19+
>dec : Symbol(dec, Decl(privateNamesAndDecorators.ts, 0, 0))
20+
21+
#bar(): void { }
22+
>#bar : Symbol(A.#bar, Decl(privateNamesAndDecorators.ts, 4, 13))
23+
}
24+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
=== tests/cases/conformance/classes/members/privateNames/privateNamesAndDecorators.ts ===
2+
declare function dec<T>(target: T): T;
3+
>dec : <T>(target: T) => T
4+
>target : T
5+
6+
class A {
7+
>A : A
8+
9+
@dec
10+
>dec : <T>(target: T) => T
11+
12+
#foo = 1;
13+
>#foo : number
14+
>1 : 1
15+
16+
@dec
17+
>dec : <T>(target: T) => T
18+
19+
#bar(): void { }
20+
>#bar : () => void
21+
}
22+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
declare function dec<T>(target: T): T;
2+
3+
class A {
4+
@dec
5+
#foo = 1;
6+
@dec
7+
#bar(): void { }
8+
}

0 commit comments

Comments
 (0)