Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add serialization of typenode for null/undefined/never as part of metadata type #13034

Merged
merged 3 commits into from
Dec 21, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 48 additions & 36 deletions src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1637,6 +1637,8 @@ namespace ts {
return createVoidZero();
}

type SerializedTypeNode = SerializedEntityNameAsExpression | VoidExpression | ConditionalExpression;

/**
* Serializes a type node for use with decorator type metadata.
*
Expand All @@ -1655,13 +1657,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 +1718,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 +1737,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 @@ -1815,14 +1826,15 @@ namespace ts {
}
}

type SerializedEntityNameAsExpression = Identifier | BinaryExpression | PropertyAccessExpression;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, can you move this next to the other type alias. possibly move both to the top of the file.

/**
* Serializes an entity name as an expression for decorator type metadata.
*
* @param node The entity name to serialize.
* @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 @@ -3581,6 +3581,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