forked from Typescript-TDD/ts-auto-mock
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(transformer): Support overloaded functions by attaching signa…
…tures 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.
- Loading branch information
1 parent
f28570c
commit 340b562
Showing
22 changed files
with
373 additions
and
95 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export function functionMethod(name: string, value: () => any): any { | ||
export function functionMethod(name: string, value: (...args: any[]) => any): any { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
return (): any => value(); | ||
return function(...args: any[]): any { | ||
return value.apply(this, args); | ||
}; | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export type TsAutoMockFeaturesOption = 'random'; | ||
export type TsAutoMockFeaturesOption = 'random' | 'overloads'; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { GetOptionByKey } from './options'; | ||
|
||
export function IsTsAutoMockOverloadsEnabled(): boolean { | ||
return GetOptionByKey('features').includes('overloads'); | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,12 @@ | ||
import * as ts from 'typescript'; | ||
import ts from 'typescript'; | ||
import { Scope } from '../../scope/scope'; | ||
import { PropertySignatureCache } from '../property/cache'; | ||
import { GetReturnTypeFromBodyDescriptor } from './bodyReturnType'; | ||
import { GetMethodDescriptor } from './method'; | ||
|
||
type functionAssignment = ts.ArrowFunction | ts.FunctionExpression; | ||
|
||
export function GetFunctionAssignmentDescriptor(node: functionAssignment, scope: Scope): ts.Expression { | ||
const property: ts.PropertyName = PropertySignatureCache.instance.get(); | ||
const returnValue: ts.Expression = GetReturnTypeFromBodyDescriptor(node, scope); | ||
|
||
return GetMethodDescriptor(property, returnValue); | ||
return GetMethodDescriptor(property, [node], scope); | ||
} |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,26 @@ | ||
import * as ts from 'typescript'; | ||
import ts from 'typescript'; | ||
import { Scope } from '../../scope/scope'; | ||
import { GetDescriptor } from '../descriptor'; | ||
import { GetFunctionReturnType } from './functionReturnType'; | ||
import { TypeChecker } from '../../typeChecker/typeChecker'; | ||
|
||
import { GetMethodDescriptor } from './method'; | ||
|
||
export function GetMethodDeclarationDescriptor(node: ts.MethodDeclaration | ts.FunctionDeclaration, scope: Scope): ts.Expression { | ||
const returnTypeNode: ts.Node = GetFunctionReturnType(node); | ||
const returnType: ts.Expression = GetDescriptor(returnTypeNode, scope); | ||
const declarationType: ts.Type | undefined = TypeChecker().getTypeAtLocation(node); | ||
const methodDeclarations: Array<ts.MethodDeclaration | ts.FunctionDeclaration> = declarationType.symbol.declarations | ||
.filter( | ||
(declaration: ts.Declaration): declaration is ts.MethodDeclaration | ts.FunctionDeclaration => | ||
ts.isMethodDeclaration(declaration) || ts.isFunctionDeclaration(declaration) | ||
); | ||
|
||
if (!methodDeclarations.length) { | ||
methodDeclarations.push(node); | ||
} | ||
|
||
if (!node.name) { | ||
throw new Error( | ||
`The transformer couldn't determine the name of ${node.getText()}. Please report this incident.`, | ||
); | ||
} | ||
|
||
return GetMethodDescriptor(node.name, returnType); | ||
return GetMethodDescriptor(node.name, methodDeclarations, scope); | ||
} |
Oops, something went wrong.