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.
This commit is contained in:
Nathan Shively-Sanders 2019-12-20 08:41:52 -08:00 committed by GitHub
parent 9445657184
commit 2cc1340a7b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 85 additions and 2 deletions

View file

@ -24865,8 +24865,8 @@ namespace ts {
const declaration = signature.declaration;
const modifiers = getSelectedModifierFlags(declaration, ModifierFlags.NonPublicAccessibilityModifier);
// Public constructor is accessible.
if (!modifiers) {
// (1) Public constructors and (2) constructor functions are always accessible.
if (!modifiers || declaration.kind !== SyntaxKind.Constructor) {
return true;
}

View file

@ -0,0 +1,31 @@
//// [privateConstructorFunction.js]
{
// make sure not to crash when parent's a block rather than a source file or some other
// symbol-having node.
/** @private */
function C() {
this.x = 1
}
new C()
}
//// [privateConstructorFunction.js]
{
// make sure not to crash when parent's a block rather than a source file or some other
// symbol-having node.
/** @private */
function C() {
this.x = 1;
}
new C();
}
//// [privateConstructorFunction.d.ts]
/** @private */
declare function C(): void;
declare class C {
x: number;
}

View file

@ -0,0 +1,16 @@
=== tests/cases/conformance/salsa/privateConstructorFunction.js ===
{
// make sure not to crash when parent's a block rather than a source file or some other
// symbol-having node.
/** @private */
function C() {
>C : Symbol(C, Decl(privateConstructorFunction.js, 0, 1))
this.x = 1
>x : Symbol(C.x, Decl(privateConstructorFunction.js, 5, 18))
}
new C()
>C : Symbol(C, Decl(privateConstructorFunction.js, 0, 1))
}

View file

@ -0,0 +1,21 @@
=== tests/cases/conformance/salsa/privateConstructorFunction.js ===
{
// make sure not to crash when parent's a block rather than a source file or some other
// symbol-having node.
/** @private */
function C() {
>C : typeof C
this.x = 1
>this.x = 1 : 1
>this.x : any
>this : any
>x : any
>1 : 1
}
new C()
>new C() : C
>C : typeof C
}

View file

@ -0,0 +1,15 @@
// @allowjs: true
// @checkjs: true
// @outdir: salsa
// @declaration: true
// @filename: privateConstructorFunction.js
{
// make sure not to crash when parent's a block rather than a source file or some other
// symbol-having node.
/** @private */
function C() {
this.x = 1
}
new C()
}