@hqjs/babel-plugin-add-type-metadata
Advanced tools
Comparing version 0.0.1 to 0.0.2
42
index.js
@@ -1,2 +0,2 @@ | ||
module.exports = function({ types: t }) { | ||
module.exports = function ({ types: t }) { | ||
return { | ||
@@ -14,24 +14,22 @@ visitor: { | ||
null); | ||
const typeMetadata = t.assignmentExpression( | ||
'=', | ||
t.memberExpression( | ||
t.identifier(cls.node.id.name), | ||
t.identifier('ctorParameters') | ||
), | ||
t.functionExpression( | ||
null, | ||
[], | ||
t.blockStatement([ | ||
t.returnStatement(t.arrayExpression(types.map(type => type ? | ||
t.objectExpression([ | ||
t.objectProperty( | ||
t.stringLiteral('type'), | ||
t.identifier(type) | ||
), | ||
]) : | ||
t.nullLiteral()))), | ||
]) | ||
) | ||
const ctorParameters = t.classMethod( | ||
'method', | ||
t.stringLiteral('ctorParameters'), | ||
[], | ||
t.blockStatement([ | ||
t.returnStatement(t.arrayExpression(types.map(type => type ? | ||
t.objectExpression([ | ||
t.objectProperty( | ||
t.stringLiteral('type'), | ||
t.identifier(type) | ||
), | ||
]) : | ||
t.nullLiteral()))), | ||
]), | ||
false, | ||
true | ||
); | ||
nodePath.insertAfter(t.expressionStatement(typeMetadata)); | ||
nodePath.get('body').unshiftContainer('body', ctorParameters); | ||
} | ||
@@ -38,0 +36,0 @@ }, |
{ | ||
"name": "@hqjs/babel-plugin-add-type-metadata", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Add plugin metadata", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -9,1 +9,41 @@ # babel-plugin-add-type-metadata | ||
# Transformation | ||
Plugin adds type metadata to classes to help Angular e.g. | ||
```ts | ||
... | ||
import { MessageService } from './message.service'; | ||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class HeroService { | ||
constructor(private messageService: MessageService) { } | ||
... | ||
} | ||
``` | ||
will turn into | ||
```ts | ||
... | ||
import { MessageService } from './message.service'; | ||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class HeroService { | ||
static "ctorParameters"() { | ||
return [{ | ||
"type": HeroService | ||
}]; | ||
} | ||
constructor(private messageService: MessageService) { } | ||
... | ||
} | ||
``` | ||
that works nice with decorator plugin. |
12211
49
38