Skip to content

Commit 4cbb50a

Browse files
authored
Merge pull request #13034 from Microsoft/unionWithNull
Add serialization of typenode for null/undefined/never as part of metadata type
2 parents e9da643 + 62426de commit 4cbb50a

File tree

6 files changed

+399
-40
lines changed

6 files changed

+399
-40
lines changed

src/compiler/transformers/ts.ts

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,12 +1555,15 @@ namespace ts {
15551555
return false;
15561556
}
15571557

1558+
type SerializedEntityNameAsExpression = Identifier | BinaryExpression | PropertyAccessExpression;
1559+
type SerializedTypeNode = SerializedEntityNameAsExpression | VoidExpression | ConditionalExpression;
1560+
15581561
/**
15591562
* Serializes the type of a node for use with decorator type metadata.
15601563
*
15611564
* @param node The node that should have its type serialized.
15621565
*/
1563-
function serializeTypeOfNode(node: Node): Expression {
1566+
function serializeTypeOfNode(node: Node): SerializedTypeNode {
15641567
switch (node.kind) {
15651568
case SyntaxKind.PropertyDeclaration:
15661569
case SyntaxKind.Parameter:
@@ -1582,15 +1585,15 @@ namespace ts {
15821585
*
15831586
* @param node The node that should have its parameter types serialized.
15841587
*/
1585-
function serializeParameterTypesOfNode(node: Node, container: ClassLikeDeclaration): Expression {
1588+
function serializeParameterTypesOfNode(node: Node, container: ClassLikeDeclaration): ArrayLiteralExpression {
15861589
const valueDeclaration =
15871590
isClassLike(node)
15881591
? getFirstConstructorWithBody(node)
15891592
: isFunctionLike(node) && nodeIsPresent(node.body)
15901593
? node
15911594
: undefined;
15921595

1593-
const expressions: Expression[] = [];
1596+
const expressions: SerializedTypeNode[] = [];
15941597
if (valueDeclaration) {
15951598
const parameters = getParametersOfDecoratedDeclaration(valueDeclaration, container);
15961599
const numParameters = parameters.length;
@@ -1626,7 +1629,7 @@ namespace ts {
16261629
*
16271630
* @param node The node that should have its return type serialized.
16281631
*/
1629-
function serializeReturnTypeOfNode(node: Node): Expression {
1632+
function serializeReturnTypeOfNode(node: Node): SerializedTypeNode {
16301633
if (isFunctionLike(node) && node.type) {
16311634
return serializeTypeNode(node.type);
16321635
}
@@ -1655,13 +1658,16 @@ namespace ts {
16551658
*
16561659
* @param node The type node to serialize.
16571660
*/
1658-
function serializeTypeNode(node: TypeNode): Expression {
1661+
function serializeTypeNode(node: TypeNode): SerializedTypeNode {
16591662
if (node === undefined) {
16601663
return createIdentifier("Object");
16611664
}
16621665

16631666
switch (node.kind) {
16641667
case SyntaxKind.VoidKeyword:
1668+
case SyntaxKind.UndefinedKeyword:
1669+
case SyntaxKind.NullKeyword:
1670+
case SyntaxKind.NeverKeyword:
16651671
return createVoidZero();
16661672

16671673
case SyntaxKind.ParenthesizedType:
@@ -1713,37 +1719,8 @@ namespace ts {
17131719

17141720
case SyntaxKind.IntersectionType:
17151721
case SyntaxKind.UnionType:
1716-
{
1717-
const unionOrIntersection = <UnionOrIntersectionTypeNode>node;
1718-
let serializedUnion: Identifier;
1719-
for (const typeNode of unionOrIntersection.types) {
1720-
const serializedIndividual = serializeTypeNode(typeNode) as Identifier;
1721-
// Non identifier
1722-
if (serializedIndividual.kind !== SyntaxKind.Identifier) {
1723-
serializedUnion = undefined;
1724-
break;
1725-
}
1726-
1727-
// One of the individual is global object, return immediately
1728-
if (serializedIndividual.text === "Object") {
1729-
return serializedIndividual;
1730-
}
1731-
1732-
// Different types
1733-
if (serializedUnion && serializedUnion.text !== serializedIndividual.text) {
1734-
serializedUnion = undefined;
1735-
break;
1736-
}
1737-
1738-
serializedUnion = serializedIndividual;
1739-
}
1722+
return serializeUnionOrIntersectionType(<UnionOrIntersectionTypeNode>node);
17401723

1741-
// If we were able to find common type
1742-
if (serializedUnion) {
1743-
return serializedUnion;
1744-
}
1745-
}
1746-
// Fallthrough
17471724
case SyntaxKind.TypeQuery:
17481725
case SyntaxKind.TypeOperator:
17491726
case SyntaxKind.IndexedAccessType:
@@ -1761,13 +1738,48 @@ namespace ts {
17611738
return createIdentifier("Object");
17621739
}
17631740

1741+
function serializeUnionOrIntersectionType(node: UnionOrIntersectionTypeNode): SerializedTypeNode {
1742+
let serializedUnion: SerializedTypeNode;
1743+
for (const typeNode of node.types) {
1744+
const serializedIndividual = serializeTypeNode(typeNode);
1745+
1746+
if (isVoidExpression(serializedIndividual)) {
1747+
// If we dont have any other type already set, set the initial type
1748+
if (!serializedUnion) {
1749+
serializedUnion = serializedIndividual;
1750+
}
1751+
}
1752+
else if (isIdentifier(serializedIndividual) && serializedIndividual.text === "Object") {
1753+
// One of the individual is global object, return immediately
1754+
return serializedIndividual;
1755+
}
1756+
// If there exists union that is not void 0 expression, check if the the common type is identifier.
1757+
// anything more complex and we will just default to Object
1758+
else if (serializedUnion && !isVoidExpression(serializedUnion)) {
1759+
// Different types
1760+
if (!isIdentifier(serializedUnion) ||
1761+
!isIdentifier(serializedIndividual) ||
1762+
serializedUnion.text !== serializedIndividual.text) {
1763+
return createIdentifier("Object");
1764+
}
1765+
}
1766+
else {
1767+
// Initialize the union type
1768+
serializedUnion = serializedIndividual;
1769+
}
1770+
}
1771+
1772+
// If we were able to find common type, use it
1773+
return serializedUnion;
1774+
}
1775+
17641776
/**
17651777
* Serializes a TypeReferenceNode to an appropriate JS constructor value for use with
17661778
* decorator type metadata.
17671779
*
17681780
* @param node The type reference node.
17691781
*/
1770-
function serializeTypeReferenceNode(node: TypeReferenceNode) {
1782+
function serializeTypeReferenceNode(node: TypeReferenceNode): SerializedTypeNode {
17711783
switch (resolver.getTypeReferenceSerializationKind(node.typeName, currentScope)) {
17721784
case TypeReferenceSerializationKind.Unknown:
17731785
const serialized = serializeEntityNameAsExpression(node.typeName, /*useFallback*/ true);
@@ -1822,7 +1834,7 @@ namespace ts {
18221834
* @param useFallback A value indicating whether to use logical operators to test for the
18231835
* entity name at runtime.
18241836
*/
1825-
function serializeEntityNameAsExpression(node: EntityName, useFallback: boolean): Expression {
1837+
function serializeEntityNameAsExpression(node: EntityName, useFallback: boolean): SerializedEntityNameAsExpression {
18261838
switch (node.kind) {
18271839
case SyntaxKind.Identifier:
18281840
// Create a clone of the name with a new parent, and treat it as if it were
@@ -1855,8 +1867,8 @@ namespace ts {
18551867
* @param useFallback A value indicating whether to use logical operators to test for the
18561868
* qualified name at runtime.
18571869
*/
1858-
function serializeQualifiedNameAsExpression(node: QualifiedName, useFallback: boolean): Expression {
1859-
let left: Expression;
1870+
function serializeQualifiedNameAsExpression(node: QualifiedName, useFallback: boolean): PropertyAccessExpression {
1871+
let left: SerializedEntityNameAsExpression;
18601872
if (node.left.kind === SyntaxKind.Identifier) {
18611873
left = serializeEntityNameAsExpression(node.left, useFallback);
18621874
}
@@ -1881,7 +1893,7 @@ namespace ts {
18811893
* Gets an expression that points to the global "Symbol" constructor at runtime if it is
18821894
* available.
18831895
*/
1884-
function getGlobalSymbolNameWithFallback(): Expression {
1896+
function getGlobalSymbolNameWithFallback(): ConditionalExpression {
18851897
return createConditional(
18861898
createTypeCheck(createIdentifier("Symbol"), "function"),
18871899
createIdentifier("Symbol"),

src/compiler/utilities.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3591,6 +3591,10 @@ namespace ts {
35913591
return node.kind === SyntaxKind.Identifier;
35923592
}
35933593

3594+
export function isVoidExpression(node: Node): node is VoidExpression {
3595+
return node.kind === SyntaxKind.VoidExpression;
3596+
}
3597+
35943598
export function isGeneratedIdentifier(node: Node): node is GeneratedIdentifier {
35953599
// Using `>` here catches both `GeneratedIdentifierKind.None` and `undefined`.
35963600
return isIdentifier(node) && node.autoGenerateKind > GeneratedIdentifierKind.None;
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
//// [metadataOfUnionWithNull.ts]
2+
function PropDeco(target: Object, propKey: string | symbol) { }
3+
4+
class A {
5+
}
6+
7+
class B {
8+
@PropDeco
9+
x: "foo" | null;
10+
11+
@PropDeco
12+
y: true | never;
13+
14+
@PropDeco
15+
z: "foo" | undefined;
16+
17+
@PropDeco
18+
a: null;
19+
20+
@PropDeco
21+
b: never;
22+
23+
@PropDeco
24+
c: undefined;
25+
26+
@PropDeco
27+
d: undefined | null;
28+
29+
@PropDeco
30+
e: symbol | null;
31+
32+
@PropDeco
33+
f: symbol | A;
34+
35+
@PropDeco
36+
g: A | null;
37+
38+
@PropDeco
39+
h: null | B;
40+
41+
@PropDeco
42+
j: null | symbol;
43+
}
44+
45+
//// [metadataOfUnionWithNull.js]
46+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
47+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
48+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
49+
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;
50+
return c > 3 && r && Object.defineProperty(target, key, r), r;
51+
};
52+
var __metadata = (this && this.__metadata) || function (k, v) {
53+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
54+
};
55+
function PropDeco(target, propKey) { }
56+
var A = (function () {
57+
function A() {
58+
}
59+
return A;
60+
}());
61+
var B = (function () {
62+
function B() {
63+
}
64+
return B;
65+
}());
66+
__decorate([
67+
PropDeco,
68+
__metadata("design:type", String)
69+
], B.prototype, "x");
70+
__decorate([
71+
PropDeco,
72+
__metadata("design:type", Boolean)
73+
], B.prototype, "y");
74+
__decorate([
75+
PropDeco,
76+
__metadata("design:type", String)
77+
], B.prototype, "z");
78+
__decorate([
79+
PropDeco,
80+
__metadata("design:type", void 0)
81+
], B.prototype, "a");
82+
__decorate([
83+
PropDeco,
84+
__metadata("design:type", void 0)
85+
], B.prototype, "b");
86+
__decorate([
87+
PropDeco,
88+
__metadata("design:type", void 0)
89+
], B.prototype, "c");
90+
__decorate([
91+
PropDeco,
92+
__metadata("design:type", void 0)
93+
], B.prototype, "d");
94+
__decorate([
95+
PropDeco,
96+
__metadata("design:type", typeof Symbol === "function" ? Symbol : Object)
97+
], B.prototype, "e");
98+
__decorate([
99+
PropDeco,
100+
__metadata("design:type", Object)
101+
], B.prototype, "f");
102+
__decorate([
103+
PropDeco,
104+
__metadata("design:type", A)
105+
], B.prototype, "g");
106+
__decorate([
107+
PropDeco,
108+
__metadata("design:type", B)
109+
], B.prototype, "h");
110+
__decorate([
111+
PropDeco,
112+
__metadata("design:type", typeof Symbol === "function" ? Symbol : Object)
113+
], B.prototype, "j");
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
=== tests/cases/compiler/metadataOfUnionWithNull.ts ===
2+
function PropDeco(target: Object, propKey: string | symbol) { }
3+
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0))
4+
>target : Symbol(target, Decl(metadataOfUnionWithNull.ts, 0, 18))
5+
>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
6+
>propKey : Symbol(propKey, Decl(metadataOfUnionWithNull.ts, 0, 33))
7+
8+
class A {
9+
>A : Symbol(A, Decl(metadataOfUnionWithNull.ts, 0, 63))
10+
}
11+
12+
class B {
13+
>B : Symbol(B, Decl(metadataOfUnionWithNull.ts, 3, 1))
14+
15+
@PropDeco
16+
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0))
17+
18+
x: "foo" | null;
19+
>x : Symbol(B.x, Decl(metadataOfUnionWithNull.ts, 5, 9))
20+
21+
@PropDeco
22+
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0))
23+
24+
y: true | never;
25+
>y : Symbol(B.y, Decl(metadataOfUnionWithNull.ts, 7, 20))
26+
27+
@PropDeco
28+
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0))
29+
30+
z: "foo" | undefined;
31+
>z : Symbol(B.z, Decl(metadataOfUnionWithNull.ts, 10, 20))
32+
33+
@PropDeco
34+
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0))
35+
36+
a: null;
37+
>a : Symbol(B.a, Decl(metadataOfUnionWithNull.ts, 13, 25))
38+
39+
@PropDeco
40+
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0))
41+
42+
b: never;
43+
>b : Symbol(B.b, Decl(metadataOfUnionWithNull.ts, 16, 12))
44+
45+
@PropDeco
46+
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0))
47+
48+
c: undefined;
49+
>c : Symbol(B.c, Decl(metadataOfUnionWithNull.ts, 19, 13))
50+
51+
@PropDeco
52+
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0))
53+
54+
d: undefined | null;
55+
>d : Symbol(B.d, Decl(metadataOfUnionWithNull.ts, 22, 17))
56+
57+
@PropDeco
58+
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0))
59+
60+
e: symbol | null;
61+
>e : Symbol(B.e, Decl(metadataOfUnionWithNull.ts, 25, 24))
62+
63+
@PropDeco
64+
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0))
65+
66+
f: symbol | A;
67+
>f : Symbol(B.f, Decl(metadataOfUnionWithNull.ts, 28, 21))
68+
>A : Symbol(A, Decl(metadataOfUnionWithNull.ts, 0, 63))
69+
70+
@PropDeco
71+
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0))
72+
73+
g: A | null;
74+
>g : Symbol(B.g, Decl(metadataOfUnionWithNull.ts, 31, 18))
75+
>A : Symbol(A, Decl(metadataOfUnionWithNull.ts, 0, 63))
76+
77+
@PropDeco
78+
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0))
79+
80+
h: null | B;
81+
>h : Symbol(B.h, Decl(metadataOfUnionWithNull.ts, 34, 16))
82+
>B : Symbol(B, Decl(metadataOfUnionWithNull.ts, 3, 1))
83+
84+
@PropDeco
85+
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0))
86+
87+
j: null | symbol;
88+
>j : Symbol(B.j, Decl(metadataOfUnionWithNull.ts, 37, 16))
89+
}

0 commit comments

Comments
 (0)