Skip to content

Commit 4641004

Browse files
authored
Improve error message for unserializable private and protected class members (#59229)
1 parent e450c46 commit 4641004

File tree

78 files changed

+3360
-892
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+3360
-892
lines changed

src/compiler/diagnosticMessages.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4212,7 +4212,7 @@
42124212
"category": "Error",
42134213
"code": 4092
42144214
},
4215-
"Property '{0}' of exported class expression may not be private or protected.": {
4215+
"Property '{0}' of exported anonymous class type may not be private or protected.": {
42164216
"category": "Error",
42174217
"code": 4094
42184218
},

src/compiler/transformers/declarations.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,10 @@ export function transformDeclarations(context: TransformationContext) {
357357
function reportPrivateInBaseOfClassExpression(propertyName: string) {
358358
if (errorNameNode || errorFallbackNode) {
359359
context.addDiagnostic(
360-
createDiagnosticForNode((errorNameNode || errorFallbackNode)!, Diagnostics.Property_0_of_exported_class_expression_may_not_be_private_or_protected, propertyName),
360+
addRelatedInfo(
361+
createDiagnosticForNode((errorNameNode || errorFallbackNode)!, Diagnostics.Property_0_of_exported_anonymous_class_type_may_not_be_private_or_protected, propertyName),
362+
...(isVariableDeclaration((errorNameNode || errorFallbackNode)!.parent) ? [createDiagnosticForNode((errorNameNode || errorFallbackNode)!, Diagnostics.Add_a_type_annotation_to_the_variable_0, errorDeclarationNameWithFallback())] : []),
363+
),
361364
);
362365
}
363366
}

tests/baselines/reference/declarationEmitMixinPrivateProtected.errors.txt

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
another.ts(11,1): error TS4094: Property '_assertIsStripped' of exported class expression may not be private or protected.
2-
another.ts(11,1): error TS4094: Property '_onDispose' of exported class expression may not be private or protected.
3-
first.ts(12,1): error TS4094: Property '_assertIsStripped' of exported class expression may not be private or protected.
4-
first.ts(12,1): error TS4094: Property '_onDispose' of exported class expression may not be private or protected.
5-
first.ts(13,14): error TS4094: Property '_assertIsStripped' of exported class expression may not be private or protected.
6-
first.ts(13,14): error TS4094: Property '_onDispose' of exported class expression may not be private or protected.
1+
another.ts(11,1): error TS4094: Property '_assertIsStripped' of exported anonymous class type may not be private or protected.
2+
another.ts(11,1): error TS4094: Property '_onDispose' of exported anonymous class type may not be private or protected.
3+
first.ts(12,1): error TS4094: Property '_assertIsStripped' of exported anonymous class type may not be private or protected.
4+
first.ts(12,1): error TS4094: Property '_onDispose' of exported anonymous class type may not be private or protected.
5+
first.ts(13,14): error TS4094: Property '_assertIsStripped' of exported anonymous class type may not be private or protected.
6+
first.ts(13,14): error TS4094: Property '_onDispose' of exported anonymous class type may not be private or protected.
77

88

99
==== first.ts (4 errors) ====
@@ -20,14 +20,14 @@ first.ts(13,14): error TS4094: Property '_onDispose' of exported class expressio
2020
// No error, but definition is wrong.
2121
export default mix(DisposableMixin);
2222
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23-
!!! error TS4094: Property '_assertIsStripped' of exported class expression may not be private or protected.
23+
!!! error TS4094: Property '_assertIsStripped' of exported anonymous class type may not be private or protected.
2424
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25-
!!! error TS4094: Property '_onDispose' of exported class expression may not be private or protected.
25+
!!! error TS4094: Property '_onDispose' of exported anonymous class type may not be private or protected.
2626
export class Monitor extends mix(DisposableMixin) {
2727
~~~~~~~
28-
!!! error TS4094: Property '_assertIsStripped' of exported class expression may not be private or protected.
28+
!!! error TS4094: Property '_assertIsStripped' of exported anonymous class type may not be private or protected.
2929
~~~~~~~
30-
!!! error TS4094: Property '_onDispose' of exported class expression may not be private or protected.
30+
!!! error TS4094: Property '_onDispose' of exported anonymous class type may not be private or protected.
3131
protected _onDispose() {
3232
}
3333
}
@@ -45,9 +45,9 @@ first.ts(13,14): error TS4094: Property '_onDispose' of exported class expressio
4545

4646
export default class extends mix(DisposableMixin) {
4747
~~~~~~
48-
!!! error TS4094: Property '_assertIsStripped' of exported class expression may not be private or protected.
48+
!!! error TS4094: Property '_assertIsStripped' of exported anonymous class type may not be private or protected.
4949
~~~~~~
50-
!!! error TS4094: Property '_onDispose' of exported class expression may not be private or protected.
50+
!!! error TS4094: Property '_onDispose' of exported anonymous class type may not be private or protected.
5151
protected _onDispose() {
5252
}
5353
}

tests/baselines/reference/emitClassExpressionInDeclarationFile2.errors.txt

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
emitClassExpressionInDeclarationFile2.ts(1,12): error TS4094: Property 'p' of exported class expression may not be private or protected.
2-
emitClassExpressionInDeclarationFile2.ts(1,12): error TS4094: Property 'ps' of exported class expression may not be private or protected.
3-
emitClassExpressionInDeclarationFile2.ts(16,17): error TS4094: Property 'property' of exported class expression may not be private or protected.
4-
emitClassExpressionInDeclarationFile2.ts(23,14): error TS4094: Property 'property' of exported class expression may not be private or protected.
1+
emitClassExpressionInDeclarationFile2.ts(1,12): error TS4094: Property 'p' of exported anonymous class type may not be private or protected.
2+
emitClassExpressionInDeclarationFile2.ts(1,12): error TS4094: Property 'ps' of exported anonymous class type may not be private or protected.
3+
emitClassExpressionInDeclarationFile2.ts(16,17): error TS4094: Property 'property' of exported anonymous class type may not be private or protected.
4+
emitClassExpressionInDeclarationFile2.ts(23,14): error TS4094: Property 'property' of exported anonymous class type may not be private or protected.
55

66

77
==== emitClassExpressionInDeclarationFile2.ts (4 errors) ====
88
export var noPrivates = class {
99
~~~~~~~~~~
10-
!!! error TS4094: Property 'p' of exported class expression may not be private or protected.
10+
!!! error TS4094: Property 'p' of exported anonymous class type may not be private or protected.
11+
!!! related TS9027 emitClassExpressionInDeclarationFile2.ts:1:12: Add a type annotation to the variable noPrivates.
1112
~~~~~~~~~~
12-
!!! error TS4094: Property 'ps' of exported class expression may not be private or protected.
13+
!!! error TS4094: Property 'ps' of exported anonymous class type may not be private or protected.
14+
!!! related TS9027 emitClassExpressionInDeclarationFile2.ts:1:12: Add a type annotation to the variable noPrivates.
1315
static getTags() { }
1416
tags() { }
1517
private static ps = -1
@@ -26,7 +28,7 @@ emitClassExpressionInDeclarationFile2.ts(23,14): error TS4094: Property 'propert
2628
export type Constructor<T> = new(...args: any[]) => T;
2729
export function WithTags<T extends Constructor<FooItem>>(Base: T) {
2830
~~~~~~~~
29-
!!! error TS4094: Property 'property' of exported class expression may not be private or protected.
31+
!!! error TS4094: Property 'property' of exported anonymous class type may not be private or protected.
3032
return class extends Base {
3133
static getTags(): void { }
3234
tags(): void { }
@@ -35,7 +37,7 @@ emitClassExpressionInDeclarationFile2.ts(23,14): error TS4094: Property 'propert
3537

3638
export class Test extends WithTags(FooItem) {}
3739
~~~~
38-
!!! error TS4094: Property 'property' of exported class expression may not be private or protected.
40+
!!! error TS4094: Property 'property' of exported anonymous class type may not be private or protected.
3941

4042
const test = new Test();
4143

0 commit comments

Comments
 (0)