diff --git a/packages/context/src/inject.ts b/packages/context/src/inject.ts index e5741719cea2..ac2510ea759e 100644 --- a/packages/context/src/inject.ts +++ b/packages/context/src/inject.ts @@ -62,7 +62,7 @@ export interface InjectionMetadata { */ export type BindingSelector = | BindingAddress - | BindingFilter; + | BindingFilter; /** * Descriptor for an injection point @@ -311,6 +311,12 @@ export namespace inject { }; } +function isBindingAddress( + bindingSelector: BindingSelector, +): bindingSelector is BindingAddress { + return typeof bindingSelector !== 'function'; +} + function resolveAsGetter( ctx: Context, injection: Readonly, @@ -324,14 +330,14 @@ function resolveAsGetter( `The type of ${targetName} (${targetType.name}) is not a Getter function`, ); } - if (typeof injection.bindingSelector === 'function') { + const bindingSelector = injection.bindingSelector; + if (!isBindingAddress(bindingSelector)) { return resolveByFilter(ctx, injection, session); } // We need to clone the session for the getter as it will be resolved later session = ResolutionSession.fork(session); return function getter() { - const key = injection.bindingSelector as BindingAddress; - return ctx.get(key, { + return ctx.get(bindingSelector, { session, optional: injection.metadata && injection.metadata.optional, }); @@ -340,17 +346,21 @@ function resolveAsGetter( function resolveAsSetter(ctx: Context, injection: Injection) { const targetType = inspectTargetType(injection); + const targetName = ResolutionSession.describeInjection(injection)!.targetName; if (targetType && targetType !== Function) { - const targetName = ResolutionSession.describeInjection(injection)! - .targetName; throw new Error( `The type of ${targetName} (${targetType.name}) is not a Setter function`, ); } + const bindingSelector = injection.bindingSelector; + if (!isBindingAddress(bindingSelector)) { + throw new Error( + `@inject.setter for (${targetType.name}) does not allow BindingFilter`, + ); + } // No resolution session should be propagated into the setter return function setter(value: unknown) { - const key = injection.bindingSelector as BindingAddress; - ctx.bind(key).to(value); + ctx.bind(bindingSelector).to(value); }; } diff --git a/packages/context/test/acceptance/context-view.acceptance.ts b/packages/context/test/acceptance/context-view.acceptance.ts index 4c48cff33947..e7d476c4e0e5 100644 --- a/packages/context/test/acceptance/context-view.acceptance.ts +++ b/packages/context/test/acceptance/context-view.acceptance.ts @@ -155,7 +155,6 @@ describe('ContextEventListener - listens on matching bindings', () => { server.subscribe(contextListener); givenController(server, '1'); givenController(server.parent!, '2'); - return contextListener; } function givenController(_ctx: Context, _name: string) { diff --git a/packages/context/test/unit/context.unit.ts b/packages/context/test/unit/context.unit.ts index f5ad6c528e3e..11c352b04266 100644 --- a/packages/context/test/unit/context.unit.ts +++ b/packages/context/test/unit/context.unit.ts @@ -712,7 +712,6 @@ describe('Context', () => { filter: binding => false, listen: (event, binding) => {}, }; - return listener; } }); diff --git a/packages/context/test/unit/inject-view.unit.ts b/packages/context/test/unit/inject-view.unit.ts index ffe6ea3df835..a37e167573f3 100644 --- a/packages/context/test/unit/inject-view.unit.ts +++ b/packages/context/test/unit/inject-view.unit.ts @@ -16,7 +16,9 @@ import { describe('@inject.view', async () => { let ctx: Context; - beforeEach(() => (ctx = givenContext())); + beforeEach(() => { + ctx = givenContext(); + }); class MyControllerWithGetter { @inject.view(filterByTag('foo'), {watch: true}) @@ -70,7 +72,9 @@ describe('@inject.view', async () => { describe('@inject with filter function', async () => { let ctx: Context; - beforeEach(() => (ctx = givenContext())); + beforeEach(() => { + ctx = givenContext(); + }); class MyControllerWithGetter { @inject.getter(filterByTag('foo'), {watch: true})