Skip to content

Commit

Permalink
Merge pull request #13034 from Microsoft/unionWithNull
Browse files Browse the repository at this point in the history
Add serialization of typenode for null/undefined/never as part of metadata type
  • Loading branch information
sheetalkamat committed Dec 21, 2016
2 parents e9da643 + 62426de commit 4cbb50a
Show file tree
Hide file tree
Showing 6 changed files with 399 additions and 40 deletions.
92 changes: 52 additions & 40 deletions src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1555,12 +1555,15 @@ namespace ts {
return false;
}

type SerializedEntityNameAsExpression = Identifier | BinaryExpression | PropertyAccessExpression;
type SerializedTypeNode = SerializedEntityNameAsExpression | VoidExpression | ConditionalExpression;

/**
* Serializes the type of a node for use with decorator type metadata.
*
* @param node The node that should have its type serialized.
*/
function serializeTypeOfNode(node: Node): Expression {
function serializeTypeOfNode(node: Node): SerializedTypeNode {
switch (node.kind) {
case SyntaxKind.PropertyDeclaration:
case SyntaxKind.Parameter:
Expand All @@ -1582,15 +1585,15 @@ namespace ts {
*
* @param node The node that should have its parameter types serialized.
*/
function serializeParameterTypesOfNode(node: Node, container: ClassLikeDeclaration): Expression {
function serializeParameterTypesOfNode(node: Node, container: ClassLikeDeclaration): ArrayLiteralExpression {
const valueDeclaration =
isClassLike(node)
? getFirstConstructorWithBody(node)
: isFunctionLike(node) && nodeIsPresent(node.body)
? node
: undefined;

const expressions: Expression[] = [];
const expressions: SerializedTypeNode[] = [];
if (valueDeclaration) {
const parameters = getParametersOfDecoratedDeclaration(valueDeclaration, container);
const numParameters = parameters.length;
Expand Down Expand Up @@ -1626,7 +1629,7 @@ namespace ts {
*
* @param node The node that should have its return type serialized.
*/
function serializeReturnTypeOfNode(node: Node): Expression {
function serializeReturnTypeOfNode(node: Node): SerializedTypeNode {
if (isFunctionLike(node) && node.type) {
return serializeTypeNode(node.type);
}
Expand Down Expand Up @@ -1655,13 +1658,16 @@ namespace ts {
*
* @param node The type node to serialize.
*/
function serializeTypeNode(node: TypeNode): Expression {
function serializeTypeNode(node: TypeNode): SerializedTypeNode {
if (node === undefined) {
return createIdentifier("Object");
}

switch (node.kind) {
case SyntaxKind.VoidKeyword:
case SyntaxKind.UndefinedKeyword:
case SyntaxKind.NullKeyword:
case SyntaxKind.NeverKeyword:
return createVoidZero();

case SyntaxKind.ParenthesizedType:
Expand Down Expand Up @@ -1713,37 +1719,8 @@ namespace ts {

case SyntaxKind.IntersectionType:
case SyntaxKind.UnionType:
{
const unionOrIntersection = <UnionOrIntersectionTypeNode>node;
let serializedUnion: Identifier;
for (const typeNode of unionOrIntersection.types) {
const serializedIndividual = serializeTypeNode(typeNode) as Identifier;
// Non identifier
if (serializedIndividual.kind !== SyntaxKind.Identifier) {
serializedUnion = undefined;
break;
}

// One of the individual is global object, return immediately
if (serializedIndividual.text === "Object") {
return serializedIndividual;
}

// Different types
if (serializedUnion && serializedUnion.text !== serializedIndividual.text) {
serializedUnion = undefined;
break;
}

serializedUnion = serializedIndividual;
}
return serializeUnionOrIntersectionType(<UnionOrIntersectionTypeNode>node);

// If we were able to find common type
if (serializedUnion) {
return serializedUnion;
}
}
// Fallthrough
case SyntaxKind.TypeQuery:
case SyntaxKind.TypeOperator:
case SyntaxKind.IndexedAccessType:
Expand All @@ -1761,13 +1738,48 @@ namespace ts {
return createIdentifier("Object");
}

function serializeUnionOrIntersectionType(node: UnionOrIntersectionTypeNode): SerializedTypeNode {
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") {
// 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)) {
// Different types
if (!isIdentifier(serializedUnion) ||
!isIdentifier(serializedIndividual) ||
serializedUnion.text !== serializedIndividual.text) {
return createIdentifier("Object");
}
}
else {
// Initialize the union type
serializedUnion = serializedIndividual;
}
}

// If we were able to find common type, use it
return serializedUnion;
}

/**
* Serializes a TypeReferenceNode to an appropriate JS constructor value for use with
* decorator type metadata.
*
* @param node The type reference node.
*/
function serializeTypeReferenceNode(node: TypeReferenceNode) {
function serializeTypeReferenceNode(node: TypeReferenceNode): SerializedTypeNode {
switch (resolver.getTypeReferenceSerializationKind(node.typeName, currentScope)) {
case TypeReferenceSerializationKind.Unknown:
const serialized = serializeEntityNameAsExpression(node.typeName, /*useFallback*/ true);
Expand Down Expand Up @@ -1822,7 +1834,7 @@ namespace ts {
* @param useFallback A value indicating whether to use logical operators to test for the
* entity name at runtime.
*/
function serializeEntityNameAsExpression(node: EntityName, useFallback: boolean): Expression {
function serializeEntityNameAsExpression(node: EntityName, useFallback: boolean): SerializedEntityNameAsExpression {
switch (node.kind) {
case SyntaxKind.Identifier:
// Create a clone of the name with a new parent, and treat it as if it were
Expand Down Expand Up @@ -1855,8 +1867,8 @@ namespace ts {
* @param useFallback A value indicating whether to use logical operators to test for the
* qualified name at runtime.
*/
function serializeQualifiedNameAsExpression(node: QualifiedName, useFallback: boolean): Expression {
let left: Expression;
function serializeQualifiedNameAsExpression(node: QualifiedName, useFallback: boolean): PropertyAccessExpression {
let left: SerializedEntityNameAsExpression;
if (node.left.kind === SyntaxKind.Identifier) {
left = serializeEntityNameAsExpression(node.left, useFallback);
}
Expand All @@ -1881,7 +1893,7 @@ namespace ts {
* Gets an expression that points to the global "Symbol" constructor at runtime if it is
* available.
*/
function getGlobalSymbolNameWithFallback(): Expression {
function getGlobalSymbolNameWithFallback(): ConditionalExpression {
return createConditional(
createTypeCheck(createIdentifier("Symbol"), "function"),
createIdentifier("Symbol"),
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3591,6 +3591,10 @@ 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;
Expand Down
113 changes: 113 additions & 0 deletions tests/baselines/reference/metadataOfUnionWithNull.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
//// [metadataOfUnionWithNull.ts]
function PropDeco(target: Object, propKey: string | symbol) { }

class A {
}

class B {
@PropDeco
x: "foo" | null;

@PropDeco
y: true | never;

@PropDeco
z: "foo" | undefined;

@PropDeco
a: null;

@PropDeco
b: never;

@PropDeco
c: undefined;

@PropDeco
d: undefined | null;

@PropDeco
e: symbol | null;

@PropDeco
f: symbol | A;

@PropDeco
g: A | null;

@PropDeco
h: null | B;

@PropDeco
j: null | symbol;
}

//// [metadataOfUnionWithNull.js]
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 PropDeco(target, propKey) { }
var A = (function () {
function A() {
}
return A;
}());
var B = (function () {
function B() {
}
return B;
}());
__decorate([
PropDeco,
__metadata("design:type", String)
], B.prototype, "x");
__decorate([
PropDeco,
__metadata("design:type", Boolean)
], B.prototype, "y");
__decorate([
PropDeco,
__metadata("design:type", String)
], B.prototype, "z");
__decorate([
PropDeco,
__metadata("design:type", void 0)
], B.prototype, "a");
__decorate([
PropDeco,
__metadata("design:type", void 0)
], B.prototype, "b");
__decorate([
PropDeco,
__metadata("design:type", void 0)
], B.prototype, "c");
__decorate([
PropDeco,
__metadata("design:type", void 0)
], B.prototype, "d");
__decorate([
PropDeco,
__metadata("design:type", typeof Symbol === "function" ? Symbol : Object)
], B.prototype, "e");
__decorate([
PropDeco,
__metadata("design:type", Object)
], B.prototype, "f");
__decorate([
PropDeco,
__metadata("design:type", A)
], B.prototype, "g");
__decorate([
PropDeco,
__metadata("design:type", B)
], B.prototype, "h");
__decorate([
PropDeco,
__metadata("design:type", typeof Symbol === "function" ? Symbol : Object)
], B.prototype, "j");
89 changes: 89 additions & 0 deletions tests/baselines/reference/metadataOfUnionWithNull.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
=== tests/cases/compiler/metadataOfUnionWithNull.ts ===
function PropDeco(target: Object, propKey: string | symbol) { }
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0))
>target : Symbol(target, Decl(metadataOfUnionWithNull.ts, 0, 18))
>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>propKey : Symbol(propKey, Decl(metadataOfUnionWithNull.ts, 0, 33))

class A {
>A : Symbol(A, Decl(metadataOfUnionWithNull.ts, 0, 63))
}

class B {
>B : Symbol(B, Decl(metadataOfUnionWithNull.ts, 3, 1))

@PropDeco
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0))

x: "foo" | null;
>x : Symbol(B.x, Decl(metadataOfUnionWithNull.ts, 5, 9))

@PropDeco
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0))

y: true | never;
>y : Symbol(B.y, Decl(metadataOfUnionWithNull.ts, 7, 20))

@PropDeco
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0))

z: "foo" | undefined;
>z : Symbol(B.z, Decl(metadataOfUnionWithNull.ts, 10, 20))

@PropDeco
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0))

a: null;
>a : Symbol(B.a, Decl(metadataOfUnionWithNull.ts, 13, 25))

@PropDeco
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0))

b: never;
>b : Symbol(B.b, Decl(metadataOfUnionWithNull.ts, 16, 12))

@PropDeco
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0))

c: undefined;
>c : Symbol(B.c, Decl(metadataOfUnionWithNull.ts, 19, 13))

@PropDeco
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0))

d: undefined | null;
>d : Symbol(B.d, Decl(metadataOfUnionWithNull.ts, 22, 17))

@PropDeco
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0))

e: symbol | null;
>e : Symbol(B.e, Decl(metadataOfUnionWithNull.ts, 25, 24))

@PropDeco
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0))

f: symbol | A;
>f : Symbol(B.f, Decl(metadataOfUnionWithNull.ts, 28, 21))
>A : Symbol(A, Decl(metadataOfUnionWithNull.ts, 0, 63))

@PropDeco
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0))

g: A | null;
>g : Symbol(B.g, Decl(metadataOfUnionWithNull.ts, 31, 18))
>A : Symbol(A, Decl(metadataOfUnionWithNull.ts, 0, 63))

@PropDeco
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0))

h: null | B;
>h : Symbol(B.h, Decl(metadataOfUnionWithNull.ts, 34, 16))
>B : Symbol(B, Decl(metadataOfUnionWithNull.ts, 3, 1))

@PropDeco
>PropDeco : Symbol(PropDeco, Decl(metadataOfUnionWithNull.ts, 0, 0))

j: null | symbol;
>j : Symbol(B.j, Decl(metadataOfUnionWithNull.ts, 37, 16))
}
Loading

0 comments on commit 4cbb50a

Please sign in to comment.