Skip to content

Commit 69c7e67

Browse files
authored
Check privateness when emittign readonly/const props (microsoft#26920)
1 parent 1e2fb9f commit 69c7e67

File tree

5 files changed

+77
-2
lines changed

5 files changed

+77
-2
lines changed

src/compiler/transformers/declarations.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,12 +1324,13 @@ namespace ts {
13241324
}
13251325

13261326
type CanHaveLiteralInitializer = VariableDeclaration | PropertyDeclaration | PropertySignature | ParameterDeclaration;
1327-
function canHaveLiteralInitializer(node: Node): node is CanHaveLiteralInitializer {
1327+
function canHaveLiteralInitializer(node: Node): boolean {
13281328
switch (node.kind) {
1329-
case SyntaxKind.VariableDeclaration:
13301329
case SyntaxKind.PropertyDeclaration:
13311330
case SyntaxKind.PropertySignature:
1331+
return !hasModifier(node, ModifierFlags.Private);
13321332
case SyntaxKind.Parameter:
1333+
case SyntaxKind.VariableDeclaration:
13331334
return true;
13341335
}
13351336
return false;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//// [declarationEmitPrivateReadonlyLiterals.ts]
2+
class Foo {
3+
private static readonly A = "a";
4+
private readonly B = "b";
5+
private static readonly C = 42;
6+
private readonly D = 42;
7+
}
8+
9+
10+
//// [declarationEmitPrivateReadonlyLiterals.js]
11+
var Foo = /** @class */ (function () {
12+
function Foo() {
13+
this.B = "b";
14+
this.D = 42;
15+
}
16+
Foo.A = "a";
17+
Foo.C = 42;
18+
return Foo;
19+
}());
20+
21+
22+
//// [declarationEmitPrivateReadonlyLiterals.d.ts]
23+
declare class Foo {
24+
private static readonly A;
25+
private readonly B;
26+
private static readonly C;
27+
private readonly D;
28+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/compiler/declarationEmitPrivateReadonlyLiterals.ts ===
2+
class Foo {
3+
>Foo : Symbol(Foo, Decl(declarationEmitPrivateReadonlyLiterals.ts, 0, 0))
4+
5+
private static readonly A = "a";
6+
>A : Symbol(Foo.A, Decl(declarationEmitPrivateReadonlyLiterals.ts, 0, 11))
7+
8+
private readonly B = "b";
9+
>B : Symbol(Foo.B, Decl(declarationEmitPrivateReadonlyLiterals.ts, 1, 36))
10+
11+
private static readonly C = 42;
12+
>C : Symbol(Foo.C, Decl(declarationEmitPrivateReadonlyLiterals.ts, 2, 29))
13+
14+
private readonly D = 42;
15+
>D : Symbol(Foo.D, Decl(declarationEmitPrivateReadonlyLiterals.ts, 3, 35))
16+
}
17+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
=== tests/cases/compiler/declarationEmitPrivateReadonlyLiterals.ts ===
2+
class Foo {
3+
>Foo : Foo
4+
5+
private static readonly A = "a";
6+
>A : "a"
7+
>"a" : "a"
8+
9+
private readonly B = "b";
10+
>B : "b"
11+
>"b" : "b"
12+
13+
private static readonly C = 42;
14+
>C : 42
15+
>42 : 42
16+
17+
private readonly D = 42;
18+
>D : 42
19+
>42 : 42
20+
}
21+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @declaration: true
2+
3+
class Foo {
4+
private static readonly A = "a";
5+
private readonly B = "b";
6+
private static readonly C = 42;
7+
private readonly D = 42;
8+
}

0 commit comments

Comments
 (0)