Skip to content

Commit 2cc1340

Browse files
authored
Ignore @private/@Protected on constructor functions (#35782)
* Ignore @Private on constructor functions This was incorrect in the best of circumstances and caused a crash when the parent of the function had no symbol, because the accessibility check assumed it was operating on a constructor and that the parent was always the containing class. * Non-constructors are always accessible Previously, all function-like kinds were accessible, which includes constructors. This was wrong.
1 parent 9445657 commit 2cc1340

File tree

5 files changed

+85
-2
lines changed

5 files changed

+85
-2
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24865,8 +24865,8 @@ namespace ts {
2486524865
const declaration = signature.declaration;
2486624866
const modifiers = getSelectedModifierFlags(declaration, ModifierFlags.NonPublicAccessibilityModifier);
2486724867

24868-
// Public constructor is accessible.
24869-
if (!modifiers) {
24868+
// (1) Public constructors and (2) constructor functions are always accessible.
24869+
if (!modifiers || declaration.kind !== SyntaxKind.Constructor) {
2487024870
return true;
2487124871
}
2487224872

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//// [privateConstructorFunction.js]
2+
{
3+
// make sure not to crash when parent's a block rather than a source file or some other
4+
// symbol-having node.
5+
6+
/** @private */
7+
function C() {
8+
this.x = 1
9+
}
10+
new C()
11+
}
12+
13+
14+
//// [privateConstructorFunction.js]
15+
{
16+
// make sure not to crash when parent's a block rather than a source file or some other
17+
// symbol-having node.
18+
/** @private */
19+
function C() {
20+
this.x = 1;
21+
}
22+
new C();
23+
}
24+
25+
26+
//// [privateConstructorFunction.d.ts]
27+
/** @private */
28+
declare function C(): void;
29+
declare class C {
30+
x: number;
31+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/conformance/salsa/privateConstructorFunction.js ===
2+
{
3+
// make sure not to crash when parent's a block rather than a source file or some other
4+
// symbol-having node.
5+
6+
/** @private */
7+
function C() {
8+
>C : Symbol(C, Decl(privateConstructorFunction.js, 0, 1))
9+
10+
this.x = 1
11+
>x : Symbol(C.x, Decl(privateConstructorFunction.js, 5, 18))
12+
}
13+
new C()
14+
>C : Symbol(C, Decl(privateConstructorFunction.js, 0, 1))
15+
}
16+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
=== tests/cases/conformance/salsa/privateConstructorFunction.js ===
2+
{
3+
// make sure not to crash when parent's a block rather than a source file or some other
4+
// symbol-having node.
5+
6+
/** @private */
7+
function C() {
8+
>C : typeof C
9+
10+
this.x = 1
11+
>this.x = 1 : 1
12+
>this.x : any
13+
>this : any
14+
>x : any
15+
>1 : 1
16+
}
17+
new C()
18+
>new C() : C
19+
>C : typeof C
20+
}
21+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// @allowjs: true
2+
// @checkjs: true
3+
// @outdir: salsa
4+
// @declaration: true
5+
// @filename: privateConstructorFunction.js
6+
{
7+
// make sure not to crash when parent's a block rather than a source file or some other
8+
// symbol-having node.
9+
10+
/** @private */
11+
function C() {
12+
this.x = 1
13+
}
14+
new C()
15+
}

0 commit comments

Comments
 (0)