Merge pull request #13584 from Microsoft/decoratorMetadata

Use the value symbol for decorator purpose only if it is same as type symbol
This commit is contained in:
Sheetal Nandi 2017-01-20 13:46:19 -08:00 committed by GitHub
commit a185ddc885
5 changed files with 110 additions and 9 deletions

View file

@ -20517,18 +20517,21 @@ namespace ts {
function getTypeReferenceSerializationKind(typeName: EntityName, location?: Node): TypeReferenceSerializationKind {
// Resolve the symbol as a value to ensure the type can be reached at runtime during emit.
const valueSymbol = resolveEntityName(typeName, SymbolFlags.Value, /*ignoreErrors*/ true, /*dontResolveAlias*/ false, location);
const globalPromiseSymbol = tryGetGlobalPromiseConstructorSymbol();
if (globalPromiseSymbol && valueSymbol === globalPromiseSymbol) {
return TypeReferenceSerializationKind.Promise;
}
const constructorType = valueSymbol ? getTypeOfSymbol(valueSymbol) : undefined;
if (constructorType && isConstructorType(constructorType)) {
return TypeReferenceSerializationKind.TypeWithConstructSignatureAndValue;
}
// Resolve the symbol as a type so that we can provide a more useful hint for the type serializer.
const typeSymbol = resolveEntityName(typeName, SymbolFlags.Type, /*ignoreErrors*/ true, /*dontResolveAlias*/ false, location);
if (valueSymbol && valueSymbol === typeSymbol) {
const globalPromiseSymbol = tryGetGlobalPromiseConstructorSymbol();
if (globalPromiseSymbol && valueSymbol === globalPromiseSymbol) {
return TypeReferenceSerializationKind.Promise;
}
const constructorType = getTypeOfSymbol(valueSymbol);
if (constructorType && isConstructorType(constructorType)) {
return TypeReferenceSerializationKind.TypeWithConstructSignatureAndValue;
}
}
// We might not be able to resolve type symbol so use unknown type in that case (eg error case)
if (!typeSymbol) {
return TypeReferenceSerializationKind.ObjectType;

View file

@ -0,0 +1,38 @@
//// [tests/cases/compiler/metadataOfEventAlias.ts] ////
//// [event.ts]
export interface Event { title: string };
//// [test.ts]
import { Event } from './event';
function Input(target: any, key: string): void { }
export class SomeClass {
@Input event: Event;
}
//// [event.js]
"use strict";
;
//// [test.js]
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
function Input(target, key) { }
var SomeClass = (function () {
function SomeClass() {
}
return SomeClass;
}());
__decorate([
Input,
__metadata("design:type", Object)
], SomeClass.prototype, "event", void 0);
exports.SomeClass = SomeClass;

View file

@ -0,0 +1,23 @@
=== tests/cases/compiler/event.ts ===
export interface Event { title: string };
>Event : Symbol(Event, Decl(event.ts, 0, 0))
>title : Symbol(Event.title, Decl(event.ts, 1, 24))
=== tests/cases/compiler/test.ts ===
import { Event } from './event';
>Event : Symbol(Event, Decl(test.ts, 0, 8))
function Input(target: any, key: string): void { }
>Input : Symbol(Input, Decl(test.ts, 0, 32))
>target : Symbol(target, Decl(test.ts, 1, 15))
>key : Symbol(key, Decl(test.ts, 1, 27))
export class SomeClass {
>SomeClass : Symbol(SomeClass, Decl(test.ts, 1, 50))
@Input event: Event;
>Input : Symbol(Input, Decl(test.ts, 0, 32))
>event : Symbol(SomeClass.event, Decl(test.ts, 2, 24))
>Event : Symbol(Event, Decl(test.ts, 0, 8))
}

View file

@ -0,0 +1,23 @@
=== tests/cases/compiler/event.ts ===
export interface Event { title: string };
>Event : Event
>title : string
=== tests/cases/compiler/test.ts ===
import { Event } from './event';
>Event : any
function Input(target: any, key: string): void { }
>Input : (target: any, key: string) => void
>target : any
>key : string
export class SomeClass {
>SomeClass : SomeClass
@Input event: Event;
>Input : (target: any, key: string) => void
>event : Event
>Event : Event
}

View file

@ -0,0 +1,14 @@
// @experimentalDecorators: true
// @emitDecoratorMetadata: true
// @target: es5
// @includeBuiltFile: lib.d.ts
// @filename: event.ts
export interface Event { title: string };
// @filename: test.ts
import { Event } from './event';
function Input(target: any, key: string): void { }
export class SomeClass {
@Input event: Event;
}