Skip to content

Commit 0eaa8eb

Browse files
committed
Updating PR as per feedback.
OneType|null is again treated as object instead of simplifying it
1 parent b292077 commit 0eaa8eb

10 files changed

+36
-53
lines changed

src/compiler/checker.ts

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18625,33 +18625,36 @@ namespace ts {
1862518625
}
1862618626
}
1862718627

18628+
/**
18629+
* This function marks the type used for metadata decorator as referenced if it is import
18630+
* from external module.
18631+
* This is different from markTypeNodeAsReferenced because it tries to simplify type nodes in
18632+
* union and intersection type
18633+
* @param node
18634+
*/
1862818635
function markDecoratorMedataDataTypeNodeAsReferenced(node: TypeNode): void {
18629-
const entityNameOrToken = getEntityNameForDecoratoryMetadata(node);
18630-
if (entityNameOrToken && isEntityName(entityNameOrToken)) {
18631-
markEntityNameOrEntityExpressionAsReference(entityNameOrToken);
18636+
const entityName = getEntityNameForDecoratorMetadata(node);
18637+
if (entityName && isEntityName(entityName)) {
18638+
markEntityNameOrEntityExpressionAsReference(entityName);
1863218639
}
1863318640
}
1863418641

18635-
type voidUndefinedNullOrNeverTypeNode = Token<SyntaxKind.VoidKeyword | SyntaxKind.UndefinedKeyword | SyntaxKind.NullKeyword | SyntaxKind.NeverKeyword>;
18636-
18637-
function getEntityNameForDecoratoryMetadata(node: TypeNode): EntityName | voidUndefinedNullOrNeverTypeNode {
18642+
function getEntityNameForDecoratorMetadata(node: TypeNode): EntityName {
1863818643
if (node) {
1863918644
switch (node.kind) {
1864018645
case SyntaxKind.IntersectionType:
1864118646
case SyntaxKind.UnionType:
18642-
let commonEntityName: EntityName | voidUndefinedNullOrNeverTypeNode;
18647+
let commonEntityName: EntityName;
1864318648
for (const typeNode of (<UnionOrIntersectionTypeNode>node).types) {
18644-
const individualEntityName = getEntityNameForDecoratoryMetadata(typeNode);
18649+
const individualEntityName = getEntityNameForDecoratorMetadata(typeNode);
1864518650
if (!individualEntityName) {
18646-
// Individual is something like string number
18647-
// So it would be serialized to either that type or object
18651+
// Individual is something like string number
18652+
// So it would be serialized to either that type or object
1864818653
// Safe to return here
1864918654
return undefined;
1865018655
}
1865118656

18652-
const isCommonEntityName = commonEntityName && isEntityName(commonEntityName);
18653-
const isIndividualEntityName = isEntityName(individualEntityName);
18654-
if (isCommonEntityName && isIndividualEntityName) {
18657+
if (commonEntityName) {
1865518658
// Note this is in sync with the transformation that happens for type node.
1865618659
// Keep this in sync with serializeUnionOrIntersectionType
1865718660
// Verify if they refer to same entity and is identifier
@@ -18662,24 +18665,17 @@ namespace ts {
1866218665
return undefined;
1866318666
}
1866418667
}
18665-
else if (!isCommonEntityName) {
18668+
else {
1866618669
commonEntityName = individualEntityName;
1866718670
}
1866818671
}
1866918672
return commonEntityName;
1867018673

1867118674
case SyntaxKind.ParenthesizedType:
18672-
return getEntityNameForDecoratoryMetadata((<ParenthesizedTypeNode>node).type);
18675+
return getEntityNameForDecoratorMetadata((<ParenthesizedTypeNode>node).type);
1867318676

1867418677
case SyntaxKind.TypeReference:
1867518678
return (<TypeReferenceNode>node).typeName;
18676-
18677-
case SyntaxKind.VoidKeyword:
18678-
case SyntaxKind.UndefinedKeyword:
18679-
case SyntaxKind.NullKeyword:
18680-
case SyntaxKind.NeverKeyword:
18681-
return <voidUndefinedNullOrNeverTypeNode>node;
18682-
1868318679
}
1868418680
}
1868518681
}

src/compiler/transformers/ts.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,25 +1761,19 @@ namespace ts {
17611761
}
17621762

17631763
function serializeUnionOrIntersectionType(node: UnionOrIntersectionTypeNode): SerializedTypeNode {
1764-
// Note when updating logic here also update getEntityNameForDecoratoryMetadata
1764+
// Note when updating logic here also update getEntityNameForDecoratorMetadata
17651765
// so that aliases can be marked as referenced
17661766
let serializedUnion: SerializedTypeNode;
17671767
for (const typeNode of node.types) {
17681768
const serializedIndividual = serializeTypeNode(typeNode);
17691769

1770-
if (isVoidExpression(serializedIndividual)) {
1771-
// If we dont have any other type already set, set the initial type
1772-
if (!serializedUnion) {
1773-
serializedUnion = serializedIndividual;
1774-
}
1775-
}
1776-
else if (isIdentifier(serializedIndividual) && serializedIndividual.text === "Object") {
1770+
if (isIdentifier(serializedIndividual) && serializedIndividual.text === "Object") {
17771771
// One of the individual is global object, return immediately
17781772
return serializedIndividual;
17791773
}
17801774
// If there exists union that is not void 0 expression, check if the the common type is identifier.
17811775
// anything more complex and we will just default to Object
1782-
else if (serializedUnion && !isVoidExpression(serializedUnion)) {
1776+
else if (serializedUnion) {
17831777
// Different types
17841778
if (!isIdentifier(serializedUnion) ||
17851779
!isIdentifier(serializedIndividual) ||

src/compiler/utilities.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3566,10 +3566,6 @@ namespace ts {
35663566
return node.kind === SyntaxKind.Identifier;
35673567
}
35683568

3569-
export function isVoidExpression(node: Node): node is VoidExpression {
3570-
return node.kind === SyntaxKind.VoidExpression;
3571-
}
3572-
35733569
export function isGeneratedIdentifier(node: Node): node is GeneratedIdentifier {
35743570
// Using `>` here catches both `GeneratedIdentifierKind.None` and `undefined`.
35753571
return isIdentifier(node) && node.autoGenerateKind > GeneratedIdentifierKind.None;

tests/baselines/reference/metadataOfClassFromAlias.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//// [tests/cases/compiler/metadataOfClassFromAlias.ts] ////
22

33
//// [auxiliry.ts]
4-
54
export class SomeClass {
65
field: string;
76
}
@@ -17,6 +16,7 @@ export class ClassA {
1716

1817
//// [auxiliry.js]
1918
"use strict";
19+
Object.defineProperty(exports, "__esModule", { value: true });
2020
var SomeClass = (function () {
2121
function SomeClass() {
2222
}
@@ -34,7 +34,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
3434
var __metadata = (this && this.__metadata) || function (k, v) {
3535
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
3636
};
37-
var auxiliry_1 = require("./auxiliry");
37+
Object.defineProperty(exports, "__esModule", { value: true });
3838
function annotation() {
3939
return function (target) { };
4040
}
@@ -45,6 +45,6 @@ var ClassA = (function () {
4545
}());
4646
__decorate([
4747
annotation(),
48-
__metadata("design:type", auxiliry_1.SomeClass)
48+
__metadata("design:type", Object)
4949
], ClassA.prototype, "array", void 0);
5050
exports.ClassA = ClassA;

tests/baselines/reference/metadataOfClassFromAlias.symbols

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
=== tests/cases/compiler/auxiliry.ts ===
2-
32
export class SomeClass {
43
>SomeClass : Symbol(SomeClass, Decl(auxiliry.ts, 0, 0))
54

65
field: string;
7-
>field : Symbol(SomeClass.field, Decl(auxiliry.ts, 1, 24))
6+
>field : Symbol(SomeClass.field, Decl(auxiliry.ts, 0, 24))
87
}
98

109
=== tests/cases/compiler/test.ts ===

tests/baselines/reference/metadataOfClassFromAlias.types

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
=== tests/cases/compiler/auxiliry.ts ===
2-
32
export class SomeClass {
43
>SomeClass : SomeClass
54

tests/baselines/reference/metadataOfClassFromAlias2.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//// [tests/cases/compiler/metadataOfClassFromAlias2.ts] ////
22

33
//// [auxiliry.ts]
4-
54
export class SomeClass {
65
field: string;
76
}
@@ -17,6 +16,7 @@ export class ClassA {
1716

1817
//// [auxiliry.js]
1918
"use strict";
19+
Object.defineProperty(exports, "__esModule", { value: true });
2020
var SomeClass = (function () {
2121
function SomeClass() {
2222
}
@@ -34,6 +34,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
3434
var __metadata = (this && this.__metadata) || function (k, v) {
3535
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
3636
};
37+
Object.defineProperty(exports, "__esModule", { value: true });
3738
function annotation() {
3839
return function (target) { };
3940
}

tests/baselines/reference/metadataOfClassFromAlias2.symbols

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
=== tests/cases/compiler/auxiliry.ts ===
2-
32
export class SomeClass {
43
>SomeClass : Symbol(SomeClass, Decl(auxiliry.ts, 0, 0))
54

65
field: string;
7-
>field : Symbol(SomeClass.field, Decl(auxiliry.ts, 1, 24))
6+
>field : Symbol(SomeClass.field, Decl(auxiliry.ts, 0, 24))
87
}
98

109
=== tests/cases/compiler/test.ts ===

tests/baselines/reference/metadataOfClassFromAlias2.types

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
=== tests/cases/compiler/auxiliry.ts ===
2-
32
export class SomeClass {
43
>SomeClass : SomeClass
54

tests/baselines/reference/metadataOfUnionWithNull.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ var B = (function () {
6565
}());
6666
__decorate([
6767
PropDeco,
68-
__metadata("design:type", String)
68+
__metadata("design:type", Object)
6969
], B.prototype, "x");
7070
__decorate([
7171
PropDeco,
72-
__metadata("design:type", Boolean)
72+
__metadata("design:type", Object)
7373
], B.prototype, "y");
7474
__decorate([
7575
PropDeco,
76-
__metadata("design:type", String)
76+
__metadata("design:type", Object)
7777
], B.prototype, "z");
7878
__decorate([
7979
PropDeco,
@@ -89,25 +89,25 @@ __decorate([
8989
], B.prototype, "c");
9090
__decorate([
9191
PropDeco,
92-
__metadata("design:type", void 0)
92+
__metadata("design:type", Object)
9393
], B.prototype, "d");
9494
__decorate([
9595
PropDeco,
96-
__metadata("design:type", typeof Symbol === "function" ? Symbol : Object)
96+
__metadata("design:type", Object)
9797
], B.prototype, "e");
9898
__decorate([
9999
PropDeco,
100100
__metadata("design:type", Object)
101101
], B.prototype, "f");
102102
__decorate([
103103
PropDeco,
104-
__metadata("design:type", A)
104+
__metadata("design:type", Object)
105105
], B.prototype, "g");
106106
__decorate([
107107
PropDeco,
108-
__metadata("design:type", B)
108+
__metadata("design:type", Object)
109109
], B.prototype, "h");
110110
__decorate([
111111
PropDeco,
112-
__metadata("design:type", typeof Symbol === "function" ? Symbol : Object)
112+
__metadata("design:type", Object)
113113
], B.prototype, "j");

0 commit comments

Comments
 (0)