Skip to content
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

Decorator type metadata #2589

Merged
merged 18 commits into from
Apr 6, 2015
Merged

Decorator type metadata #2589

merged 18 commits into from
Apr 6, 2015

Conversation

rbuckton
Copy link
Member

@rbuckton rbuckton commented Apr 2, 2015

Resolves #2577

Adds support behind an experimental compiler option to emit design-time type metadata for decorated declarations in source.

  • Adds a __metadata helper that depends on a polyfill for a proposed Reflect.metadata decorator.
  • Adds a __param helper decorator used to apply parameter decorators after metadata is applied and before the method's decorators are run.
  • Emits calls to __metadata for class and member (property and method) decorators
  • For a class declaration, emits the type metadata for constructor parameters of the constructor with a body.
  • For a method declaration, emits the type metadata for the member, its parameters, and its return type.
  • For an accessor or property declaration, emits the type metadata for the member.
  • Adds compiler option to enable experimental metadata support (--emitDecoratorMetadata).

A few notes on metadata:

  • Type metadata uses the metadata key "design:type".
  • Parameter type metadata uses the metadata key "design:paramtypes".
  • Return type metadata uses the metadata key "design:returntype".

@rbuckton rbuckton changed the title Decorators types Decorator type metadata Apr 2, 2015
//
function emitDecoratorArray(decorators: NodeArray<Decorator>, writeComma: boolean, prefix?: string, postfix?: string): boolean {
if (decorators) {
let decoratorCount = decorators ? decorators.length : 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you already check for decorators is truthy.

function checkParameterTypeAnnotationsAsExpressions(node: FunctionLikeDeclaration) {
// ensure all type annotations with a value declaration are checked as an expression
if (node) {
forEach(node.parameters, checkTypeAnnotationAsExpression);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will stop with the first checkTypeAnnotationAsExpression that returns something 'truthy'. Is that what you wanted?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checkTypeAnnotationAsExpression returns undefined, so generally this is fine. I have been periodically migrating these to for..of since its available in the compiler now, though it wasn't when this was originally written.

for (var i = 0; i < parameterCount; i++) {
if (parameters[i].dotDotDotToken) {
var parameterType = parameters[i].type;
if (parameterType.kind === SyntaxKind.ArrayType) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we're doing this stuff syntactically only? Is that ok? Should we consider using the type information isntead?

rbuckton added a commit that referenced this pull request Apr 6, 2015
Experimental support for decorator type metadata. 
NOTE: Requires a polyfill for `Reflect.metadata` which has not yet been considered by TC39 for ES7.
@rbuckton rbuckton merged commit e195d89 into master Apr 6, 2015
@rbuckton rbuckton deleted the decorators_types branch April 6, 2015 19:50
@basarat basarat mentioned this pull request Apr 6, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Emit serialized design-time type metadata for decorators
4 participants