Skip to content

Commit bfd5b2f

Browse files
authored
Fix decorator metadata references to type-only-imported namespaces (#44915)
* Add test * Fix metadata references to type-only-imported namespaces * Use `!!` instead of `|| false`
1 parent 7669bfb commit bfd5b2f

File tree

5 files changed

+130
-1
lines changed

5 files changed

+130
-1
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40480,9 +40480,14 @@ namespace ts {
4048040480
}
4048140481

4048240482
// Resolve the symbol as a value to ensure the type can be reached at runtime during emit.
40483+
let isTypeOnly = false;
40484+
if (isQualifiedName(typeName)) {
40485+
const rootValueSymbol = resolveEntityName(getFirstIdentifier(typeName), SymbolFlags.Value, /*ignoreErrors*/ true, /*dontResolveAlias*/ true, location);
40486+
isTypeOnly = !!rootValueSymbol?.declarations?.every(isTypeOnlyImportOrExportDeclaration);
40487+
}
4048340488
const valueSymbol = resolveEntityName(typeName, SymbolFlags.Value, /*ignoreErrors*/ true, /*dontResolveAlias*/ true, location);
40484-
const isTypeOnly = valueSymbol?.declarations?.every(isTypeOnlyImportOrExportDeclaration) || false;
4048540489
const resolvedSymbol = valueSymbol && valueSymbol.flags & SymbolFlags.Alias ? resolveAlias(valueSymbol) : valueSymbol;
40490+
isTypeOnly ||= !!valueSymbol?.declarations?.every(isTypeOnlyImportOrExportDeclaration);
4048640491

4048740492
// Resolve the symbol as a type so that we can provide a more useful hint for the type serializer.
4048840493
const typeSymbol = resolveEntityName(typeName, SymbolFlags.Type, /*ignoreErrors*/ true, /*dontResolveAlias*/ false, location);
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//// [tests/cases/conformance/decorators/decoratorMetadataWithTypeOnlyImport2.ts] ////
2+
3+
//// [services.ts]
4+
export namespace Services {
5+
export class Service {}
6+
}
7+
8+
//// [index.ts]
9+
import type { Services } from './services';
10+
11+
declare const decorator: any;
12+
export class Main {
13+
@decorator()
14+
field: Services.Service;
15+
}
16+
17+
18+
//// [services.js]
19+
"use strict";
20+
exports.__esModule = true;
21+
exports.Services = void 0;
22+
var Services;
23+
(function (Services) {
24+
var Service = /** @class */ (function () {
25+
function Service() {
26+
}
27+
return Service;
28+
}());
29+
Services.Service = Service;
30+
})(Services = exports.Services || (exports.Services = {}));
31+
//// [index.js]
32+
"use strict";
33+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
34+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
35+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
36+
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;
37+
return c > 3 && r && Object.defineProperty(target, key, r), r;
38+
};
39+
var __metadata = (this && this.__metadata) || function (k, v) {
40+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
41+
};
42+
exports.__esModule = true;
43+
exports.Main = void 0;
44+
var Main = /** @class */ (function () {
45+
function Main() {
46+
}
47+
__decorate([
48+
decorator(),
49+
__metadata("design:type", Function)
50+
], Main.prototype, "field");
51+
return Main;
52+
}());
53+
exports.Main = Main;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
=== tests/cases/conformance/decorators/services.ts ===
2+
export namespace Services {
3+
>Services : Symbol(Services, Decl(services.ts, 0, 0))
4+
5+
export class Service {}
6+
>Service : Symbol(Service, Decl(services.ts, 0, 27))
7+
}
8+
9+
=== tests/cases/conformance/decorators/index.ts ===
10+
import type { Services } from './services';
11+
>Services : Symbol(Services, Decl(index.ts, 0, 13))
12+
13+
declare const decorator: any;
14+
>decorator : Symbol(decorator, Decl(index.ts, 2, 13))
15+
16+
export class Main {
17+
>Main : Symbol(Main, Decl(index.ts, 2, 29))
18+
19+
@decorator()
20+
>decorator : Symbol(decorator, Decl(index.ts, 2, 13))
21+
22+
field: Services.Service;
23+
>field : Symbol(Main.field, Decl(index.ts, 3, 19))
24+
>Services : Symbol(Services, Decl(index.ts, 0, 13))
25+
>Service : Symbol(Services.Service, Decl(services.ts, 0, 27))
26+
}
27+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
=== tests/cases/conformance/decorators/services.ts ===
2+
export namespace Services {
3+
>Services : typeof Services
4+
5+
export class Service {}
6+
>Service : Service
7+
}
8+
9+
=== tests/cases/conformance/decorators/index.ts ===
10+
import type { Services } from './services';
11+
>Services : any
12+
13+
declare const decorator: any;
14+
>decorator : any
15+
16+
export class Main {
17+
>Main : Main
18+
19+
@decorator()
20+
>decorator() : any
21+
>decorator : any
22+
23+
field: Services.Service;
24+
>field : Services.Service
25+
>Services : any
26+
}
27+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// @experimentalDecorators: true
2+
// @emitDecoratorMetadata: true
3+
4+
5+
// @filename: services.ts
6+
export namespace Services {
7+
export class Service {}
8+
}
9+
10+
// @filename: index.ts
11+
import type { Services } from './services';
12+
13+
declare const decorator: any;
14+
export class Main {
15+
@decorator()
16+
field: Services.Service;
17+
}

0 commit comments

Comments
 (0)