Skip to content

Commit

Permalink
Fix decorator metadata references to type-only-imported namespaces (#…
Browse files Browse the repository at this point in the history
…44915)

* Add test

* Fix metadata references to type-only-imported namespaces

* Use `!!` instead of `|| false`
  • Loading branch information
andrewbranch authored Aug 2, 2021
1 parent 7669bfb commit bfd5b2f
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40480,9 +40480,14 @@ namespace ts {
}

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

// Resolve the symbol as a type so that we can provide a more useful hint for the type serializer.
const typeSymbol = resolveEntityName(typeName, SymbolFlags.Type, /*ignoreErrors*/ true, /*dontResolveAlias*/ false, location);
Expand Down
53 changes: 53 additions & 0 deletions tests/baselines/reference/decoratorMetadataWithTypeOnlyImport2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//// [tests/cases/conformance/decorators/decoratorMetadataWithTypeOnlyImport2.ts] ////

//// [services.ts]
export namespace Services {
export class Service {}
}

//// [index.ts]
import type { Services } from './services';

declare const decorator: any;
export class Main {
@decorator()
field: Services.Service;
}


//// [services.js]
"use strict";
exports.__esModule = true;
exports.Services = void 0;
var Services;
(function (Services) {
var Service = /** @class */ (function () {
function Service() {
}
return Service;
}());
Services.Service = Service;
})(Services = exports.Services || (exports.Services = {}));
//// [index.js]
"use strict";
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);
};
exports.__esModule = true;
exports.Main = void 0;
var Main = /** @class */ (function () {
function Main() {
}
__decorate([
decorator(),
__metadata("design:type", Function)
], Main.prototype, "field");
return Main;
}());
exports.Main = Main;
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
=== tests/cases/conformance/decorators/services.ts ===
export namespace Services {
>Services : Symbol(Services, Decl(services.ts, 0, 0))

export class Service {}
>Service : Symbol(Service, Decl(services.ts, 0, 27))
}

=== tests/cases/conformance/decorators/index.ts ===
import type { Services } from './services';
>Services : Symbol(Services, Decl(index.ts, 0, 13))

declare const decorator: any;
>decorator : Symbol(decorator, Decl(index.ts, 2, 13))

export class Main {
>Main : Symbol(Main, Decl(index.ts, 2, 29))

@decorator()
>decorator : Symbol(decorator, Decl(index.ts, 2, 13))

field: Services.Service;
>field : Symbol(Main.field, Decl(index.ts, 3, 19))
>Services : Symbol(Services, Decl(index.ts, 0, 13))
>Service : Symbol(Services.Service, Decl(services.ts, 0, 27))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
=== tests/cases/conformance/decorators/services.ts ===
export namespace Services {
>Services : typeof Services

export class Service {}
>Service : Service
}

=== tests/cases/conformance/decorators/index.ts ===
import type { Services } from './services';
>Services : any

declare const decorator: any;
>decorator : any

export class Main {
>Main : Main

@decorator()
>decorator() : any
>decorator : any

field: Services.Service;
>field : Services.Service
>Services : any
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// @experimentalDecorators: true
// @emitDecoratorMetadata: true


// @filename: services.ts
export namespace Services {
export class Service {}
}

// @filename: index.ts
import type { Services } from './services';

declare const decorator: any;
export class Main {
@decorator()
field: Services.Service;
}

0 comments on commit bfd5b2f

Please sign in to comment.