-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Emit fallback for decorator metadata for type only imports #39337
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
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
tests/baselines/reference/decoratorMetadataWithTypeOnlyImport.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
//// [tests/cases/conformance/decorators/decoratorMetadataWithTypeOnlyImport.ts] //// | ||
|
||
//// [service.ts] | ||
export class Service { | ||
} | ||
//// [component.ts] | ||
import type { Service } from "./service"; | ||
|
||
declare var decorator: any; | ||
|
||
@decorator | ||
class MyComponent { | ||
constructor(public Service: Service) { | ||
} | ||
|
||
@decorator | ||
method(x: this) { | ||
} | ||
} | ||
|
||
//// [service.js] | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Service = void 0; | ||
var Service = /** @class */ (function () { | ||
function Service() { | ||
} | ||
return Service; | ||
}()); | ||
exports.Service = Service; | ||
//// [component.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); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var MyComponent = /** @class */ (function () { | ||
function MyComponent(Service) { | ||
this.Service = Service; | ||
} | ||
MyComponent.prototype.method = function (x) { | ||
}; | ||
__decorate([ | ||
decorator, | ||
__metadata("design:type", Function), | ||
__metadata("design:paramtypes", [Object]), | ||
__metadata("design:returntype", void 0) | ||
], MyComponent.prototype, "method", null); | ||
MyComponent = __decorate([ | ||
decorator, | ||
__metadata("design:paramtypes", [Function]) | ||
], MyComponent); | ||
return MyComponent; | ||
}()); |
30 changes: 30 additions & 0 deletions
30
tests/baselines/reference/decoratorMetadataWithTypeOnlyImport.symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
=== tests/cases/conformance/decorators/service.ts === | ||
export class Service { | ||
>Service : Symbol(Service, Decl(service.ts, 0, 0)) | ||
} | ||
=== tests/cases/conformance/decorators/component.ts === | ||
import type { Service } from "./service"; | ||
>Service : Symbol(Service, Decl(component.ts, 0, 13)) | ||
|
||
declare var decorator: any; | ||
>decorator : Symbol(decorator, Decl(component.ts, 2, 11)) | ||
|
||
@decorator | ||
>decorator : Symbol(decorator, Decl(component.ts, 2, 11)) | ||
|
||
class MyComponent { | ||
>MyComponent : Symbol(MyComponent, Decl(component.ts, 2, 27)) | ||
|
||
constructor(public Service: Service) { | ||
>Service : Symbol(MyComponent.Service, Decl(component.ts, 6, 16)) | ||
>Service : Symbol(Service, Decl(component.ts, 0, 13)) | ||
} | ||
|
||
@decorator | ||
>decorator : Symbol(decorator, Decl(component.ts, 2, 11)) | ||
|
||
method(x: this) { | ||
>method : Symbol(MyComponent.method, Decl(component.ts, 7, 5)) | ||
>x : Symbol(x, Decl(component.ts, 10, 11)) | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
tests/baselines/reference/decoratorMetadataWithTypeOnlyImport.types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
=== tests/cases/conformance/decorators/service.ts === | ||
export class Service { | ||
>Service : Service | ||
} | ||
=== tests/cases/conformance/decorators/component.ts === | ||
import type { Service } from "./service"; | ||
>Service : Service | ||
|
||
declare var decorator: any; | ||
>decorator : any | ||
|
||
@decorator | ||
>decorator : any | ||
|
||
class MyComponent { | ||
>MyComponent : MyComponent | ||
|
||
constructor(public Service: Service) { | ||
>Service : Service | ||
} | ||
|
||
@decorator | ||
>decorator : any | ||
|
||
method(x: this) { | ||
>method : (x: this) => void | ||
>x : this | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
tests/cases/conformance/decorators/decoratorMetadataWithTypeOnlyImport.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// @experimentalDecorators: true | ||
// @emitDecoratorMetadata: true | ||
// @target: es5 | ||
// @module: commonjs | ||
// @filename: service.ts | ||
export class Service { | ||
} | ||
// @filename: component.ts | ||
import type { Service } from "./service"; | ||
|
||
declare var decorator: any; | ||
|
||
@decorator | ||
class MyComponent { | ||
constructor(public Service: Service) { | ||
} | ||
|
||
@decorator | ||
method(x: this) { | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do we know the type is objecty? You can
import type
a primitive.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TypeSerializationKind.ObjectType
tells thets
transform to emitObject
for the type metadata (which essentially meansany
).TypeSerializationKind.Unknown
means we should try to resolve a value at runtime using that name (by usingtypeof
testing), which would be incorrect behavior. Consider the following using the current version of the compiler (without this change):The
design:paramtypes
metadata forC
will incorrectly use the globalMap
object.For a type-only import we can be certain that the type cannot be referenced as a value, so we should emit
Object
instead of trying to resolve a value for it.