Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report errors about private fields in exported class expressions #60885

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7316,8 +7316,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (propertySymbol.flags & SymbolFlags.Prototype) {
continue;
}
if (getDeclarationModifierFlagsFromSymbol(propertySymbol) & (ModifierFlags.Private | ModifierFlags.Protected) && context.tracker.reportPrivateInBaseOfClassExpression) {
context.tracker.reportPrivateInBaseOfClassExpression(unescapeLeadingUnderscores(propertySymbol.escapedName));
if ((getDeclarationModifierFlagsFromSymbol(propertySymbol) & (ModifierFlags.Private | ModifierFlags.Protected) || propertySymbol.valueDeclaration && isPrivateIdentifierClassElementDeclaration(propertySymbol.valueDeclaration)) && context.tracker.reportPrivateInBaseOfClassExpression) {
context.tracker.reportPrivateInBaseOfClassExpression(symbolToString(propertySymbol));
}
}
if (checkTruncationLength(context) && (i + 2 < properties.length - 1)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
another.ts(7,1): error TS4094: Property '#onDispose' of exported anonymous class type may not be private or protected.
first.ts(7,1): error TS4094: Property '#onDispose' of exported anonymous class type may not be private or protected.
first.ts(8,14): error TS4094: Property '#onDispose' of exported anonymous class type may not be private or protected.


==== first.ts (2 errors) ====
declare function mix<TMix>(mixin: TMix): TMix;

const DisposableMixin = class {
#onDispose() {}
};

export default mix(DisposableMixin);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS4094: Property '#onDispose' of exported anonymous class type may not be private or protected.
export class Monitor extends mix(DisposableMixin) {}
~~~~~~~
!!! error TS4094: Property '#onDispose' of exported anonymous class type may not be private or protected.

==== another.ts (1 errors) ====
declare function mix<TMix>(mixin: TMix): TMix;

const DisposableMixin = class {
#onDispose() {}
};

export default class extends mix(DisposableMixin) {}
~~~~~~
!!! error TS4094: Property '#onDispose' of exported anonymous class type may not be private or protected.

35 changes: 35 additions & 0 deletions tests/baselines/reference/declarationEmitMixinPrivateField1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//// [tests/cases/compiler/declarationEmitMixinPrivateField1.ts] ////

//// [first.ts]
declare function mix<TMix>(mixin: TMix): TMix;

const DisposableMixin = class {
#onDispose() {}
};

export default mix(DisposableMixin);
export class Monitor extends mix(DisposableMixin) {}

//// [another.ts]
declare function mix<TMix>(mixin: TMix): TMix;

const DisposableMixin = class {
#onDispose() {}
};

export default class extends mix(DisposableMixin) {}


//// [first.js]
const DisposableMixin = class {
#onDispose() { }
};
export default mix(DisposableMixin);
export class Monitor extends mix(DisposableMixin) {
}
//// [another.js]
const DisposableMixin = class {
#onDispose() { }
};
export default class extends mix(DisposableMixin) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//// [tests/cases/compiler/declarationEmitMixinPrivateField1.ts] ////

=== first.ts ===
declare function mix<TMix>(mixin: TMix): TMix;
>mix : Symbol(mix, Decl(first.ts, 0, 0))
>TMix : Symbol(TMix, Decl(first.ts, 0, 21))
>mixin : Symbol(mixin, Decl(first.ts, 0, 27))
>TMix : Symbol(TMix, Decl(first.ts, 0, 21))
>TMix : Symbol(TMix, Decl(first.ts, 0, 21))

const DisposableMixin = class {
>DisposableMixin : Symbol(DisposableMixin, Decl(first.ts, 2, 5))

#onDispose() {}
>#onDispose : Symbol(DisposableMixin.#onDispose, Decl(first.ts, 2, 31))

};

export default mix(DisposableMixin);
>mix : Symbol(mix, Decl(first.ts, 0, 0))
>DisposableMixin : Symbol(DisposableMixin, Decl(first.ts, 2, 5))

export class Monitor extends mix(DisposableMixin) {}
>Monitor : Symbol(Monitor, Decl(first.ts, 6, 36))
>mix : Symbol(mix, Decl(first.ts, 0, 0))
>DisposableMixin : Symbol(DisposableMixin, Decl(first.ts, 2, 5))

=== another.ts ===
declare function mix<TMix>(mixin: TMix): TMix;
>mix : Symbol(mix, Decl(another.ts, 0, 0))
>TMix : Symbol(TMix, Decl(another.ts, 0, 21))
>mixin : Symbol(mixin, Decl(another.ts, 0, 27))
>TMix : Symbol(TMix, Decl(another.ts, 0, 21))
>TMix : Symbol(TMix, Decl(another.ts, 0, 21))

const DisposableMixin = class {
>DisposableMixin : Symbol(DisposableMixin, Decl(another.ts, 2, 5))

#onDispose() {}
>#onDispose : Symbol(DisposableMixin.#onDispose, Decl(another.ts, 2, 31))

};

export default class extends mix(DisposableMixin) {}
>mix : Symbol(mix, Decl(another.ts, 0, 0))
>DisposableMixin : Symbol(DisposableMixin, Decl(another.ts, 2, 5))

66 changes: 66 additions & 0 deletions tests/baselines/reference/declarationEmitMixinPrivateField1.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//// [tests/cases/compiler/declarationEmitMixinPrivateField1.ts] ////

=== first.ts ===
declare function mix<TMix>(mixin: TMix): TMix;
>mix : <TMix>(mixin: TMix) => TMix
> : ^ ^^ ^^ ^^^^^
>mixin : TMix
> : ^^^^

const DisposableMixin = class {
>DisposableMixin : typeof DisposableMixin
> : ^^^^^^^^^^^^^^^^^^^^^^
>class { #onDispose() {}} : typeof DisposableMixin
> : ^^^^^^^^^^^^^^^^^^^^^^

#onDispose() {}
>#onDispose : () => void
> : ^^^^^^^^^^

};

export default mix(DisposableMixin);
>mix(DisposableMixin) : typeof DisposableMixin
> : ^^^^^^^^^^^^^^^^^^^^^^
>mix : <TMix>(mixin: TMix) => TMix
> : ^ ^^ ^^ ^^^^^
>DisposableMixin : typeof DisposableMixin
> : ^^^^^^^^^^^^^^^^^^^^^^

export class Monitor extends mix(DisposableMixin) {}
>Monitor : Monitor
> : ^^^^^^^
>mix(DisposableMixin) : DisposableMixin
> : ^^^^^^^^^^^^^^^
>mix : <TMix>(mixin: TMix) => TMix
> : ^ ^^ ^^ ^^^^^
>DisposableMixin : typeof DisposableMixin
> : ^^^^^^^^^^^^^^^^^^^^^^

=== another.ts ===
declare function mix<TMix>(mixin: TMix): TMix;
>mix : <TMix>(mixin: TMix) => TMix
> : ^ ^^ ^^ ^^^^^
>mixin : TMix
> : ^^^^

const DisposableMixin = class {
>DisposableMixin : typeof DisposableMixin
> : ^^^^^^^^^^^^^^^^^^^^^^
>class { #onDispose() {}} : typeof DisposableMixin
> : ^^^^^^^^^^^^^^^^^^^^^^

#onDispose() {}
>#onDispose : () => void
> : ^^^^^^^^^^

};

export default class extends mix(DisposableMixin) {}
>mix(DisposableMixin) : DisposableMixin
> : ^^^^^^^^^^^^^^^
>mix : <TMix>(mixin: TMix) => TMix
> : ^ ^^ ^^ ^^^^^
>DisposableMixin : typeof DisposableMixin
> : ^^^^^^^^^^^^^^^^^^^^^^

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ emitClassExpressionInDeclarationFile2.ts(1,12): error TS4094: Property 'p' of ex
emitClassExpressionInDeclarationFile2.ts(1,12): error TS4094: Property 'ps' of exported anonymous class type may not be private or protected.
emitClassExpressionInDeclarationFile2.ts(16,17): error TS4094: Property 'property' of exported anonymous class type may not be private or protected.
emitClassExpressionInDeclarationFile2.ts(23,14): error TS4094: Property 'property' of exported anonymous class type may not be private or protected.
emitClassExpressionInDeclarationFile2.ts(31,3): error TS4094: Property 'prop' of exported anonymous class type may not be private or protected.


==== emitClassExpressionInDeclarationFile2.ts (4 errors) ====
==== emitClassExpressionInDeclarationFile2.ts (5 errors) ====
export var noPrivates = class {
~~~~~~~~~~
!!! error TS4094: Property 'p' of exported anonymous class type may not be private or protected.
Expand Down Expand Up @@ -43,4 +44,12 @@ emitClassExpressionInDeclarationFile2.ts(23,14): error TS4094: Property 'propert

Test.getTags()
test.tags();

export class Test2 {
nested = class {
~~~~~~
!!! error TS4094: Property 'prop' of exported anonymous class type may not be private or protected.
private prop = 42;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ const test = new Test();

Test.getTags()
test.tags();

export class Test2 {
nested = class {
private prop = 42;
}
}


//// [emitClassExpressionInDeclarationFile2.js]
Expand All @@ -54,7 +60,7 @@ var __setFunctionName = (this && this.__setFunctionName) || function (f, name, p
};
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Test = exports.FooItem = exports.noPrivates = void 0;
exports.Test2 = exports.Test = exports.FooItem = exports.noPrivates = void 0;
exports.WithTags = WithTags;
exports.noPrivates = (_a = /** @class */ (function () {
function class_1() {
Expand Down Expand Up @@ -98,3 +104,15 @@ exports.Test = Test;
var test = new Test();
Test.getTags();
test.tags();
var Test2 = /** @class */ (function () {
function Test2() {
this.nested = /** @class */ (function () {
function class_3() {
this.prop = 42;
}
return class_3;
}());
}
return Test2;
}());
exports.Test2 = Test2;
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,14 @@ test.tags();
>test : Symbol(test, Decl(emitClassExpressionInDeclarationFile2.ts, 24, 5))
>tags : Symbol((Anonymous class).tags, Decl(emitClassExpressionInDeclarationFile2.ts, 17, 34))

export class Test2 {
>Test2 : Symbol(Test2, Decl(emitClassExpressionInDeclarationFile2.ts, 27, 12))

nested = class {
>nested : Symbol(Test2.nested, Decl(emitClassExpressionInDeclarationFile2.ts, 29, 20))

private prop = 42;
>prop : Symbol((Anonymous class).prop, Decl(emitClassExpressionInDeclarationFile2.ts, 30, 18))
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,21 @@ test.tags();
>tags : () => void
> : ^^^^^^

export class Test2 {
>Test2 : Test2
> : ^^^^^

nested = class {
>nested : typeof (Anonymous class)
> : ^^^^^^^^^^^^^^^^^^^^^^^^
>class { private prop = 42; } : typeof (Anonymous class)
> : ^^^^^^^^^^^^^^^^^^^^^^^^

private prop = 42;
>prop : number
> : ^^^^^^
>42 : 42
> : ^^
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
emitClassExpressionInDeclarationFile3.ts(1,12): error TS4094: Property '#p' of exported anonymous class type may not be private or protected.
emitClassExpressionInDeclarationFile3.ts(1,12): error TS4094: Property '#ps' of exported anonymous class type may not be private or protected.
emitClassExpressionInDeclarationFile3.ts(15,17): error TS4094: Property '#property' of exported anonymous class type may not be private or protected.
emitClassExpressionInDeclarationFile3.ts(22,14): error TS4094: Property '#property' of exported anonymous class type may not be private or protected.
emitClassExpressionInDeclarationFile3.ts(25,3): error TS4094: Property '#prop' of exported anonymous class type may not be private or protected.


==== emitClassExpressionInDeclarationFile3.ts (5 errors) ====
export var noPrivates = class {
~~~~~~~~~~
!!! error TS4094: Property '#p' of exported anonymous class type may not be private or protected.
!!! related TS9027 emitClassExpressionInDeclarationFile3.ts:1:12: Add a type annotation to the variable noPrivates.
~~~~~~~~~~
!!! error TS4094: Property '#ps' of exported anonymous class type may not be private or protected.
!!! related TS9027 emitClassExpressionInDeclarationFile3.ts:1:12: Add a type annotation to the variable noPrivates.
static getTags() { }
tags() { }
static #ps = -1
#p = 12
}

export class FooItem {
foo(): void { }
name?: string;
#property = "capitalism"
}

export type Constructor<T> = new(...args: any[]) => T;
export function WithTags<T extends Constructor<FooItem>>(Base: T) {
~~~~~~~~
!!! error TS4094: Property '#property' of exported anonymous class type may not be private or protected.
return class extends Base {
static getTags(): void { }
tags(): void { }
}
}

export class Test extends WithTags(FooItem) {}
~~~~
!!! error TS4094: Property '#property' of exported anonymous class type may not be private or protected.

export class Test2 {
nested = class {
~~~~~~
!!! error TS4094: Property '#prop' of exported anonymous class type may not be private or protected.
#prop = 42;
}
}

Loading
Loading