Skip to content

Commit 0573c0b

Browse files
committed
Merge pull request #4599 from Microsoft/decoratorInferredType
If Type annotation is missing, emit design:Type as Object
2 parents f47650c + 5f78d39 commit 0573c0b

File tree

5 files changed

+183
-46
lines changed

5 files changed

+183
-46
lines changed

src/compiler/emitter.ts

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4939,63 +4939,61 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
49394939
}
49404940

49414941
function emitSerializedTypeNode(node: TypeNode) {
4942-
if (!node) {
4943-
return;
4944-
}
4945-
4946-
switch (node.kind) {
4947-
case SyntaxKind.VoidKeyword:
4948-
write("void 0");
4949-
return;
4942+
if (node) {
49504943

4951-
case SyntaxKind.ParenthesizedType:
4952-
emitSerializedTypeNode((<ParenthesizedTypeNode>node).type);
4953-
return;
4944+
switch (node.kind) {
4945+
case SyntaxKind.VoidKeyword:
4946+
write("void 0");
4947+
return;
49544948

4955-
case SyntaxKind.FunctionType:
4956-
case SyntaxKind.ConstructorType:
4957-
write("Function");
4958-
return;
4949+
case SyntaxKind.ParenthesizedType:
4950+
emitSerializedTypeNode((<ParenthesizedTypeNode>node).type);
4951+
return;
49594952

4960-
case SyntaxKind.ArrayType:
4961-
case SyntaxKind.TupleType:
4962-
write("Array");
4963-
return;
4953+
case SyntaxKind.FunctionType:
4954+
case SyntaxKind.ConstructorType:
4955+
write("Function");
4956+
return;
49644957

4965-
case SyntaxKind.TypePredicate:
4966-
case SyntaxKind.BooleanKeyword:
4967-
write("Boolean");
4968-
return;
4958+
case SyntaxKind.ArrayType:
4959+
case SyntaxKind.TupleType:
4960+
write("Array");
4961+
return;
49694962

4970-
case SyntaxKind.StringKeyword:
4971-
case SyntaxKind.StringLiteral:
4972-
write("String");
4973-
return;
4963+
case SyntaxKind.TypePredicate:
4964+
case SyntaxKind.BooleanKeyword:
4965+
write("Boolean");
4966+
return;
49744967

4975-
case SyntaxKind.NumberKeyword:
4976-
write("Number");
4977-
return;
4968+
case SyntaxKind.StringKeyword:
4969+
case SyntaxKind.StringLiteral:
4970+
write("String");
4971+
return;
49784972

4979-
case SyntaxKind.SymbolKeyword:
4980-
write("Symbol");
4981-
return;
4973+
case SyntaxKind.NumberKeyword:
4974+
write("Number");
4975+
return;
49824976

4983-
case SyntaxKind.TypeReference:
4984-
emitSerializedTypeReferenceNode(<TypeReferenceNode>node);
4985-
return;
4977+
case SyntaxKind.SymbolKeyword:
4978+
write("Symbol");
4979+
return;
49864980

4987-
case SyntaxKind.TypeQuery:
4988-
case SyntaxKind.TypeLiteral:
4989-
case SyntaxKind.UnionType:
4990-
case SyntaxKind.IntersectionType:
4991-
case SyntaxKind.AnyKeyword:
4992-
break;
4981+
case SyntaxKind.TypeReference:
4982+
emitSerializedTypeReferenceNode(<TypeReferenceNode>node);
4983+
return;
49934984

4994-
default:
4995-
Debug.fail("Cannot serialize unexpected type node.");
4996-
break;
4997-
}
4985+
case SyntaxKind.TypeQuery:
4986+
case SyntaxKind.TypeLiteral:
4987+
case SyntaxKind.UnionType:
4988+
case SyntaxKind.IntersectionType:
4989+
case SyntaxKind.AnyKeyword:
4990+
break;
49984991

4992+
default:
4993+
Debug.fail("Cannot serialize unexpected type node.");
4994+
break;
4995+
}
4996+
}
49994997
write("Object");
50004998
}
50014999

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//// [decoratorMetadataOnInferredType.ts]
2+
3+
declare var console: {
4+
log(msg: string): void;
5+
};
6+
7+
class A {
8+
constructor() { console.log('new A'); }
9+
}
10+
11+
function decorator(target: Object, propertyKey: string) {
12+
}
13+
14+
export class B {
15+
@decorator
16+
x = new A();
17+
}
18+
19+
20+
//// [decoratorMetadataOnInferredType.js]
21+
var A = (function () {
22+
function A() {
23+
console.log('new A');
24+
}
25+
return A;
26+
})();
27+
function decorator(target, propertyKey) {
28+
}
29+
var B = (function () {
30+
function B() {
31+
this.x = new A();
32+
}
33+
__decorate([
34+
decorator,
35+
__metadata('design:type', Object)
36+
], B.prototype, "x");
37+
return B;
38+
})();
39+
exports.B = B;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
=== tests/cases/compiler/decoratorMetadataOnInferredType.ts ===
2+
3+
declare var console: {
4+
>console : Symbol(console, Decl(decoratorMetadataOnInferredType.ts, 1, 11))
5+
6+
log(msg: string): void;
7+
>log : Symbol(log, Decl(decoratorMetadataOnInferredType.ts, 1, 22))
8+
>msg : Symbol(msg, Decl(decoratorMetadataOnInferredType.ts, 2, 8))
9+
10+
};
11+
12+
class A {
13+
>A : Symbol(A, Decl(decoratorMetadataOnInferredType.ts, 3, 2))
14+
15+
constructor() { console.log('new A'); }
16+
>console.log : Symbol(log, Decl(decoratorMetadataOnInferredType.ts, 1, 22))
17+
>console : Symbol(console, Decl(decoratorMetadataOnInferredType.ts, 1, 11))
18+
>log : Symbol(log, Decl(decoratorMetadataOnInferredType.ts, 1, 22))
19+
}
20+
21+
function decorator(target: Object, propertyKey: string) {
22+
>decorator : Symbol(decorator, Decl(decoratorMetadataOnInferredType.ts, 7, 1))
23+
>target : Symbol(target, Decl(decoratorMetadataOnInferredType.ts, 9, 19))
24+
>Object : Symbol(Object, Decl(lib.d.ts, 92, 1), Decl(lib.d.ts, 223, 11))
25+
>propertyKey : Symbol(propertyKey, Decl(decoratorMetadataOnInferredType.ts, 9, 34))
26+
}
27+
28+
export class B {
29+
>B : Symbol(B, Decl(decoratorMetadataOnInferredType.ts, 10, 1))
30+
31+
@decorator
32+
>decorator : Symbol(decorator, Decl(decoratorMetadataOnInferredType.ts, 7, 1))
33+
34+
x = new A();
35+
>x : Symbol(x, Decl(decoratorMetadataOnInferredType.ts, 12, 16))
36+
>A : Symbol(A, Decl(decoratorMetadataOnInferredType.ts, 3, 2))
37+
}
38+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
=== tests/cases/compiler/decoratorMetadataOnInferredType.ts ===
2+
3+
declare var console: {
4+
>console : { log(msg: string): void; }
5+
6+
log(msg: string): void;
7+
>log : (msg: string) => void
8+
>msg : string
9+
10+
};
11+
12+
class A {
13+
>A : A
14+
15+
constructor() { console.log('new A'); }
16+
>console.log('new A') : void
17+
>console.log : (msg: string) => void
18+
>console : { log(msg: string): void; }
19+
>log : (msg: string) => void
20+
>'new A' : string
21+
}
22+
23+
function decorator(target: Object, propertyKey: string) {
24+
>decorator : (target: Object, propertyKey: string) => void
25+
>target : Object
26+
>Object : Object
27+
>propertyKey : string
28+
}
29+
30+
export class B {
31+
>B : B
32+
33+
@decorator
34+
>decorator : (target: Object, propertyKey: string) => void
35+
36+
x = new A();
37+
>x : A
38+
>new A() : A
39+
>A : typeof A
40+
}
41+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// @noemithelpers: true
2+
// @experimentaldecorators: true
3+
// @emitdecoratormetadata: true
4+
// @target: es5
5+
// @module: commonjs
6+
7+
declare var console: {
8+
log(msg: string): void;
9+
};
10+
11+
class A {
12+
constructor() { console.log('new A'); }
13+
}
14+
15+
function decorator(target: Object, propertyKey: string) {
16+
}
17+
18+
export class B {
19+
@decorator
20+
x = new A();
21+
}

0 commit comments

Comments
 (0)