@travetto/di
Advanced tools
Comparing version 0.0.10 to 0.0.11
@@ -22,3 +22,3 @@ { | ||
}, | ||
"version": "0.0.10" | ||
"version": "0.0.11" | ||
} |
@@ -6,4 +6,21 @@ import { InjectableConfig, Dependency } from '../types'; | ||
export function Injectable(config: Partial<InjectableConfig<any>> = {}): ClassDecorator { | ||
function extractSymbolOrConfig<T extends { qualifier?: Symbol }>(args: any[]) { | ||
const out = {} as T; | ||
if (args) { | ||
let extra = args[0]; | ||
if (typeof extra === 'symbol') { | ||
out.qualifier = extra; | ||
extra = args[1]; | ||
} | ||
Object.assign(out, extra); | ||
} | ||
return out; | ||
} | ||
export function Injectable(qualifier: symbol, config?: Partial<InjectableConfig<any>>): ClassDecorator; | ||
export function Injectable(config?: Partial<InjectableConfig<any>>): ClassDecorator; | ||
export function Injectable(...args: any[]): ClassDecorator { | ||
return (target: Class | any) => { | ||
const config = extractSymbolOrConfig(args) as Partial<InjectableConfig<any>>; | ||
config.class = target; | ||
@@ -26,15 +43,2 @@ if (typeof config.autoCreate === 'boolean') { | ||
function extractSymbolOrConfig<T extends { qualifier?: Symbol }>(args: any[]) { | ||
const out = {} as T; | ||
if (args) { | ||
let extra = args[0]; | ||
if (typeof extra === 'symbol') { | ||
out.qualifier = extra; | ||
extra = args[1]; | ||
} | ||
Object.assign(out, extra); | ||
} | ||
return out; | ||
} | ||
export function Inject(symbol: symbol, config?: InjectConfig): ParameterDecorator & PropertyDecorator; | ||
@@ -41,0 +45,0 @@ export function Inject(config?: InjectConfig): ParameterDecorator & PropertyDecorator; |
@@ -45,3 +45,3 @@ import * as ts from 'typescript'; | ||
if (optional === undefined && !!param.questionToken) { | ||
optional = ts.createFalse(); | ||
optional = ts.createTrue(); | ||
} | ||
@@ -80,3 +80,3 @@ | ||
function visitNode<T extends ts.Node>(context: ts.TransformationContext, node: T, state: DiState): T { | ||
if (ts.isClassDeclaration(node)) { | ||
if (ts.isClassDeclaration(node)) { // Class declaration | ||
const foundDec = TransformUtil.findAnyDecorator(node, INJECTABLES, state); | ||
@@ -111,2 +111,20 @@ | ||
declTemp.push(injectable); | ||
} else { | ||
let original = undefined; | ||
const callExpr = (injectable && injectable.expression as any as ts.CallExpression); | ||
let injectConfig = undefined; | ||
if (callExpr) { | ||
const args = callExpr.arguments! || []; | ||
injectConfig = args[0] as any; | ||
// Handle special case | ||
if (args[0] && ts.isIdentifier(args[0])) { | ||
original = args[0]; | ||
injectConfig = args[1] as any; | ||
} | ||
if (injectConfig === undefined) { | ||
injectConfig = TransformUtil.fromLiteral({}); | ||
} | ||
ts.updateCall(callExpr, callExpr.expression, callExpr.typeArguments, ts.createNodeArray([injectConfig])); | ||
} | ||
} | ||
@@ -113,0 +131,0 @@ |
@@ -40,4 +40,5 @@ import { Injectable, Inject, InjectableFactory } from '../src'; | ||
export const SERVICE_INHERIT_2 = Symbol() | ||
export const SERVICE_INHERIT_3 = Symbol() | ||
@Injectable({ qualifier: SERVICE_INHERIT_2 }) | ||
@Injectable(SERVICE_INHERIT_2) | ||
export class ServiceInherit2 extends ServiceInherit { | ||
@@ -61,4 +62,6 @@ age = 31; | ||
@InjectableFactory(CUSTOM_SERVICE_INHERIT) | ||
static getObject(@Inject(SERVICE_INHERIT_2) svc: ServiceInherit): ServiceInherit { | ||
return new ServiceInherit2(svc.db); | ||
static getObject(@Inject(SERVICE_INHERIT_2) svc?: ServiceInherit): ServiceInherit { | ||
const out = new ServiceInherit2(svc ? svc.db : new Database()); | ||
out.age = 11; | ||
return out; | ||
} | ||
@@ -65,0 +68,0 @@ |
@@ -83,2 +83,6 @@ import { DependencyRegistry } from '../src/service'; | ||
assert(inst.age === 11); | ||
//assert(inst.db.dbConfig === undefined); | ||
assert(inst.db.dbConfig); | ||
@@ -85,0 +89,0 @@ assert.ok(!inst.db.dbConfig.temp); |
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
29558
800