Skip to content

Commit b49cf0e

Browse files
committed
fix(di): register zero-parameter arrow factories without annotate()
register(name, fn) sent every function through the legacy provider kind, whose annotate() regex-parses the function source. An arrow factory has no parameters to resolve and cannot be constructed, so it now registers as a plain factory provider with identical call semantics. Command dispatchers and other new-path registrations no longer depend on annotate().
1 parent c6c794a commit b49cf0e

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

lib/common/yok.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,18 @@ export class Yok extends Injector implements IInjector {
475475

476476
shared = shared === undefined ? true : shared;
477477
if (_.isFunction(resolver)) {
478+
if (resolver.length === 0 && !resolver.prototype) {
479+
// A prototype-less zero-parameter function (an arrow factory) cannot
480+
// be `new`ed and has no parameters to resolve, so annotate() would
481+
// contribute nothing — register it as a plain factory.
482+
super.register({
483+
provide: nameOrProviders,
484+
useFactory: <() => any>resolver,
485+
shared,
486+
});
487+
return;
488+
}
489+
478490
// Classes and factory functions alike: the legacy provider kind
479491
// annotate()s the resolver and calls or news it by casing.
480492
super.register({

test/command-registration.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,25 @@ describe("yok: command registration", () => {
5959
]);
6060
});
6161
});
62+
63+
describe("register with a zero-parameter arrow factory", () => {
64+
it("resolves by calling the factory, without annotate()", () => {
65+
const factory = () => ({ value: 42 });
66+
injector.register("arrowFactoryService", factory);
67+
68+
const instance = injector.resolve("arrowFactoryService");
69+
70+
assert.strictEqual(instance.value, 42);
71+
assert.isUndefined((<any>factory).$inject);
72+
});
73+
74+
it("stays shared by default", () => {
75+
injector.register("sharedArrowFactoryService", () => ({}));
76+
77+
assert.strictEqual(
78+
injector.resolve("sharedArrowFactoryService"),
79+
injector.resolve("sharedArrowFactoryService"),
80+
);
81+
});
82+
});
6283
});

0 commit comments

Comments
 (0)