Skip to content

Commit

Permalink
Check privateness when emittign readonly/const props (microsoft#26920)
Browse files Browse the repository at this point in the history
  • Loading branch information
weswigham authored Sep 5, 2018
1 parent 1e2fb9f commit 69c7e67
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/compiler/transformers/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1324,12 +1324,13 @@ namespace ts {
}

type CanHaveLiteralInitializer = VariableDeclaration | PropertyDeclaration | PropertySignature | ParameterDeclaration;
function canHaveLiteralInitializer(node: Node): node is CanHaveLiteralInitializer {
function canHaveLiteralInitializer(node: Node): boolean {
switch (node.kind) {
case SyntaxKind.VariableDeclaration:
case SyntaxKind.PropertyDeclaration:
case SyntaxKind.PropertySignature:
return !hasModifier(node, ModifierFlags.Private);
case SyntaxKind.Parameter:
case SyntaxKind.VariableDeclaration:
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//// [declarationEmitPrivateReadonlyLiterals.ts]
class Foo {
private static readonly A = "a";
private readonly B = "b";
private static readonly C = 42;
private readonly D = 42;
}


//// [declarationEmitPrivateReadonlyLiterals.js]
var Foo = /** @class */ (function () {
function Foo() {
this.B = "b";
this.D = 42;
}
Foo.A = "a";
Foo.C = 42;
return Foo;
}());


//// [declarationEmitPrivateReadonlyLiterals.d.ts]
declare class Foo {
private static readonly A;
private readonly B;
private static readonly C;
private readonly D;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
=== tests/cases/compiler/declarationEmitPrivateReadonlyLiterals.ts ===
class Foo {
>Foo : Symbol(Foo, Decl(declarationEmitPrivateReadonlyLiterals.ts, 0, 0))

private static readonly A = "a";
>A : Symbol(Foo.A, Decl(declarationEmitPrivateReadonlyLiterals.ts, 0, 11))

private readonly B = "b";
>B : Symbol(Foo.B, Decl(declarationEmitPrivateReadonlyLiterals.ts, 1, 36))

private static readonly C = 42;
>C : Symbol(Foo.C, Decl(declarationEmitPrivateReadonlyLiterals.ts, 2, 29))

private readonly D = 42;
>D : Symbol(Foo.D, Decl(declarationEmitPrivateReadonlyLiterals.ts, 3, 35))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
=== tests/cases/compiler/declarationEmitPrivateReadonlyLiterals.ts ===
class Foo {
>Foo : Foo

private static readonly A = "a";
>A : "a"
>"a" : "a"

private readonly B = "b";
>B : "b"
>"b" : "b"

private static readonly C = 42;
>C : 42
>42 : 42

private readonly D = 42;
>D : 42
>42 : 42
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @declaration: true

class Foo {
private static readonly A = "a";
private readonly B = "b";
private static readonly C = 42;
private readonly D = 42;
}

0 comments on commit 69c7e67

Please sign in to comment.