@@ -372,6 +372,7 @@ func (w *Writer) composeHook(hook Hook, t1, t2, dst string) {
372372 w .line (`}` )
373373}
374374
375+ //nolint:funlen
375376func (w * Writer ) composeHookCall (fn * Func , h1 , h2 string ) {
376377 w .newScope (func () {
377378 w .capture (h1 , h2 )
@@ -385,70 +386,57 @@ func (w *Writer) composeHookCall(fn *Func, h1, h2 string) {
385386 w .funcResults (fn )
386387 w .line (` {` )
387388 w .line (`if options.panicCallback != nil {` )
388- w .addPanicCallback ()
389- w .line ("}" )
390- if fn .HasResult () {
391- rs := w .declareResults (fn )
392- w .addHookCalls (fn , h1 , h2 , args , rs )
393- w .addReturnStatement (fn , rs )
394- } else {
395- w .addHookCalls (fn , h1 , h2 , args , nil )
396- }
397- w .line (`}` )
398- })
399- })
400- }
401-
402- func (w * Writer ) addPanicCallback () {
403- w .block (func () {
404- w .line ("defer func() {" )
405- w .block (func () {
406- w .line ("if e := recover(); e != nil {" )
407389 w .block (func () {
408- w .line (`options.panicCallback(e)` )
390+ w .line ("defer func() {" )
391+ w .block (func () {
392+ w .line ("if e := recover(); e != nil {" )
393+ w .block (func () {
394+ w .line (`options.panicCallback(e)` )
395+ })
396+ w .line ("}" )
397+ })
398+ w .line ("}()" )
409399 })
410400 w .line ("}" )
411- })
412- w .line ("}()" )
413- })
414- }
415-
416- func (w * Writer ) declareResults (fn * Func ) []string {
417- r1 := w .declare ("r" )
418- r2 := w .declare ("r" )
419- rs := []string {r1 , r2 }
420- w .code ("var " + r1 + ", " + r2 + " " )
421- w .funcResults (fn )
422- _ = w .bw .WriteByte ('\n' )
423- w .atEOL = true
424-
425- return rs
426- }
427-
428- func (w * Writer ) addHookCalls (fn * Func , h1 , h2 string , args , rs []string ) {
429- for i , h := range []string {h1 , h2 } {
430- w .line ("if " + h + " != nil {" )
431- w .block (func () {
401+ var (
402+ r1 string
403+ r2 string
404+ rs []string
405+ )
406+ if fn .HasResult () {
407+ r1 = w .declare ("r" )
408+ r2 = w .declare ("r" )
409+ rs = []string {r1 , r2 }
410+ w .code ("var " + r1 + ", " + r2 + " " )
411+ w .funcResults (fn )
412+ _ = w .bw .WriteByte ('\n' )
413+ w .atEOL = true
414+ }
415+ for i , h := range []string {h1 , h2 } {
416+ w .line ("if " + h + " != nil {" )
417+ w .block (func () {
418+ if fn .HasResult () {
419+ w .code (rs [i ], ` = ` ) //nolint:scopelint
420+ }
421+ w .code (h ) //nolint:scopelint
422+ w .call (args )
423+ })
424+ w .line ("}" )
425+ }
432426 if fn .HasResult () {
433- w .code (rs [i ], ` = ` ) //nolint:scopelint
427+ w .code (`return ` )
428+ switch x := fn .Result [0 ].(type ) {
429+ case * Func :
430+ w .composeHookCall (x , r1 , r2 )
431+ case * Trace :
432+ w .line (r1 , `.Compose(` , r2 , `)` )
433+ default :
434+ panic ("unknown result type" )
435+ }
434436 }
435- w .code (h ) //nolint:scopelint
436- w .call (args )
437437 })
438- w .line ("}" )
439- }
440- }
441-
442- func (w * Writer ) addReturnStatement (fn * Func , rs []string ) {
443- w .code (`return ` )
444- switch x := fn .Result [0 ].(type ) {
445- case * Func :
446- w .composeHookCall (x , rs [0 ], rs [1 ])
447- case * Trace :
448- w .line (rs [0 ], `.Compose(` , rs [1 ], `)` )
449- default :
450- panic ("unknown result type" )
451- }
438+ w .line (`}` )
439+ })
452440}
453441
454442func (w * Writer ) options (trace * Trace ) {
0 commit comments