Updating PR as per feedback.

OneType|null is again treated as object instead of simplifying it
This commit is contained in:
Sheetal Nandi
2017-04-14 11:34:15 -07:00
parent b292077bc5
commit 0eaa8eb3ab
10 changed files with 36 additions and 53 deletions
+18 -22
View File
@@ -18625,33 +18625,36 @@ namespace ts {
}
}
/**
* This function marks the type used for metadata decorator as referenced if it is import
* from external module.
* This is different from markTypeNodeAsReferenced because it tries to simplify type nodes in
* union and intersection type
* @param node
*/
function markDecoratorMedataDataTypeNodeAsReferenced(node: TypeNode): void {
const entityNameOrToken = getEntityNameForDecoratoryMetadata(node);
if (entityNameOrToken && isEntityName(entityNameOrToken)) {
markEntityNameOrEntityExpressionAsReference(entityNameOrToken);
const entityName = getEntityNameForDecoratorMetadata(node);
if (entityName && isEntityName(entityName)) {
markEntityNameOrEntityExpressionAsReference(entityName);
}
}
type voidUndefinedNullOrNeverTypeNode = Token<SyntaxKind.VoidKeyword | SyntaxKind.UndefinedKeyword | SyntaxKind.NullKeyword | SyntaxKind.NeverKeyword>;
function getEntityNameForDecoratoryMetadata(node: TypeNode): EntityName | voidUndefinedNullOrNeverTypeNode {
function getEntityNameForDecoratorMetadata(node: TypeNode): EntityName {
if (node) {
switch (node.kind) {
case SyntaxKind.IntersectionType:
case SyntaxKind.UnionType:
let commonEntityName: EntityName | voidUndefinedNullOrNeverTypeNode;
let commonEntityName: EntityName;
for (const typeNode of (<UnionOrIntersectionTypeNode>node).types) {
const individualEntityName = getEntityNameForDecoratoryMetadata(typeNode);
const individualEntityName = getEntityNameForDecoratorMetadata(typeNode);
if (!individualEntityName) {
// Individual is something like string number
// So it would be serialized to either that type or object
// 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) {
if (commonEntityName) {
// 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
@@ -18662,24 +18665,17 @@ namespace ts {
return undefined;
}
}
else if (!isCommonEntityName) {
else {
commonEntityName = individualEntityName;
}
}
return commonEntityName;
case SyntaxKind.ParenthesizedType:
return getEntityNameForDecoratoryMetadata((<ParenthesizedTypeNode>node).type);
return getEntityNameForDecoratorMetadata((<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;
}
}
}
+3 -9
View File
@@ -1761,25 +1761,19 @@ namespace ts {
}
function serializeUnionOrIntersectionType(node: UnionOrIntersectionTypeNode): SerializedTypeNode {
// Note when updating logic here also update getEntityNameForDecoratoryMetadata
// Note when updating logic here also update getEntityNameForDecoratorMetadata
// so that aliases can be marked as referenced
let serializedUnion: SerializedTypeNode;
for (const typeNode of node.types) {
const serializedIndividual = serializeTypeNode(typeNode);
if (isVoidExpression(serializedIndividual)) {
// If we dont have any other type already set, set the initial type
if (!serializedUnion) {
serializedUnion = serializedIndividual;
}
}
else if (isIdentifier(serializedIndividual) && serializedIndividual.text === "Object") {
if (isIdentifier(serializedIndividual) && serializedIndividual.text === "Object") {
// One of the individual is global object, return immediately
return serializedIndividual;
}
// If there exists union that is not void 0 expression, check if the the common type is identifier.
// anything more complex and we will just default to Object
else if (serializedUnion && !isVoidExpression(serializedUnion)) {
else if (serializedUnion) {
// Different types
if (!isIdentifier(serializedUnion) ||
!isIdentifier(serializedIndividual) ||
-4
View File
@@ -3566,10 +3566,6 @@ namespace ts {
return node.kind === SyntaxKind.Identifier;
}
export function isVoidExpression(node: Node): node is VoidExpression {
return node.kind === SyntaxKind.VoidExpression;
}
export function isGeneratedIdentifier(node: Node): node is GeneratedIdentifier {
// Using `>` here catches both `GeneratedIdentifierKind.None` and `undefined`.
return isIdentifier(node) && node.autoGenerateKind > GeneratedIdentifierKind.None;
@@ -1,7 +1,6 @@
//// [tests/cases/compiler/metadataOfClassFromAlias.ts] ////
//// [auxiliry.ts]
export class SomeClass {
field: string;
}
@@ -17,6 +16,7 @@ export class ClassA {
//// [auxiliry.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var SomeClass = (function () {
function SomeClass() {
}
@@ -34,7 +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");
Object.defineProperty(exports, "__esModule", { value: true });
function annotation() {
return function (target) { };
}
@@ -45,6 +45,6 @@ var ClassA = (function () {
}());
__decorate([
annotation(),
__metadata("design:type", auxiliry_1.SomeClass)
__metadata("design:type", Object)
], ClassA.prototype, "array", void 0);
exports.ClassA = ClassA;
@@ -1,10 +1,9 @@
=== 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))
>field : Symbol(SomeClass.field, Decl(auxiliry.ts, 0, 24))
}
=== tests/cases/compiler/test.ts ===
@@ -1,5 +1,4 @@
=== tests/cases/compiler/auxiliry.ts ===
export class SomeClass {
>SomeClass : SomeClass
@@ -1,7 +1,6 @@
//// [tests/cases/compiler/metadataOfClassFromAlias2.ts] ////
//// [auxiliry.ts]
export class SomeClass {
field: string;
}
@@ -17,6 +16,7 @@ export class ClassA {
//// [auxiliry.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var SomeClass = (function () {
function SomeClass() {
}
@@ -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);
};
Object.defineProperty(exports, "__esModule", { value: true });
function annotation() {
return function (target) { };
}
@@ -1,10 +1,9 @@
=== 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))
>field : Symbol(SomeClass.field, Decl(auxiliry.ts, 0, 24))
}
=== tests/cases/compiler/test.ts ===
@@ -1,5 +1,4 @@
=== tests/cases/compiler/auxiliry.ts ===
export class SomeClass {
>SomeClass : SomeClass
@@ -65,15 +65,15 @@ var B = (function () {
}());
__decorate([
PropDeco,
__metadata("design:type", String)
__metadata("design:type", Object)
], B.prototype, "x");
__decorate([
PropDeco,
__metadata("design:type", Boolean)
__metadata("design:type", Object)
], B.prototype, "y");
__decorate([
PropDeco,
__metadata("design:type", String)
__metadata("design:type", Object)
], B.prototype, "z");
__decorate([
PropDeco,
@@ -89,11 +89,11 @@ __decorate([
], B.prototype, "c");
__decorate([
PropDeco,
__metadata("design:type", void 0)
__metadata("design:type", Object)
], B.prototype, "d");
__decorate([
PropDeco,
__metadata("design:type", typeof Symbol === "function" ? Symbol : Object)
__metadata("design:type", Object)
], B.prototype, "e");
__decorate([
PropDeco,
@@ -101,13 +101,13 @@ __decorate([
], B.prototype, "f");
__decorate([
PropDeco,
__metadata("design:type", A)
__metadata("design:type", Object)
], B.prototype, "g");
__decorate([
PropDeco,
__metadata("design:type", B)
__metadata("design:type", Object)
], B.prototype, "h");
__decorate([
PropDeco,
__metadata("design:type", typeof Symbol === "function" ? Symbol : Object)
__metadata("design:type", Object)
], B.prototype, "j");