From 301901709336b23ee3a4762855cd516a7229b6d6 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 28 Jan 2016 10:39:54 -0800 Subject: [PATCH 1/2] Emit readonly in declaration files --- src/compiler/declarationEmitter.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/compiler/declarationEmitter.ts b/src/compiler/declarationEmitter.ts index 9a57b4b1a37..7a8db9f90ed 100644 --- a/src/compiler/declarationEmitter.ts +++ b/src/compiler/declarationEmitter.ts @@ -648,6 +648,9 @@ namespace ts { if (node.flags & NodeFlags.Static) { write("static "); } + if (node.flags & NodeFlags.Readonly) { + write("readonly "); + } if (node.flags & NodeFlags.Abstract) { write("abstract "); } @@ -1342,15 +1345,17 @@ namespace ts { const prevEnclosingDeclaration = enclosingDeclaration; enclosingDeclaration = node; - // Construct signature or constructor type write new Signature - if (node.kind === SyntaxKind.ConstructSignature || node.kind === SyntaxKind.ConstructorType) { - write("new "); - } - emitTypeParameters(node.typeParameters); if (node.kind === SyntaxKind.IndexSignature) { + // Index signature can have readonly modifier + emitClassMemberDeclarationFlags(node); write("["); } else { + // Construct signature or constructor type write new Signature + if (node.kind === SyntaxKind.ConstructSignature || node.kind === SyntaxKind.ConstructorType) { + write("new "); + } + emitTypeParameters(node.typeParameters); write("("); } From 371811ab5b795ce0abb2a0c81e96098c09768652 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 28 Jan 2016 10:40:08 -0800 Subject: [PATCH 2/2] Adding test --- .../reference/readonlyInDeclarationFile.js | 83 +++++++++++++++++++ .../readonlyInDeclarationFile.symbols | 76 +++++++++++++++++ .../reference/readonlyInDeclarationFile.types | 83 +++++++++++++++++++ .../compiler/readonlyInDeclarationFile.ts | 36 ++++++++ 4 files changed, 278 insertions(+) create mode 100644 tests/baselines/reference/readonlyInDeclarationFile.js create mode 100644 tests/baselines/reference/readonlyInDeclarationFile.symbols create mode 100644 tests/baselines/reference/readonlyInDeclarationFile.types create mode 100644 tests/cases/compiler/readonlyInDeclarationFile.ts diff --git a/tests/baselines/reference/readonlyInDeclarationFile.js b/tests/baselines/reference/readonlyInDeclarationFile.js new file mode 100644 index 00000000000..d2a20280e0f --- /dev/null +++ b/tests/baselines/reference/readonlyInDeclarationFile.js @@ -0,0 +1,83 @@ +//// [readonlyInDeclarationFile.ts] + +interface Foo { + readonly x: number; + readonly [x: string]: Object; +} + +class C { + protected readonly y: number; + readonly [x: string]: Object; + private static readonly a = "foo"; + protected static readonly b = "foo"; + public static readonly c = "foo"; +} + +var z: { + readonly a: string; + readonly [x: string]: Object; +} + +function f() { + return { + get x() { return 1; }, + get y() { return 1; }, + set y(value) { } + } +} + +function g() { + var x: { + readonly a: string; + readonly [x: string]: Object; + } + return x; +} + +//// [readonlyInDeclarationFile.js] +var C = (function () { + function C() { + } + C.a = "foo"; + C.b = "foo"; + C.c = "foo"; + return C; +}()); +var z; +function f() { + return { + get x() { return 1; }, + get y() { return 1; }, + set y(value) { } + }; +} +function g() { + var x; + return x; +} + + +//// [readonlyInDeclarationFile.d.ts] +interface Foo { + readonly x: number; + readonly [x: string]: Object; +} +declare class C { + protected readonly y: number; + readonly [x: string]: Object; + private static readonly a; + protected static readonly b: string; + static readonly c: string; +} +declare var z: { + readonly a: string; + readonly [x: string]: Object; +}; +declare function f(): { + readonly x: number; + y: number; +}; +declare function g(): { + readonly [x: string]: Object; + readonly a: string; +}; diff --git a/tests/baselines/reference/readonlyInDeclarationFile.symbols b/tests/baselines/reference/readonlyInDeclarationFile.symbols new file mode 100644 index 00000000000..f7bbf19a0ae --- /dev/null +++ b/tests/baselines/reference/readonlyInDeclarationFile.symbols @@ -0,0 +1,76 @@ +=== tests/cases/compiler/readonlyInDeclarationFile.ts === + +interface Foo { +>Foo : Symbol(Foo, Decl(readonlyInDeclarationFile.ts, 0, 0)) + + readonly x: number; +>x : Symbol(x, Decl(readonlyInDeclarationFile.ts, 1, 15)) + + readonly [x: string]: Object; +>x : Symbol(x, Decl(readonlyInDeclarationFile.ts, 3, 14)) +>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +} + +class C { +>C : Symbol(C, Decl(readonlyInDeclarationFile.ts, 4, 1)) + + protected readonly y: number; +>y : Symbol(y, Decl(readonlyInDeclarationFile.ts, 6, 9)) + + readonly [x: string]: Object; +>x : Symbol(x, Decl(readonlyInDeclarationFile.ts, 8, 14)) +>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) + + private static readonly a = "foo"; +>a : Symbol(C.a, Decl(readonlyInDeclarationFile.ts, 8, 33)) + + protected static readonly b = "foo"; +>b : Symbol(C.b, Decl(readonlyInDeclarationFile.ts, 9, 38)) + + public static readonly c = "foo"; +>c : Symbol(C.c, Decl(readonlyInDeclarationFile.ts, 10, 40)) +} + +var z: { +>z : Symbol(z, Decl(readonlyInDeclarationFile.ts, 14, 3)) + + readonly a: string; +>a : Symbol(a, Decl(readonlyInDeclarationFile.ts, 14, 8)) + + readonly [x: string]: Object; +>x : Symbol(x, Decl(readonlyInDeclarationFile.ts, 16, 14)) +>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +} + +function f() { +>f : Symbol(f, Decl(readonlyInDeclarationFile.ts, 17, 1)) + + return { + get x() { return 1; }, +>x : Symbol(x, Decl(readonlyInDeclarationFile.ts, 20, 12)) + + get y() { return 1; }, +>y : Symbol(y, Decl(readonlyInDeclarationFile.ts, 21, 30), Decl(readonlyInDeclarationFile.ts, 22, 30)) + + set y(value) { } +>y : Symbol(y, Decl(readonlyInDeclarationFile.ts, 21, 30), Decl(readonlyInDeclarationFile.ts, 22, 30)) +>value : Symbol(value, Decl(readonlyInDeclarationFile.ts, 23, 14)) + } +} + +function g() { +>g : Symbol(g, Decl(readonlyInDeclarationFile.ts, 25, 1)) + + var x: { +>x : Symbol(x, Decl(readonlyInDeclarationFile.ts, 28, 7)) + + readonly a: string; +>a : Symbol(a, Decl(readonlyInDeclarationFile.ts, 28, 12)) + + readonly [x: string]: Object; +>x : Symbol(x, Decl(readonlyInDeclarationFile.ts, 30, 18)) +>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) + } + return x; +>x : Symbol(x, Decl(readonlyInDeclarationFile.ts, 28, 7)) +} diff --git a/tests/baselines/reference/readonlyInDeclarationFile.types b/tests/baselines/reference/readonlyInDeclarationFile.types new file mode 100644 index 00000000000..9b2f9d51d99 --- /dev/null +++ b/tests/baselines/reference/readonlyInDeclarationFile.types @@ -0,0 +1,83 @@ +=== tests/cases/compiler/readonlyInDeclarationFile.ts === + +interface Foo { +>Foo : Foo + + readonly x: number; +>x : number + + readonly [x: string]: Object; +>x : string +>Object : Object +} + +class C { +>C : C + + protected readonly y: number; +>y : number + + readonly [x: string]: Object; +>x : string +>Object : Object + + private static readonly a = "foo"; +>a : string +>"foo" : string + + protected static readonly b = "foo"; +>b : string +>"foo" : string + + public static readonly c = "foo"; +>c : string +>"foo" : string +} + +var z: { +>z : { readonly [x: string]: Object; readonly a: string; } + + readonly a: string; +>a : string + + readonly [x: string]: Object; +>x : string +>Object : Object +} + +function f() { +>f : () => { readonly x: number; y: number; } + + return { +>{ get x() { return 1; }, get y() { return 1; }, set y(value) { } } : { readonly x: number; y: number; } + + get x() { return 1; }, +>x : number +>1 : number + + get y() { return 1; }, +>y : number +>1 : number + + set y(value) { } +>y : number +>value : number + } +} + +function g() { +>g : () => { readonly [x: string]: Object; readonly a: string; } + + var x: { +>x : { readonly [x: string]: Object; readonly a: string; } + + readonly a: string; +>a : string + + readonly [x: string]: Object; +>x : string +>Object : Object + } + return x; +>x : { readonly [x: string]: Object; readonly a: string; } +} diff --git a/tests/cases/compiler/readonlyInDeclarationFile.ts b/tests/cases/compiler/readonlyInDeclarationFile.ts new file mode 100644 index 00000000000..99241b8c0c0 --- /dev/null +++ b/tests/cases/compiler/readonlyInDeclarationFile.ts @@ -0,0 +1,36 @@ +// @target: es5 +// @declaration: true + +interface Foo { + readonly x: number; + readonly [x: string]: Object; +} + +class C { + protected readonly y: number; + readonly [x: string]: Object; + private static readonly a = "foo"; + protected static readonly b = "foo"; + public static readonly c = "foo"; +} + +var z: { + readonly a: string; + readonly [x: string]: Object; +} + +function f() { + return { + get x() { return 1; }, + get y() { return 1; }, + set y(value) { } + } +} + +function g() { + var x: { + readonly a: string; + readonly [x: string]: Object; + } + return x; +} \ No newline at end of file