@travetto/di
Advanced tools
Comparing version 0.1.6 to 0.2.0
@@ -7,8 +7,8 @@ { | ||
"dependencies": { | ||
"@travetto/config": "^0.1.4", | ||
"@travetto/registry": "^0.1.5" | ||
"@travetto/config": "^0.2.0", | ||
"@travetto/registry": "^0.2.0" | ||
}, | ||
"description": "Dependency registration/management and injection support.", | ||
"devDependencies": { | ||
"@travetto/test": "^0.1.6" | ||
"@travetto/test": "^0.2.0" | ||
}, | ||
@@ -33,3 +33,3 @@ "homepage": "https://travetto.io", | ||
}, | ||
"version": "0.1.6" | ||
"version": "0.2.0" | ||
} |
@@ -1,5 +0,6 @@ | ||
import * as ts from 'typescript'; | ||
import { TransformUtil, State } from '@travetto/compiler'; | ||
import { TransformUtil, TransformerState } from '@travetto/compiler'; | ||
import { ConfigLoader } from '@travetto/config'; | ||
const INJECTABLE_MOD = require.resolve('../src/decorator/injectable'); | ||
const INJECTABLES = TransformUtil.buildImportAliasMap({ | ||
@@ -10,13 +11,11 @@ ...ConfigLoader.get('registry.injectable'), | ||
interface DiState extends State { | ||
interface DiState extends TransformerState { | ||
inInjectable: boolean; | ||
decorators: { [key: string]: ts.Expression }; | ||
import?: ts.Identifier; | ||
} | ||
function processDeclaration(state: State, param: ts.ParameterDeclaration | ts.PropertyDeclaration) { | ||
const injection = TransformUtil.findAnyDecorator(param, { Inject: new Set(['@travetto/di']) }, state); | ||
function processDeclaration(state: TransformerState, param: ts.ParameterDeclaration | ts.PropertyDeclaration) { | ||
const injection = TransformUtil.findAnyDecorator(state, param, { Inject: new Set(['@travetto/di']) }); | ||
if (injection || ts.isParameter(param)) { | ||
const finalTarget = TransformUtil.importIfExternal(param.type!, state); | ||
const finalTarget = TransformUtil.importTypeIfExternal(state, param.type!); | ||
@@ -57,26 +56,5 @@ let injectConfig = TransformUtil.getPrimaryArgument<ts.ObjectLiteralExpression>(injection); | ||
function createInjectDecorator(state: DiState, name: string, contents?: ts.Expression) { | ||
if (!state.decorators[name]) { | ||
if (!state.import) { | ||
state.import = ts.createIdentifier(`import_Injectable`); | ||
state.newImports.push({ | ||
ident: state.import, | ||
path: require.resolve('../src/decorator/injectable') | ||
}); | ||
} | ||
const ident = ts.createIdentifier(name); | ||
state.decorators[name] = ts.createPropertyAccess(state.import, ident); | ||
} | ||
return ts.createDecorator( | ||
ts.createCall( | ||
state.decorators[name], | ||
undefined, | ||
contents ? [contents] : [] | ||
) | ||
); | ||
} | ||
function visitNode<T extends ts.Node>(context: ts.TransformationContext, node: T, state: DiState): T { | ||
if (ts.isClassDeclaration(node)) { // Class declaration | ||
const foundDec = TransformUtil.findAnyDecorator(node, INJECTABLES, state); | ||
const foundDec = TransformUtil.findAnyDecorator(state, node, INJECTABLES); | ||
@@ -106,8 +84,8 @@ if (foundDec) { // Constructor | ||
declTemp.push(createInjectDecorator(state, 'InjectArgs', injectArgs)); | ||
declTemp.push(TransformUtil.createDecorator(state, INJECTABLE_MOD, 'InjectArgs', injectArgs)); | ||
// Add injectable decorator if not there (for aliased decorators) | ||
let injectable = TransformUtil.findAnyDecorator(node, { Injectable: new Set(['@travetto/di']) }, state); | ||
let injectable = TransformUtil.findAnyDecorator(state, node, { Injectable: new Set(['@travetto/di']) }); | ||
if (!injectable) { | ||
injectable = createInjectDecorator(state, 'Injectable'); | ||
injectable = TransformUtil.createDecorator(state, INJECTABLE_MOD, 'Injectable'); | ||
declTemp.push(injectable); | ||
@@ -157,3 +135,3 @@ } else { | ||
if (expr) { | ||
const final = createInjectDecorator(state, 'Inject', expr); | ||
const final = TransformUtil.createDecorator(state, INJECTABLE_MOD, 'Inject', expr); | ||
const finalDecs = ((node.decorators as any as ts.Decorator[]) || []) | ||
@@ -179,3 +157,3 @@ .filter(x => TransformUtil.getDecoratorIdent(x).text !== 'Inject'); | ||
// Factory for static methods | ||
const foundDec = TransformUtil.findAnyDecorator(node, { InjectableFactory: new Set(['@travetto/di']) }, state); | ||
const foundDec = TransformUtil.findAnyDecorator(state, node, { InjectableFactory: new Set(['@travetto/di']) }); | ||
const decls = node.decorators; | ||
@@ -215,3 +193,3 @@ | ||
if (node.type && target === undefined) { // TODO: infer from typings, not just text? | ||
target = TransformUtil.importIfExternal(node.type!, state); | ||
target = TransformUtil.importTypeIfExternal(state, node.type!); | ||
} | ||
@@ -258,3 +236,2 @@ const args = TransformUtil.extendObjectLiteral({ | ||
inInjectable: false, | ||
decorators: {} | ||
}), visitNode), | ||
@@ -261,0 +238,0 @@ priority: 11, |
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"target": "es2017", | ||
"target": "es2018", | ||
"strict": true, | ||
@@ -6,0 +6,0 @@ "noStrictGenericChecks": true, |
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
37410
387
18
981
7
+ Added@travetto/base@0.2.11(transitive)
+ Added@travetto/compiler@0.2.10(transitive)
+ Added@travetto/config@0.2.10(transitive)
+ Added@travetto/registry@0.2.10(transitive)
+ Addedcommander@2.20.3(transitive)
+ Addedtypescript@3.9.10(transitive)
- Removed@travetto/base@0.1.4(transitive)
- Removed@travetto/compiler@0.1.5(transitive)
- Removed@travetto/config@0.1.4(transitive)
- Removed@travetto/registry@0.1.5(transitive)
- Removedtypescript@2.9.2(transitive)
Updated@travetto/config@^0.2.0
Updated@travetto/registry@^0.2.0