Skip to content

Commit 0760d34

Browse files
committed
fix(reactant-module): fix @action check result value in dev mode
1 parent ed501dc commit 0760d34

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

packages/reactant-module/src/decorators/action.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,22 @@ const action = (
7676
Record<string, unknown>
7777
>(lastState, (draftState) => {
7878
stagedState = draftState;
79-
fn.apply(this, args);
79+
const result = fn.apply(this, args);
80+
if (__DEV__ && result !== undefined) {
81+
throw new Error(
82+
`The return value of the method '${key}' is not allowed.`
83+
);
84+
}
8085
});
8186
} else {
8287
state = produce<Record<string, unknown>>(lastState, (draftState) => {
8388
stagedState = draftState;
84-
fn.apply(this, args);
89+
const result = fn.apply(this, args);
90+
if (__DEV__ && result !== undefined) {
91+
throw new Error(
92+
`The return value of the method '${key}' is not allowed.`
93+
);
94+
}
8595
});
8696
}
8797
stagedState = undefined;

packages/reactant-module/test/decorators/action.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ describe('@action', () => {
3030
const { count } = this;
3131
this.count = count;
3232
}
33+
34+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
35+
// @ts-ignore
36+
@action
37+
unexpectedChange(): any {
38+
this.count = 0;
39+
return this.returnValue;
40+
}
41+
42+
returnValue: any = 0;
3343
}
3444
const ServiceIdentifiers = new Map();
3545
const modules = [Counter];
@@ -71,6 +81,30 @@ describe('@action', () => {
7181
`There are no state updates to method 'counter.noChange'`
7282
);
7383
warn.mockReset();
84+
85+
for (const value of [
86+
0,
87+
1,
88+
null,
89+
true,
90+
false,
91+
new Promise(() => {
92+
//
93+
}),
94+
function a() {
95+
//
96+
},
97+
{},
98+
[],
99+
Symbol(''),
100+
]) {
101+
expect(() => {
102+
counter.returnValue = value;
103+
counter.unexpectedChange();
104+
}).toThrowError(
105+
/The return value of the method 'unexpectedChange' is not allowed./
106+
);
107+
}
74108
});
75109

76110
test('enable `autoFreeze` in devOptions', () => {

0 commit comments

Comments
 (0)