Mark as referenced aliases in Union that will get emitted as part of decorator metadaa

Fixes #13449
This commit is contained in:
Sheetal Nandi 2017-01-17 11:20:50 -08:00
parent b345a781d7
commit 839dde3cf2
7 changed files with 197 additions and 6 deletions

View File

@ -16732,7 +16732,10 @@ namespace ts {
* marked as referenced to prevent import elision.
*/
function markTypeNodeAsReferenced(node: TypeNode) {
const typeName = node && getEntityNameFromTypeNode(node);
markEntityNameOrEntityExpressionAsReference(node && getEntityNameFromTypeNode(node));
}
function markEntityNameOrEntityExpressionAsReference(typeName: EntityNameOrEntityNameExpression) {
const rootName = typeName && getFirstIdentifier(typeName);
const rootSymbol = rootName && resolveName(rootName, rootName.text, (typeName.kind === SyntaxKind.Identifier ? SymbolFlags.Type : SymbolFlags.Namespace) | SymbolFlags.Alias, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined);
if (rootSymbol
@ -16743,6 +16746,65 @@ namespace ts {
}
}
function markDecoratorMedataDataTypeNodeAsReferenced(node: TypeNode): void {
const entityNameOrToken = getEntityNameForDecoratoryMetadata(node);
if (entityNameOrToken && isEntityName(entityNameOrToken)) {
markEntityNameOrEntityExpressionAsReference(entityNameOrToken);
}
}
type voidUndefinedNullOrNeverTypeNode = Token<SyntaxKind.VoidKeyword | SyntaxKind.UndefinedKeyword | SyntaxKind.NullKeyword | SyntaxKind.NeverKeyword>;
function getEntityNameForDecoratoryMetadata(node: TypeNode): EntityName | voidUndefinedNullOrNeverTypeNode {
if (node) {
switch (node.kind) {
case SyntaxKind.IntersectionType:
case SyntaxKind.UnionType:
let commonEntityName: EntityName | voidUndefinedNullOrNeverTypeNode;
for (const typeNode of (<UnionOrIntersectionTypeNode>node).types) {
const individualEntityName = getEntityNameForDecoratoryMetadata(typeNode);
if (!individualEntityName) {
// Individual is something like string number
// So it would be serialized to either that type or object
// Safe to return here
return undefined;
}
const isCommonEntityName = commonEntityName && isEntityName(commonEntityName);
const isIndividualEntityName = isEntityName(individualEntityName);
if (isCommonEntityName && isIndividualEntityName) {
// Note this is in sync with the transformation that happens for type node.
// Keep this in sync with serializeUnionOrIntersectionType
// Verify if they refer to same entity and is identifier
// return undefined if they dont match because we would emit object
if (!isIdentifier(commonEntityName) ||
!isIdentifier(individualEntityName) ||
commonEntityName.text !== individualEntityName.text) {
return undefined;
}
}
else if (!isCommonEntityName) {
commonEntityName = individualEntityName;
}
}
return commonEntityName;
case SyntaxKind.ParenthesizedType:
return getEntityNameForDecoratoryMetadata((<ParenthesizedTypeNode>node).type);
case SyntaxKind.TypeReference:
return (<TypeReferenceNode>node).typeName;
case SyntaxKind.VoidKeyword:
case SyntaxKind.UndefinedKeyword:
case SyntaxKind.NullKeyword:
case SyntaxKind.NeverKeyword:
return <voidUndefinedNullOrNeverTypeNode>node;
}
}
}
function getParameterTypeNodeForDecoratorCheck(node: ParameterDeclaration): TypeNode {
return node.dotDotDotToken ? getRestParameterElementType(node.type) : node.type;
}
@ -16778,7 +16840,7 @@ namespace ts {
const constructor = getFirstConstructorWithBody(<ClassDeclaration>node);
if (constructor) {
for (const parameter of constructor.parameters) {
markTypeNodeAsReferenced(getParameterTypeNodeForDecoratorCheck(parameter));
markDecoratorMedataDataTypeNodeAsReferenced(getParameterTypeNodeForDecoratorCheck(parameter));
}
}
break;
@ -16787,17 +16849,17 @@ namespace ts {
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
for (const parameter of (<FunctionLikeDeclaration>node).parameters) {
markTypeNodeAsReferenced(getParameterTypeNodeForDecoratorCheck(parameter));
markDecoratorMedataDataTypeNodeAsReferenced(getParameterTypeNodeForDecoratorCheck(parameter));
}
markTypeNodeAsReferenced((<FunctionLikeDeclaration>node).type);
markDecoratorMedataDataTypeNodeAsReferenced((<FunctionLikeDeclaration>node).type);
break;
case SyntaxKind.PropertyDeclaration:
markTypeNodeAsReferenced(getParameterTypeNodeForDecoratorCheck(<ParameterDeclaration>node));
markDecoratorMedataDataTypeNodeAsReferenced(getParameterTypeNodeForDecoratorCheck(<ParameterDeclaration>node));
break;
case SyntaxKind.Parameter:
markTypeNodeAsReferenced((<PropertyDeclaration>node).type);
markDecoratorMedataDataTypeNodeAsReferenced((<PropertyDeclaration>node).type);
break;
}
}

View File

@ -1739,6 +1739,8 @@ namespace ts {
}
function serializeUnionOrIntersectionType(node: UnionOrIntersectionTypeNode): SerializedTypeNode {
// Note when updating logic here also update getEntityNameForDecoratoryMetadata
// so that aliases can be marked as referenced
let serializedUnion: SerializedTypeNode;
for (const typeNode of node.types) {
const serializedIndividual = serializeTypeNode(typeNode);

View File

@ -34,6 +34,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var auxiliry_1 = require("./auxiliry");
function annotation() {
return function (target) { };
}

View File

@ -0,0 +1,49 @@
//// [tests/cases/compiler/metadataOfClassFromAlias2.ts] ////
//// [auxiliry.ts]
export class SomeClass {
field: string;
}
//// [test.ts]
import { SomeClass } from './auxiliry';
function annotation(): PropertyDecorator {
return (target: any): void => { };
}
export class ClassA {
@annotation() array: SomeClass | null | string;
}
//// [auxiliry.js]
"use strict";
var SomeClass = (function () {
function SomeClass() {
}
return SomeClass;
}());
exports.SomeClass = SomeClass;
//// [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 annotation() {
return function (target) { };
}
var ClassA = (function () {
function ClassA() {
}
return ClassA;
}());
__decorate([
annotation(),
__metadata("design:type", Object)
], ClassA.prototype, "array", void 0);
exports.ClassA = ClassA;

View File

@ -0,0 +1,28 @@
=== tests/cases/compiler/auxiliry.ts ===
export class SomeClass {
>SomeClass : Symbol(SomeClass, Decl(auxiliry.ts, 0, 0))
field: string;
>field : Symbol(SomeClass.field, Decl(auxiliry.ts, 1, 24))
}
=== tests/cases/compiler/test.ts ===
import { SomeClass } from './auxiliry';
>SomeClass : Symbol(SomeClass, Decl(test.ts, 0, 8))
function annotation(): PropertyDecorator {
>annotation : Symbol(annotation, Decl(test.ts, 0, 39))
>PropertyDecorator : Symbol(PropertyDecorator, Decl(lib.d.ts, --, --))
return (target: any): void => { };
>target : Symbol(target, Decl(test.ts, 2, 12))
}
export class ClassA {
>ClassA : Symbol(ClassA, Decl(test.ts, 3, 1))
@annotation() array: SomeClass | null | string;
>annotation : Symbol(annotation, Decl(test.ts, 0, 39))
>array : Symbol(ClassA.array, Decl(test.ts, 4, 21))
>SomeClass : Symbol(SomeClass, Decl(test.ts, 0, 8))
}

View File

@ -0,0 +1,31 @@
=== tests/cases/compiler/auxiliry.ts ===
export class SomeClass {
>SomeClass : SomeClass
field: string;
>field : string
}
=== tests/cases/compiler/test.ts ===
import { SomeClass } from './auxiliry';
>SomeClass : typeof SomeClass
function annotation(): PropertyDecorator {
>annotation : () => PropertyDecorator
>PropertyDecorator : PropertyDecorator
return (target: any): void => { };
>(target: any): void => { } : (target: any) => void
>target : any
}
export class ClassA {
>ClassA : ClassA
@annotation() array: SomeClass | null | string;
>annotation() : PropertyDecorator
>annotation : () => PropertyDecorator
>array : string | SomeClass
>SomeClass : SomeClass
>null : null
}

View File

@ -0,0 +1,18 @@
// @experimentalDecorators: true
// @emitDecoratorMetadata: true
// @target: es5
// @module: commonjs
// @filename: auxiliry.ts
export class SomeClass {
field: string;
}
//@filename: test.ts
import { SomeClass } from './auxiliry';
function annotation(): PropertyDecorator {
return (target: any): void => { };
}
export class ClassA {
@annotation() array: SomeClass | null | string;
}