@hqjs/babel-plugin-add-decorators-metadata
Advanced tools
Comparing version 0.0.1 to 0.0.2
44
index.js
@@ -0,2 +1,8 @@ | ||
const ANGULAR_PROVIDERS_DECORATORS = ['NgModule', 'Component']; | ||
module.exports = function({ types: t }) { | ||
const args = t.identifier('args'); | ||
const decorators = t.identifier('decorators'); | ||
const provide = t.identifier('provide'); | ||
const type = t.identifier('type'); | ||
return { | ||
@@ -13,14 +19,30 @@ visitor: { | ||
t.identifier(name), | ||
t.identifier('decorators') | ||
decorators | ||
), | ||
t.arrayExpression(node.decorators.map(d => t.objectExpression([ | ||
t.objectProperty( | ||
t.identifier('type'), | ||
t.identifier(d.expression.callee.name) | ||
), | ||
t.objectProperty( | ||
t.identifier('args'), | ||
t.arrayExpression(d.expression.arguments) | ||
), | ||
]))) | ||
t.arrayExpression(node.decorators.map(d => t.isCallExpression(d.expression) ? | ||
t.objectExpression([ | ||
t.objectProperty( | ||
type, | ||
t.identifier(d.expression.callee.name) | ||
), | ||
t.objectProperty( | ||
args, | ||
t.arrayExpression(d.expression.arguments.map(arg => { | ||
if (!ANGULAR_PROVIDERS_DECORATORS.includes(d.expression.callee.name)) return arg; | ||
const provider = arg.properties.find(p => p.key && p.key.name === 'providers'); | ||
if (!provider) return arg; | ||
provider.value.elements = provider.value.elements.map(el => t.isObjectExpression(el) && el.properties.find(pr => pr.key.name === 'provide') ? | ||
el : | ||
t.objectExpression([ | ||
t.objectProperty(provide, el) | ||
]) | ||
); | ||
return arg; | ||
})) | ||
), | ||
]) : | ||
t.objectExpression([ | ||
t.objectProperty(type, d.expression) | ||
]) | ||
)) | ||
); | ||
@@ -27,0 +49,0 @@ |
{ | ||
"name": "@hqjs/babel-plugin-add-decorators-metadata", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Add decorators metadata", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,1 +8,62 @@ # babel-plugin-add-decorators-metadata | ||
``` | ||
# Usage | ||
```json | ||
{ | ||
"plugins": [["hqjs@babel-plugin-add-decorators-metadata"]] | ||
} | ||
``` | ||
# Transformation | ||
Transforms decorators into class metadata, takes care of wrapping `providers` property into an object. This transformations allow angular to use dependency injection.123 | ||
So the code | ||
```ts | ||
import { BrowserModule } from '@angular/platform-browser'; | ||
import { NgModule } from '@angular/core'; | ||
import {CoreConfig} from './app.service'; | ||
import { AppComponent } from './app.component'; | ||
@NgModule({ | ||
declarations: [ | ||
AppComponent | ||
], | ||
imports: [ | ||
BrowserModule | ||
], | ||
providers: [ | ||
CoreConfig | ||
], | ||
bootstrap: [AppComponent] | ||
}) | ||
export class AppModule { } | ||
``` | ||
will turn into | ||
```ts | ||
import { BrowserModule } from '@angular/platform-browser'; | ||
import { NgModule } from '@angular/core'; | ||
import { CoreConfig } from './app.service'; | ||
import { AppComponent } from './app.component'; | ||
@NgModule({ | ||
declarations: [AppComponent], | ||
imports: [BrowserModule], | ||
providers: [{ | ||
provide: CoreConfig | ||
}], | ||
bootstrap: [AppComponent] | ||
}) | ||
export class AppModule {} | ||
AppModule.decorators = [{ | ||
type: NgModule, | ||
args: [{ | ||
declarations: [AppComponent], | ||
imports: [BrowserModule], | ||
providers: [{ | ||
provide: CoreConfig | ||
}], | ||
bootstrap: [AppComponent] | ||
}] | ||
}]; | ||
``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13474
51
69