Skip to content

Commit 12ca0ce

Browse files
committed
feat: add fallback method
Useful for incorrect typings
1 parent df488ef commit 12ca0ce

2 files changed

Lines changed: 56 additions & 13 deletions

File tree

src/patch-method.ts

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ export default function patchMethod<
3232
this: Instance,
3333
superMethod: SuperMethod,
3434
...args: Parameters<SuperMethod>
35-
) => ReturnType<SuperMethod>
35+
) => ReturnType<SuperMethod>,
36+
fallback?: (this: Instance, ...args: Parameters<SuperMethod>) => ReturnType<SuperMethod>
3637
): Class {
37-
const superMethod: SuperMethod = klass.prototype[methodName]
38+
const superMethod: SuperMethod = klass.prototype[methodName] || fallback
3839

3940
klass.prototype[methodName] = function(
4041
this: Instance,
@@ -70,11 +71,21 @@ export function beforeMethod<
7071
Instance extends InstanceType<Class>,
7172
K extends PropertiesOfType<Instance, Function>,
7273
SuperMethod extends Instance[K]
73-
>(klass: Class, methodName: K, fn: (this: Instance, ...args: Parameters<SuperMethod>) => any) {
74-
return patchMethod(klass, methodName, function(superMethod, ...args) {
75-
fn.call(this, ...args)
76-
return superMethod(...args)
77-
})
74+
>(
75+
klass: Class,
76+
methodName: K,
77+
fn: (this: Instance, ...args: Parameters<SuperMethod>) => any,
78+
fallback?: (this: Instance, ...args: Parameters<SuperMethod>) => ReturnType<SuperMethod>
79+
) {
80+
return patchMethod(
81+
klass,
82+
methodName,
83+
function(superMethod, ...args) {
84+
fn.call(this, ...args)
85+
return superMethod(...args)
86+
},
87+
fallback
88+
)
7889
}
7990

8091
/**
@@ -109,11 +120,17 @@ export function afterMethod<
109120
this: Instance,
110121
returnValue: ReturnType<SuperMethod>,
111122
...args: Parameters<SuperMethod>
112-
) => any
123+
) => any,
124+
fallback?: (this: Instance, ...args: Parameters<SuperMethod>) => ReturnType<SuperMethod>
113125
) {
114-
return patchMethod(klass, methodName, function(superMethod, ...args) {
115-
const returnValue = superMethod(...args)
116-
fn.call(this, returnValue, ...args)
117-
return returnValue
118-
})
126+
return patchMethod(
127+
klass,
128+
methodName,
129+
function(superMethod, ...args) {
130+
const returnValue = superMethod(...args)
131+
fn.call(this, returnValue, ...args)
132+
return returnValue
133+
},
134+
fallback
135+
)
119136
}

test/patch-method.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,32 @@ describe('patchMethod', () => {
5858

5959
expect(derived.bar('test')).toEqual('testcalled')
6060
})
61+
62+
it('accepts a fallback method', () => {
63+
class Foo {
64+
// @ts-ignore
65+
bar: (value: string) => string
66+
}
67+
68+
patchMethod(
69+
Foo,
70+
'bar',
71+
function(superMethod, value) {
72+
expect(this).toBeInstanceOf(Foo)
73+
expect(value).toEqual('test')
74+
return superMethod(value) + 'called'
75+
},
76+
function(value) {
77+
expect(this).toBeInstanceOf(Foo)
78+
expect(value).toEqual('test')
79+
return 'fallback' + value
80+
}
81+
)
82+
83+
const foo = new Foo()
84+
85+
expect(foo.bar('test')).toEqual('fallbacktestcalled')
86+
})
6187
})
6288

6389
describe('beforeMethod', () => {

0 commit comments

Comments
 (0)