Check privateness when emittign readonly/const props (#26920)

This commit is contained in:
Wesley Wigham 2018-09-05 13:49:38 -07:00 committed by GitHub
parent 1e2fb9f0ae
commit 69c7e67c88
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 77 additions and 2 deletions

View file

@ -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;

View file

@ -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;
}

View file

@ -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))
}

View file

@ -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
}

View file

@ -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;
}