Skip to content

Commit 9c51a85

Browse files
authored
Synchronize getEntityNameForDecoratorMetadata and serializeUnionOrIntersectionType (microsoft#19879)
1 parent 36ce7ea commit 9c51a85

6 files changed

+145
-1
lines changed

src/compiler/checker.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -20191,7 +20191,16 @@ namespace ts {
2019120191
case SyntaxKind.IntersectionType:
2019220192
case SyntaxKind.UnionType:
2019320193
let commonEntityName: EntityName;
20194-
for (const typeNode of (<UnionOrIntersectionTypeNode>node).types) {
20194+
for (let typeNode of (<UnionOrIntersectionTypeNode>node).types) {
20195+
while (typeNode.kind === SyntaxKind.ParenthesizedType) {
20196+
typeNode = (typeNode as ParenthesizedTypeNode).type; // Skip parens if need be
20197+
}
20198+
if (typeNode.kind === SyntaxKind.NeverKeyword) {
20199+
continue; // Always elide `never` from the union/intersection if possible
20200+
}
20201+
if (!strictNullChecks && (typeNode.kind === SyntaxKind.NullKeyword || typeNode.kind === SyntaxKind.UndefinedKeyword)) {
20202+
continue; // Elide null and undefined from unions for metadata, just like what we did prior to the implementation of strict null checks
20203+
}
2019520204
const individualEntityName = getEntityNameForDecoratorMetadata(typeNode);
2019620205
if (!individualEntityName) {
2019720206
// Individual is something like string number

tests/baselines/reference/metadataOfClassFromAlias.js

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ var __metadata = (this && this.__metadata) || function (k, v) {
3535
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
3636
};
3737
Object.defineProperty(exports, "__esModule", { value: true });
38+
var auxiliry_1 = require("./auxiliry");
3839
function annotation() {
3940
return function (target) { };
4041
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//// [tests/cases/compiler/metadataReferencedWithinFilteredUnion.ts] ////
2+
3+
//// [Class1.ts]
4+
export class Class1 {
5+
}
6+
//// [Class2.ts]
7+
import { Class1 } from './Class1';
8+
9+
function decorate(target: any, propertyKey: string) {
10+
}
11+
12+
export class Class2 {
13+
@decorate
14+
get prop(): Class1 | undefined {
15+
return undefined;
16+
}
17+
}
18+
19+
//// [Class1.js]
20+
"use strict";
21+
Object.defineProperty(exports, "__esModule", { value: true });
22+
var Class1 = /** @class */ (function () {
23+
function Class1() {
24+
}
25+
return Class1;
26+
}());
27+
exports.Class1 = Class1;
28+
//// [Class2.js]
29+
"use strict";
30+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
31+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
32+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
33+
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;
34+
return c > 3 && r && Object.defineProperty(target, key, r), r;
35+
};
36+
var __metadata = (this && this.__metadata) || function (k, v) {
37+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
38+
};
39+
Object.defineProperty(exports, "__esModule", { value: true });
40+
var Class1_1 = require("./Class1");
41+
function decorate(target, propertyKey) {
42+
}
43+
var Class2 = /** @class */ (function () {
44+
function Class2() {
45+
}
46+
Object.defineProperty(Class2.prototype, "prop", {
47+
get: function () {
48+
return undefined;
49+
},
50+
enumerable: true,
51+
configurable: true
52+
});
53+
__decorate([
54+
decorate,
55+
__metadata("design:type", Class1_1.Class1),
56+
__metadata("design:paramtypes", [])
57+
], Class2.prototype, "prop", null);
58+
return Class2;
59+
}());
60+
exports.Class2 = Class2;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
=== tests/cases/compiler/Class1.ts ===
2+
export class Class1 {
3+
>Class1 : Symbol(Class1, Decl(Class1.ts, 0, 0))
4+
}
5+
=== tests/cases/compiler/Class2.ts ===
6+
import { Class1 } from './Class1';
7+
>Class1 : Symbol(Class1, Decl(Class2.ts, 0, 8))
8+
9+
function decorate(target: any, propertyKey: string) {
10+
>decorate : Symbol(decorate, Decl(Class2.ts, 0, 34))
11+
>target : Symbol(target, Decl(Class2.ts, 2, 18))
12+
>propertyKey : Symbol(propertyKey, Decl(Class2.ts, 2, 30))
13+
}
14+
15+
export class Class2 {
16+
>Class2 : Symbol(Class2, Decl(Class2.ts, 3, 1))
17+
18+
@decorate
19+
>decorate : Symbol(decorate, Decl(Class2.ts, 0, 34))
20+
21+
get prop(): Class1 | undefined {
22+
>prop : Symbol(Class2.prop, Decl(Class2.ts, 5, 21))
23+
>Class1 : Symbol(Class1, Decl(Class2.ts, 0, 8))
24+
25+
return undefined;
26+
>undefined : Symbol(undefined)
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
=== tests/cases/compiler/Class1.ts ===
2+
export class Class1 {
3+
>Class1 : Class1
4+
}
5+
=== tests/cases/compiler/Class2.ts ===
6+
import { Class1 } from './Class1';
7+
>Class1 : typeof Class1
8+
9+
function decorate(target: any, propertyKey: string) {
10+
>decorate : (target: any, propertyKey: string) => void
11+
>target : any
12+
>propertyKey : string
13+
}
14+
15+
export class Class2 {
16+
>Class2 : Class2
17+
18+
@decorate
19+
>decorate : (target: any, propertyKey: string) => void
20+
21+
get prop(): Class1 | undefined {
22+
>prop : Class1
23+
>Class1 : Class1
24+
25+
return undefined;
26+
>undefined : undefined
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// @experimentalDecorators: true
2+
// @emitDecoratorMetadata: true
3+
// @target: es5
4+
// @filename: Class1.ts
5+
export class Class1 {
6+
}
7+
// @filename: Class2.ts
8+
import { Class1 } from './Class1';
9+
10+
function decorate(target: any, propertyKey: string) {
11+
}
12+
13+
export class Class2 {
14+
@decorate
15+
get prop(): Class1 | undefined {
16+
return undefined;
17+
}
18+
}

0 commit comments

Comments
 (0)