You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(transformer): Support overloaded functions by attaching signatures on use
Add overloads feature flag that enables this feature. Enabling it makes the transformer process function calls if their declaration was previously marked for mocking (via getMethod). From a type-perspective, typed methods shouldn't bother to consider their inputs in order to determine the output in runtime. At transformation time, the type checker resolves the matching overload and that information can be used to attach to the function, by utilizing the "instance" (`this`) of it. The transformer changes transform functions in the following way. ``` mockedFunction() -> mockedFunction.apply(<signature>, []) ``` As for constructor instantiation signatures in interfaces, those can be wrapped by an intermediate function that will copy the mocked properties to preserve the instantiation behavior. ``` new mockedNewFunction() | `-> new (mockedNewFunction[<signature>] || (mockedNewFunction[<signature>] = function() { Object.assign(this, mockedNewFunction.apply(<signature>, [])); }))() ``` These attached interfaces will determine the branching at runtime and to reduce as much overhead as possible, all signatures of an overloaded function are mapped to the resolved return type and stored in a jump table, i.e.: ``` getMethod("functionName", function () { const jt = { ['<signature-1>']: () => <signature-1-return-descriptor>, ['<signature-2>']: () => <signature-2-return-descriptor>, ... }; return jt[this](); }) ``` It should be noted, that if spies are introduced using the method provider, then `this` will be occupied by the signature key.
0 commit comments