From 5f78d39662646ad3ba4f8af435b0ffbcf161f8cf Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Tue, 1 Sep 2015 13:46:44 -0700 Subject: [PATCH] If Type annotation is missing, emit design:Type as Object Fixes #4549 --- src/compiler/emitter.ts | 90 +++++++++---------- .../decoratorMetadataOnInferredType.js | 39 ++++++++ .../decoratorMetadataOnInferredType.symbols | 38 ++++++++ .../decoratorMetadataOnInferredType.types | 41 +++++++++ .../decoratorMetadataOnInferredType.ts | 21 +++++ 5 files changed, 183 insertions(+), 46 deletions(-) create mode 100644 tests/baselines/reference/decoratorMetadataOnInferredType.js create mode 100644 tests/baselines/reference/decoratorMetadataOnInferredType.symbols create mode 100644 tests/baselines/reference/decoratorMetadataOnInferredType.types create mode 100644 tests/cases/compiler/decoratorMetadataOnInferredType.ts diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index eeaab6d212357..d84b7b70ada36 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -4939,63 +4939,61 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi } function emitSerializedTypeNode(node: TypeNode) { - if (!node) { - return; - } - - switch (node.kind) { - case SyntaxKind.VoidKeyword: - write("void 0"); - return; + if (node) { - case SyntaxKind.ParenthesizedType: - emitSerializedTypeNode((node).type); - return; + switch (node.kind) { + case SyntaxKind.VoidKeyword: + write("void 0"); + return; - case SyntaxKind.FunctionType: - case SyntaxKind.ConstructorType: - write("Function"); - return; + case SyntaxKind.ParenthesizedType: + emitSerializedTypeNode((node).type); + return; - case SyntaxKind.ArrayType: - case SyntaxKind.TupleType: - write("Array"); - return; + case SyntaxKind.FunctionType: + case SyntaxKind.ConstructorType: + write("Function"); + return; - case SyntaxKind.TypePredicate: - case SyntaxKind.BooleanKeyword: - write("Boolean"); - return; + case SyntaxKind.ArrayType: + case SyntaxKind.TupleType: + write("Array"); + return; - case SyntaxKind.StringKeyword: - case SyntaxKind.StringLiteral: - write("String"); - return; + case SyntaxKind.TypePredicate: + case SyntaxKind.BooleanKeyword: + write("Boolean"); + return; - case SyntaxKind.NumberKeyword: - write("Number"); - return; + case SyntaxKind.StringKeyword: + case SyntaxKind.StringLiteral: + write("String"); + return; - case SyntaxKind.SymbolKeyword: - write("Symbol"); - return; + case SyntaxKind.NumberKeyword: + write("Number"); + return; - case SyntaxKind.TypeReference: - emitSerializedTypeReferenceNode(node); - return; + case SyntaxKind.SymbolKeyword: + write("Symbol"); + return; - case SyntaxKind.TypeQuery: - case SyntaxKind.TypeLiteral: - case SyntaxKind.UnionType: - case SyntaxKind.IntersectionType: - case SyntaxKind.AnyKeyword: - break; + case SyntaxKind.TypeReference: + emitSerializedTypeReferenceNode(node); + return; - default: - Debug.fail("Cannot serialize unexpected type node."); - break; - } + case SyntaxKind.TypeQuery: + case SyntaxKind.TypeLiteral: + case SyntaxKind.UnionType: + case SyntaxKind.IntersectionType: + case SyntaxKind.AnyKeyword: + break; + default: + Debug.fail("Cannot serialize unexpected type node."); + break; + } + } write("Object"); } diff --git a/tests/baselines/reference/decoratorMetadataOnInferredType.js b/tests/baselines/reference/decoratorMetadataOnInferredType.js new file mode 100644 index 0000000000000..8103dbbdafa6d --- /dev/null +++ b/tests/baselines/reference/decoratorMetadataOnInferredType.js @@ -0,0 +1,39 @@ +//// [decoratorMetadataOnInferredType.ts] + +declare var console: { + log(msg: string): void; +}; + +class A { + constructor() { console.log('new A'); } +} + +function decorator(target: Object, propertyKey: string) { +} + +export class B { + @decorator + x = new A(); +} + + +//// [decoratorMetadataOnInferredType.js] +var A = (function () { + function A() { + console.log('new A'); + } + return A; +})(); +function decorator(target, propertyKey) { +} +var B = (function () { + function B() { + this.x = new A(); + } + __decorate([ + decorator, + __metadata('design:type', Object) + ], B.prototype, "x"); + return B; +})(); +exports.B = B; diff --git a/tests/baselines/reference/decoratorMetadataOnInferredType.symbols b/tests/baselines/reference/decoratorMetadataOnInferredType.symbols new file mode 100644 index 0000000000000..e1507c75e9473 --- /dev/null +++ b/tests/baselines/reference/decoratorMetadataOnInferredType.symbols @@ -0,0 +1,38 @@ +=== tests/cases/compiler/decoratorMetadataOnInferredType.ts === + +declare var console: { +>console : Symbol(console, Decl(decoratorMetadataOnInferredType.ts, 1, 11)) + + log(msg: string): void; +>log : Symbol(log, Decl(decoratorMetadataOnInferredType.ts, 1, 22)) +>msg : Symbol(msg, Decl(decoratorMetadataOnInferredType.ts, 2, 8)) + +}; + +class A { +>A : Symbol(A, Decl(decoratorMetadataOnInferredType.ts, 3, 2)) + + constructor() { console.log('new A'); } +>console.log : Symbol(log, Decl(decoratorMetadataOnInferredType.ts, 1, 22)) +>console : Symbol(console, Decl(decoratorMetadataOnInferredType.ts, 1, 11)) +>log : Symbol(log, Decl(decoratorMetadataOnInferredType.ts, 1, 22)) +} + +function decorator(target: Object, propertyKey: string) { +>decorator : Symbol(decorator, Decl(decoratorMetadataOnInferredType.ts, 7, 1)) +>target : Symbol(target, Decl(decoratorMetadataOnInferredType.ts, 9, 19)) +>Object : Symbol(Object, Decl(lib.d.ts, 92, 1), Decl(lib.d.ts, 223, 11)) +>propertyKey : Symbol(propertyKey, Decl(decoratorMetadataOnInferredType.ts, 9, 34)) +} + +export class B { +>B : Symbol(B, Decl(decoratorMetadataOnInferredType.ts, 10, 1)) + + @decorator +>decorator : Symbol(decorator, Decl(decoratorMetadataOnInferredType.ts, 7, 1)) + + x = new A(); +>x : Symbol(x, Decl(decoratorMetadataOnInferredType.ts, 12, 16)) +>A : Symbol(A, Decl(decoratorMetadataOnInferredType.ts, 3, 2)) +} + diff --git a/tests/baselines/reference/decoratorMetadataOnInferredType.types b/tests/baselines/reference/decoratorMetadataOnInferredType.types new file mode 100644 index 0000000000000..41c9334244f67 --- /dev/null +++ b/tests/baselines/reference/decoratorMetadataOnInferredType.types @@ -0,0 +1,41 @@ +=== tests/cases/compiler/decoratorMetadataOnInferredType.ts === + +declare var console: { +>console : { log(msg: string): void; } + + log(msg: string): void; +>log : (msg: string) => void +>msg : string + +}; + +class A { +>A : A + + constructor() { console.log('new A'); } +>console.log('new A') : void +>console.log : (msg: string) => void +>console : { log(msg: string): void; } +>log : (msg: string) => void +>'new A' : string +} + +function decorator(target: Object, propertyKey: string) { +>decorator : (target: Object, propertyKey: string) => void +>target : Object +>Object : Object +>propertyKey : string +} + +export class B { +>B : B + + @decorator +>decorator : (target: Object, propertyKey: string) => void + + x = new A(); +>x : A +>new A() : A +>A : typeof A +} + diff --git a/tests/cases/compiler/decoratorMetadataOnInferredType.ts b/tests/cases/compiler/decoratorMetadataOnInferredType.ts new file mode 100644 index 0000000000000..170afe3b3e5b0 --- /dev/null +++ b/tests/cases/compiler/decoratorMetadataOnInferredType.ts @@ -0,0 +1,21 @@ +// @noemithelpers: true +// @experimentaldecorators: true +// @emitdecoratormetadata: true +// @target: es5 +// @module: commonjs + +declare var console: { + log(msg: string): void; +}; + +class A { + constructor() { console.log('new A'); } +} + +function decorator(target: Object, propertyKey: string) { +} + +export class B { + @decorator + x = new A(); +}