Skip to content

Commit e0f436c

Browse files
authored
feat(36080): forbid to use JSDoc visibility modifiers in private fields (microsoft#46056)
1 parent e4d9282 commit e0f436c

File tree

6 files changed

+437
-0
lines changed

6 files changed

+437
-0
lines changed

src/compiler/checker.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35861,6 +35861,13 @@ namespace ts {
3586135861
}
3586235862
}
3586335863

35864+
function checkJSDocAccessibilityModifiers(node: JSDocPublicTag | JSDocProtectedTag | JSDocPrivateTag): void {
35865+
const host = getJSDocHost(node);
35866+
if (host && isPrivateIdentifierClassElementDeclaration(host)) {
35867+
error(node, Diagnostics.An_accessibility_modifier_cannot_be_used_with_a_private_identifier);
35868+
}
35869+
}
35870+
3586435871
function getIdentifierFromEntityNameExpression(node: Identifier | PropertyAccessExpression): Identifier | PrivateIdentifier;
3586535872
function getIdentifierFromEntityNameExpression(node: Expression): Identifier | PrivateIdentifier | undefined;
3586635873
function getIdentifierFromEntityNameExpression(node: Expression): Identifier | PrivateIdentifier | undefined {
@@ -39854,6 +39861,10 @@ namespace ts {
3985439861
return;
3985539862
case SyntaxKind.JSDocTypeExpression:
3985639863
return checkSourceElement((node as JSDocTypeExpression).type);
39864+
case SyntaxKind.JSDocPublicTag:
39865+
case SyntaxKind.JSDocProtectedTag:
39866+
case SyntaxKind.JSDocPrivateTag:
39867+
return checkJSDocAccessibilityModifiers(node as JSDocPublicTag | JSDocProtectedTag | JSDocPrivateTag);
3985739868
case SyntaxKind.IndexedAccessType:
3985839869
return checkIndexedAccessType(node as IndexedAccessTypeNode);
3985939870
case SyntaxKind.MappedType:
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
tests/cases/conformance/classes/members/privateNames/privateNamesIncompatibleModifiersJs.js(3,8): error TS18010: An accessibility modifier cannot be used with a private identifier.
2+
tests/cases/conformance/classes/members/privateNames/privateNamesIncompatibleModifiersJs.js(8,8): error TS18010: An accessibility modifier cannot be used with a private identifier.
3+
tests/cases/conformance/classes/members/privateNames/privateNamesIncompatibleModifiersJs.js(13,8): error TS18010: An accessibility modifier cannot be used with a private identifier.
4+
tests/cases/conformance/classes/members/privateNames/privateNamesIncompatibleModifiersJs.js(18,8): error TS18010: An accessibility modifier cannot be used with a private identifier.
5+
tests/cases/conformance/classes/members/privateNames/privateNamesIncompatibleModifiersJs.js(23,8): error TS18010: An accessibility modifier cannot be used with a private identifier.
6+
tests/cases/conformance/classes/members/privateNames/privateNamesIncompatibleModifiersJs.js(28,8): error TS18010: An accessibility modifier cannot be used with a private identifier.
7+
tests/cases/conformance/classes/members/privateNames/privateNamesIncompatibleModifiersJs.js(33,8): error TS18010: An accessibility modifier cannot be used with a private identifier.
8+
tests/cases/conformance/classes/members/privateNames/privateNamesIncompatibleModifiersJs.js(37,8): error TS18010: An accessibility modifier cannot be used with a private identifier.
9+
tests/cases/conformance/classes/members/privateNames/privateNamesIncompatibleModifiersJs.js(42,8): error TS18010: An accessibility modifier cannot be used with a private identifier.
10+
tests/cases/conformance/classes/members/privateNames/privateNamesIncompatibleModifiersJs.js(46,8): error TS18010: An accessibility modifier cannot be used with a private identifier.
11+
tests/cases/conformance/classes/members/privateNames/privateNamesIncompatibleModifiersJs.js(51,7): error TS18010: An accessibility modifier cannot be used with a private identifier.
12+
tests/cases/conformance/classes/members/privateNames/privateNamesIncompatibleModifiersJs.js(55,8): error TS18010: An accessibility modifier cannot be used with a private identifier.
13+
14+
15+
==== tests/cases/conformance/classes/members/privateNames/privateNamesIncompatibleModifiersJs.js (12 errors) ====
16+
class A {
17+
/**
18+
* @public
19+
~~~~~~~
20+
*/
21+
~~~~~
22+
!!! error TS18010: An accessibility modifier cannot be used with a private identifier.
23+
#a = 1;
24+
25+
/**
26+
* @private
27+
~~~~~~~~
28+
*/
29+
~~~~~
30+
!!! error TS18010: An accessibility modifier cannot be used with a private identifier.
31+
#b = 1;
32+
33+
/**
34+
* @protected
35+
~~~~~~~~~~
36+
*/
37+
~~~~~
38+
!!! error TS18010: An accessibility modifier cannot be used with a private identifier.
39+
#c = 1;
40+
41+
/**
42+
* @public
43+
~~~~~~~
44+
*/
45+
~~~~~
46+
!!! error TS18010: An accessibility modifier cannot be used with a private identifier.
47+
#aMethod() { return 1; }
48+
49+
/**
50+
* @private
51+
~~~~~~~~
52+
*/
53+
~~~~~
54+
!!! error TS18010: An accessibility modifier cannot be used with a private identifier.
55+
#bMethod() { return 1; }
56+
57+
/**
58+
* @protected
59+
~~~~~~~~~~
60+
*/
61+
~~~~~
62+
!!! error TS18010: An accessibility modifier cannot be used with a private identifier.
63+
#cMethod() { return 1; }
64+
65+
/**
66+
* @public
67+
~~~~~~~
68+
*/
69+
~~~~~
70+
!!! error TS18010: An accessibility modifier cannot be used with a private identifier.
71+
get #aProp() { return 1; }
72+
/**
73+
* @public
74+
~~~~~~~
75+
*/
76+
~~~~~
77+
!!! error TS18010: An accessibility modifier cannot be used with a private identifier.
78+
set #aProp(value) { }
79+
80+
/**
81+
* @private
82+
~~~~~~~~
83+
*/
84+
~~~~~
85+
!!! error TS18010: An accessibility modifier cannot be used with a private identifier.
86+
get #bProp() { return 1; }
87+
/**
88+
* @private
89+
~~~~~~~~
90+
*/
91+
~~~~~
92+
!!! error TS18010: An accessibility modifier cannot be used with a private identifier.
93+
set #bProp(value) { }
94+
95+
/**
96+
* @protected
97+
~~~~~~~~~~
98+
*/
99+
~~~~
100+
!!! error TS18010: An accessibility modifier cannot be used with a private identifier.
101+
get #cProp() { return 1; }
102+
/**
103+
* @protected
104+
~~~~~~~~~~
105+
*/
106+
~~~~~
107+
!!! error TS18010: An accessibility modifier cannot be used with a private identifier.
108+
set #cProp(value) { }
109+
}
110+
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//// [privateNamesIncompatibleModifiersJs.js]
2+
class A {
3+
/**
4+
* @public
5+
*/
6+
#a = 1;
7+
8+
/**
9+
* @private
10+
*/
11+
#b = 1;
12+
13+
/**
14+
* @protected
15+
*/
16+
#c = 1;
17+
18+
/**
19+
* @public
20+
*/
21+
#aMethod() { return 1; }
22+
23+
/**
24+
* @private
25+
*/
26+
#bMethod() { return 1; }
27+
28+
/**
29+
* @protected
30+
*/
31+
#cMethod() { return 1; }
32+
33+
/**
34+
* @public
35+
*/
36+
get #aProp() { return 1; }
37+
/**
38+
* @public
39+
*/
40+
set #aProp(value) { }
41+
42+
/**
43+
* @private
44+
*/
45+
get #bProp() { return 1; }
46+
/**
47+
* @private
48+
*/
49+
set #bProp(value) { }
50+
51+
/**
52+
* @protected
53+
*/
54+
get #cProp() { return 1; }
55+
/**
56+
* @protected
57+
*/
58+
set #cProp(value) { }
59+
}
60+
61+
62+
//// [privateNamesIncompatibleModifiersJs.js]
63+
"use strict";
64+
var _A_instances, _A_a, _A_b, _A_c, _A_aMethod, _A_bMethod, _A_cMethod, _A_aProp_get, _A_aProp_set, _A_bProp_get, _A_bProp_set, _A_cProp_get, _A_cProp_set;
65+
class A {
66+
constructor() {
67+
_A_instances.add(this);
68+
/**
69+
* @public
70+
*/
71+
_A_a.set(this, 1);
72+
/**
73+
* @private
74+
*/
75+
_A_b.set(this, 1);
76+
/**
77+
* @protected
78+
*/
79+
_A_c.set(this, 1);
80+
}
81+
}
82+
_A_a = new WeakMap(), _A_b = new WeakMap(), _A_c = new WeakMap(), _A_instances = new WeakSet(), _A_aMethod = function _A_aMethod() { return 1; }, _A_bMethod = function _A_bMethod() { return 1; }, _A_cMethod = function _A_cMethod() { return 1; }, _A_aProp_get = function _A_aProp_get() { return 1; }, _A_aProp_set = function _A_aProp_set(value) { }, _A_bProp_get = function _A_bProp_get() { return 1; }, _A_bProp_set = function _A_bProp_set(value) { }, _A_cProp_get = function _A_cProp_get() { return 1; }, _A_cProp_set = function _A_cProp_set(value) { };
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
=== tests/cases/conformance/classes/members/privateNames/privateNamesIncompatibleModifiersJs.js ===
2+
class A {
3+
>A : Symbol(A, Decl(privateNamesIncompatibleModifiersJs.js, 0, 0))
4+
5+
/**
6+
* @public
7+
*/
8+
#a = 1;
9+
>#a : Symbol(A.#a, Decl(privateNamesIncompatibleModifiersJs.js, 0, 9))
10+
11+
/**
12+
* @private
13+
*/
14+
#b = 1;
15+
>#b : Symbol(A.#b, Decl(privateNamesIncompatibleModifiersJs.js, 4, 11))
16+
17+
/**
18+
* @protected
19+
*/
20+
#c = 1;
21+
>#c : Symbol(A.#c, Decl(privateNamesIncompatibleModifiersJs.js, 9, 11))
22+
23+
/**
24+
* @public
25+
*/
26+
#aMethod() { return 1; }
27+
>#aMethod : Symbol(A.#aMethod, Decl(privateNamesIncompatibleModifiersJs.js, 14, 11))
28+
29+
/**
30+
* @private
31+
*/
32+
#bMethod() { return 1; }
33+
>#bMethod : Symbol(A.#bMethod, Decl(privateNamesIncompatibleModifiersJs.js, 19, 28))
34+
35+
/**
36+
* @protected
37+
*/
38+
#cMethod() { return 1; }
39+
>#cMethod : Symbol(A.#cMethod, Decl(privateNamesIncompatibleModifiersJs.js, 24, 28))
40+
41+
/**
42+
* @public
43+
*/
44+
get #aProp() { return 1; }
45+
>#aProp : Symbol(A.#aProp, Decl(privateNamesIncompatibleModifiersJs.js, 29, 28), Decl(privateNamesIncompatibleModifiersJs.js, 34, 30))
46+
47+
/**
48+
* @public
49+
*/
50+
set #aProp(value) { }
51+
>#aProp : Symbol(A.#aProp, Decl(privateNamesIncompatibleModifiersJs.js, 29, 28), Decl(privateNamesIncompatibleModifiersJs.js, 34, 30))
52+
>value : Symbol(value, Decl(privateNamesIncompatibleModifiersJs.js, 38, 15))
53+
54+
/**
55+
* @private
56+
*/
57+
get #bProp() { return 1; }
58+
>#bProp : Symbol(A.#bProp, Decl(privateNamesIncompatibleModifiersJs.js, 38, 25), Decl(privateNamesIncompatibleModifiersJs.js, 43, 30))
59+
60+
/**
61+
* @private
62+
*/
63+
set #bProp(value) { }
64+
>#bProp : Symbol(A.#bProp, Decl(privateNamesIncompatibleModifiersJs.js, 38, 25), Decl(privateNamesIncompatibleModifiersJs.js, 43, 30))
65+
>value : Symbol(value, Decl(privateNamesIncompatibleModifiersJs.js, 47, 15))
66+
67+
/**
68+
* @protected
69+
*/
70+
get #cProp() { return 1; }
71+
>#cProp : Symbol(A.#cProp, Decl(privateNamesIncompatibleModifiersJs.js, 47, 25), Decl(privateNamesIncompatibleModifiersJs.js, 52, 30))
72+
73+
/**
74+
* @protected
75+
*/
76+
set #cProp(value) { }
77+
>#cProp : Symbol(A.#cProp, Decl(privateNamesIncompatibleModifiersJs.js, 47, 25), Decl(privateNamesIncompatibleModifiersJs.js, 52, 30))
78+
>value : Symbol(value, Decl(privateNamesIncompatibleModifiersJs.js, 56, 15))
79+
}
80+
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
=== tests/cases/conformance/classes/members/privateNames/privateNamesIncompatibleModifiersJs.js ===
2+
class A {
3+
>A : A
4+
5+
/**
6+
* @public
7+
*/
8+
#a = 1;
9+
>#a : number
10+
>1 : 1
11+
12+
/**
13+
* @private
14+
*/
15+
#b = 1;
16+
>#b : number
17+
>1 : 1
18+
19+
/**
20+
* @protected
21+
*/
22+
#c = 1;
23+
>#c : number
24+
>1 : 1
25+
26+
/**
27+
* @public
28+
*/
29+
#aMethod() { return 1; }
30+
>#aMethod : () => number
31+
>1 : 1
32+
33+
/**
34+
* @private
35+
*/
36+
#bMethod() { return 1; }
37+
>#bMethod : () => number
38+
>1 : 1
39+
40+
/**
41+
* @protected
42+
*/
43+
#cMethod() { return 1; }
44+
>#cMethod : () => number
45+
>1 : 1
46+
47+
/**
48+
* @public
49+
*/
50+
get #aProp() { return 1; }
51+
>#aProp : number
52+
>1 : 1
53+
54+
/**
55+
* @public
56+
*/
57+
set #aProp(value) { }
58+
>#aProp : number
59+
>value : number
60+
61+
/**
62+
* @private
63+
*/
64+
get #bProp() { return 1; }
65+
>#bProp : number
66+
>1 : 1
67+
68+
/**
69+
* @private
70+
*/
71+
set #bProp(value) { }
72+
>#bProp : number
73+
>value : number
74+
75+
/**
76+
* @protected
77+
*/
78+
get #cProp() { return 1; }
79+
>#cProp : number
80+
>1 : 1
81+
82+
/**
83+
* @protected
84+
*/
85+
set #cProp(value) { }
86+
>#cProp : number
87+
>value : number
88+
}
89+

0 commit comments

Comments
 (0)