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 1 commit
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
41 changes: 26 additions & 15 deletions src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1662,6 +1662,8 @@ namespace ts {

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

case SyntaxKind.ParenthesizedType:
Expand Down Expand Up @@ -1715,27 +1717,35 @@ namespace ts {
case SyntaxKind.UnionType:
{
const unionOrIntersection = <UnionOrIntersectionTypeNode>node;
let serializedUnion: Identifier;
let serializedUnion: Identifier | VoidExpression;
Copy link
Contributor

Choose a reason for hiding this comment

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

We should make the serializeType* family strongly typed when it comes to return value. today it returns Expression

for (const typeNode of unionOrIntersection.types) {
const serializedIndividual = serializeTypeNode(typeNode) as Identifier;
// Non identifier
if (serializedIndividual.kind !== SyntaxKind.Identifier) {
serializedUnion = undefined;
break;
}
const serializedIndividual = serializeTypeNode(typeNode);

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

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

// Different types
if (serializedUnion && serializedUnion.text !== serializedIndividual.text) {
serializedUnion = serializedIndividual;
}
else if (isVoidExpression(serializedIndividual)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

it could also return BinaryExpression would that be fine?

Copy link
Contributor

Choose a reason for hiding this comment

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

or ConditionalExpression

// If we dont have any other type already set, set the initial type
if (!serializedUnion) {
serializedUnion = serializedIndividual;
}
}
else {
// Non identifier and undefined/null
serializedUnion = undefined;
break;
}

serializedUnion = serializedIndividual;
}

// If we were able to find common type
Expand All @@ -1751,6 +1761,7 @@ namespace ts {
case SyntaxKind.TypeLiteral:
case SyntaxKind.AnyKeyword:
case SyntaxKind.ThisType:
case SyntaxKind.NeverKeyword:
break;

default:
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
78 changes: 78 additions & 0 deletions tests/baselines/reference/metadataOfUnionWithNull.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//// [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;
}

//// [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", Object)
], 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", Object)
], B.prototype, "b");
__decorate([
PropDeco,
__metadata("design:type", void 0)
], B.prototype, "c");
__decorate([
PropDeco,
__metadata("design:type", void 0)
], B.prototype, "d");
56 changes: 56 additions & 0 deletions tests/baselines/reference/metadataOfUnionWithNull.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
=== 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))
}
60 changes: 60 additions & 0 deletions tests/baselines/reference/metadataOfUnionWithNull.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
=== tests/cases/compiler/metadataOfUnionWithNull.ts ===
function PropDeco(target: Object, propKey: string | symbol) { }
>PropDeco : (target: Object, propKey: string | symbol) => void
>target : Object
>Object : Object
>propKey : string | symbol

class A {
>A : A
}

class B {
>B : B

@PropDeco
>PropDeco : (target: Object, propKey: string | symbol) => void

x: "foo" | null;
>x : "foo"
>null : null

@PropDeco
>PropDeco : (target: Object, propKey: string | symbol) => void

y: true | never;
>y : true
>true : true

@PropDeco
>PropDeco : (target: Object, propKey: string | symbol) => void

z: "foo" | undefined;
>z : "foo"

@PropDeco
>PropDeco : (target: Object, propKey: string | symbol) => void

a: null;
>a : null
>null : null

@PropDeco
>PropDeco : (target: Object, propKey: string | symbol) => void

b: never;
>b : never

@PropDeco
>PropDeco : (target: Object, propKey: string | symbol) => void

c: undefined;
>c : undefined

@PropDeco
>PropDeco : (target: Object, propKey: string | symbol) => void

d: undefined | null;
>d : null
>null : null
}
29 changes: 29 additions & 0 deletions tests/cases/compiler/metadataOfUnionWithNull.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// @experimentalDecorators: true
// @emitDecoratorMetadata: true
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;
}