feat(transformer): Support overloaded functions by attaching signatures on use - #390
Open
martinjlowm wants to merge 4 commits into
Conversation
…es 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.
martinjlowm
force-pushed
the
feature/support-overloads-through-signature-attachment
branch
from
June 20, 2020 11:52
19df57e to
c1f7cb2
Compare
martinjlowm
force-pushed
the
feature/support-overloads-through-signature-attachment
branch
from
June 20, 2020 20:33
da5ce65 to
bd4d366
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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. Thetransformer changes transform functions in the following way.
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.
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.:
It should be noted, that if spies are introduced using the method provider, then
thiswill be occupied by the signature key.This is an alternative implementation of the feature(s) covered in #303 and #313