Merge pull request #6697 from Microsoft/readonlyInDeclarationFiles

Readonly in declaration files
This commit is contained in:
Anders Hejlsberg 2016-01-28 10:57:38 -08:00
commit fbf1477970
5 changed files with 288 additions and 5 deletions

View File

@ -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("(");
}

View File

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

View File

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

View File

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

View File

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