File tree Expand file tree Collapse file tree 2 files changed +46
-2
lines changed Expand file tree Collapse file tree 2 files changed +46
-2
lines changed Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff 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+ / T h e r e t u r n v a l u e o f t h e m e t h o d ' u n e x p e c t e d C h a n g e ' i s n o t a l l o w e d ./
106+ ) ;
107+ }
74108 } ) ;
75109
76110 test ( 'enable `autoFreeze` in devOptions' , ( ) => {
You can’t perform that action at this time.
0 commit comments