Skip to content

Commit b28f427

Browse files
committed
fix: private field in class expression emits invalid dts
1 parent 6afd0fb commit b28f427

6 files changed

Lines changed: 90 additions & 53 deletions

src/compiler/checker.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7485,8 +7485,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
74857485
if (propertySymbol.flags & SymbolFlags.Prototype) {
74867486
continue;
74877487
}
7488-
if (getDeclarationModifierFlagsFromSymbol(propertySymbol) & (ModifierFlags.Private | ModifierFlags.Protected) && context.tracker.reportPrivateInBaseOfClassExpression) {
7488+
if (context.tracker.reportPrivateInBaseOfClassExpression && (getDeclarationModifierFlagsFromSymbol(propertySymbol) & (ModifierFlags.Private | ModifierFlags.Protected))) {
74897489
context.tracker.reportPrivateInBaseOfClassExpression(unescapeLeadingUnderscores(propertySymbol.escapedName));
7490+
} else if (context.tracker.reportPrivateInBaseOfClassExpression && isHashPrivate(propertySymbol)) {
7491+
context.tracker.reportPrivateInBaseOfClassExpression(unescapeLeadingUnderscores(getClonedHashPrivateName(propertySymbol)?.escapedText || '' as __String));
74907492
}
74917493
}
74927494
if (checkTruncationLength(context) && (i + 2 < properties.length - 1)) {

tests/baselines/reference/emitClassExpressionInDeclarationFile2.errors.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ emitClassExpressionInDeclarationFile2.ts(1,12): error TS4094: Property 'p' of ex
22
emitClassExpressionInDeclarationFile2.ts(1,12): error TS4094: Property 'ps' of exported anonymous class type may not be private or protected.
33
emitClassExpressionInDeclarationFile2.ts(16,17): error TS4094: Property 'property' of exported anonymous class type may not be private or protected.
44
emitClassExpressionInDeclarationFile2.ts(23,14): error TS4094: Property 'property' of exported anonymous class type may not be private or protected.
5+
emitClassExpressionInDeclarationFile2.ts(30,12): error TS4094: Property '#p' of exported anonymous class type may not be private or protected.
56

67

7-
==== emitClassExpressionInDeclarationFile2.ts (4 errors) ====
8+
==== emitClassExpressionInDeclarationFile2.ts (5 errors) ====
89
export var noPrivates = class {
910
~~~~~~~~~~
1011
!!! error TS4094: Property 'p' of exported anonymous class type may not be private or protected.
@@ -43,4 +44,13 @@ emitClassExpressionInDeclarationFile2.ts(23,14): error TS4094: Property 'propert
4344

4445
Test.getTags()
4546
test.tags();
47+
48+
export var noPrivates2 = class {
49+
~~~~~~~~~~~
50+
!!! error TS4094: Property '#p' of exported anonymous class type may not be private or protected.
51+
!!! related TS9027 emitClassExpressionInDeclarationFile2.ts:30:12: Add a type annotation to the variable noPrivates2.
52+
static getTags() { }
53+
tags() { }
54+
#p = -1
55+
}
4656

tests/baselines/reference/emitClassExpressionInDeclarationFile2.js

Lines changed: 33 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -29,72 +29,54 @@ const test = new Test();
2929

3030
Test.getTags()
3131
test.tags();
32+
33+
export var noPrivates2 = class {
34+
static getTags() { }
35+
tags() { }
36+
#p = -1
37+
}
3238

3339

3440
//// [emitClassExpressionInDeclarationFile2.js]
35-
"use strict";
36-
var __extends = (this && this.__extends) || (function () {
37-
var extendStatics = function (d, b) {
38-
extendStatics = Object.setPrototypeOf ||
39-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
40-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
41-
return extendStatics(d, b);
42-
};
43-
return function (d, b) {
44-
if (typeof b !== "function" && b !== null)
45-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
46-
extendStatics(d, b);
47-
function __() { this.constructor = d; }
48-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
49-
};
50-
})();
5141
var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) {
5242
if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
5343
return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
5444
};
55-
var _a;
56-
Object.defineProperty(exports, "__esModule", { value: true });
57-
exports.Test = exports.FooItem = exports.noPrivates = void 0;
58-
exports.WithTags = WithTags;
59-
exports.noPrivates = (_a = /** @class */ (function () {
60-
function class_1() {
45+
var _a, _noPrivates2_p, _b;
46+
export var noPrivates = (_a = class {
47+
constructor() {
6148
this.p = 12;
6249
}
63-
class_1.getTags = function () { };
64-
class_1.prototype.tags = function () { };
65-
return class_1;
66-
}()),
50+
static getTags() { }
51+
tags() { }
52+
},
6753
__setFunctionName(_a, "noPrivates"),
6854
_a.ps = -1,
6955
_a);
7056
// altered repro from #15066 to add private property
71-
var FooItem = /** @class */ (function () {
72-
function FooItem() {
57+
export class FooItem {
58+
constructor() {
7359
this.property = "capitalism";
7460
}
75-
FooItem.prototype.foo = function () { };
76-
return FooItem;
77-
}());
78-
exports.FooItem = FooItem;
79-
function WithTags(Base) {
80-
return /** @class */ (function (_super) {
81-
__extends(class_2, _super);
82-
function class_2() {
83-
return _super !== null && _super.apply(this, arguments) || this;
84-
}
85-
class_2.getTags = function () { };
86-
class_2.prototype.tags = function () { };
87-
return class_2;
88-
}(Base));
61+
foo() { }
8962
}
90-
var Test = /** @class */ (function (_super) {
91-
__extends(Test, _super);
92-
function Test() {
93-
return _super !== null && _super.apply(this, arguments) || this;
94-
}
95-
return Test;
96-
}(WithTags(FooItem)));
97-
exports.Test = Test;
98-
var test = new Test();
63+
export function WithTags(Base) {
64+
return class extends Base {
65+
static getTags() { }
66+
tags() { }
67+
};
68+
}
69+
export class Test extends WithTags(FooItem) {
70+
}
71+
const test = new Test();
9972
Test.getTags();
10073
test.tags();
74+
export var noPrivates2 = (_b = class {
75+
constructor() {
76+
_noPrivates2_p.set(this, -1);
77+
}
78+
static getTags() { }
79+
tags() { }
80+
},
81+
_noPrivates2_p = new WeakMap(),
82+
_b);

tests/baselines/reference/emitClassExpressionInDeclarationFile2.symbols

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,16 @@ test.tags();
7575
>test : Symbol(test, Decl(emitClassExpressionInDeclarationFile2.ts, 24, 5))
7676
>tags : Symbol((Anonymous class).tags, Decl(emitClassExpressionInDeclarationFile2.ts, 17, 34))
7777

78+
export var noPrivates2 = class {
79+
>noPrivates2 : Symbol(noPrivates2, Decl(emitClassExpressionInDeclarationFile2.ts, 29, 10))
80+
81+
static getTags() { }
82+
>getTags : Symbol(noPrivates2.getTags, Decl(emitClassExpressionInDeclarationFile2.ts, 29, 32))
83+
84+
tags() { }
85+
>tags : Symbol(noPrivates2.tags, Decl(emitClassExpressionInDeclarationFile2.ts, 30, 24))
86+
87+
#p = -1
88+
>#p : Symbol(noPrivates2.#p, Decl(emitClassExpressionInDeclarationFile2.ts, 31, 14))
89+
}
90+

tests/baselines/reference/emitClassExpressionInDeclarationFile2.types

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,26 @@ test.tags();
116116
>tags : () => void
117117
> : ^^^^^^
118118

119+
export var noPrivates2 = class {
120+
>noPrivates2 : typeof noPrivates2
121+
> : ^^^^^^^^^^^^^^^^^^
122+
>class { static getTags() { } tags() { } #p = -1} : typeof noPrivates2
123+
> : ^^^^^^^^^^^^^^^^^^
124+
125+
static getTags() { }
126+
>getTags : () => void
127+
> : ^^^^^^^^^^
128+
129+
tags() { }
130+
>tags : () => void
131+
> : ^^^^^^^^^^
132+
133+
#p = -1
134+
>#p : number
135+
> : ^^^^^^
136+
>-1 : -1
137+
> : ^^
138+
>1 : 1
139+
> : ^
140+
}
141+

tests/cases/compiler/emitClassExpressionInDeclarationFile2.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// @declaration: true
2+
// @target: ES6
23
export var noPrivates = class {
34
static getTags() { }
45
tags() { }
@@ -27,3 +28,9 @@ const test = new Test();
2728

2829
Test.getTags()
2930
test.tags();
31+
32+
export var noPrivates2 = class {
33+
static getTags() { }
34+
tags() { }
35+
#p = -1
36+
}

0 commit comments

Comments
 (0)