@@ -225,14 +225,14 @@ function localDeclarations(sf: ts.SourceFile): LocalDeclarations {
225225}
226226
227227/**
228- * Block bodies of the exported `loader`/`action` handlers, the region a whole-body wrapper wraps. Reads
228+ * Bodies of the exported `loader`/`action` handlers, the region a whole-body wrapper wraps. Reads
229229 * the same four export forms `scan.ts` reads, and `wraps a body in every non-delegating entry point the
230230 * scanner finds` pins that so the harness cannot lag the scanner again.
231231 *
232232 * Not a retreat from the independence `collectNamedHandlers` keeps: that is about where a HANDLER sits
233233 * inside a builder's argument, which is a judgement. Which exports exist is not.
234234 */
235- function entryBodies ( sf : ts . SourceFile ) : ts . Block [ ] {
235+ function entryBodies ( sf : ts . SourceFile ) : ts . ConciseBody [ ] {
236236 const functions : EntryFunction [ ] = [ ] ;
237237 const locals = localDeclarations ( sf ) ;
238238 const fromLocal = ( name : string ) => {
@@ -284,11 +284,11 @@ function entryBodies(sf: ts.SourceFile): ts.Block[] {
284284 // action of `const { loader, action } = createActionApiRoute({ handler })` resolve to the same
285285 // node. Wrapping it twice would splice the same text in twice at the same offset, because
286286 // `applyEdits` treats two zero-width inserts at one position as non-overlapping.
287- const bodies : ts . Block [ ] = [ ] ;
288- for ( const fn of new Set ( functions ) ) {
289- if ( fn . body && ts . isBlock ( fn . body ) ) bodies . push ( fn . body ) ;
290- }
291- return bodies ;
287+ return [ ... new Set ( functions ) ] . flatMap ( ( fn ) => ( fn . body ? [ fn . body ] : [ ] ) ) ;
288+ }
289+
290+ function entryBlocks ( sf : ts . SourceFile ) : ts . Block [ ] {
291+ return entryBodies ( sf ) . filter ( ts . isBlock ) ;
292292}
293293
294294// -- generic mutation shapes ----------------------------------------------------------------
@@ -416,9 +416,26 @@ function wrapEveryBody(
416416 apply ( fileName , source ) {
417417 const sf = parse ( fileName , source ) ;
418418 const edits : Edit [ ] = [ ] ;
419- for ( const body of entryBodies ( sf ) ) {
420- edits . push ( insert ( body . getStart ( ) + 1 , `\n${ open } \n` ) ) ;
421- edits . push ( insert ( body . end - 1 , `\n${ close } \n` ) ) ;
419+ const bodies = entryBodies ( sf ) ;
420+ const hasBlock = bodies . some ( ts . isBlock ) ;
421+ for ( const body of bodies ) {
422+ if ( ts . isBlock ( body ) ) {
423+ edits . push ( insert ( body . getStart ( ) + 1 , `\n${ open } \n` ) ) ;
424+ edits . push ( insert ( body . end - 1 , `\n${ close } \n` ) ) ;
425+ } else {
426+ const expression = unwrap ( body ) ;
427+ // A redirect-only loader is not the substantive handler of a mixed route.
428+ if (
429+ hasBlock &&
430+ ts . isCallExpression ( expression ) &&
431+ ts . isIdentifier ( expression . expression ) &&
432+ expression . expression . text === "redirect"
433+ ) {
434+ continue ;
435+ }
436+ edits . push ( insert ( body . getStart ( ) , `{\n${ open } \nreturn (` ) ) ;
437+ edits . push ( insert ( body . end , `);\n${ close } \n}` ) ) ;
438+ }
422439 }
423440 return applyEdits ( source , edits ) ;
424441 } ,
@@ -811,7 +828,7 @@ export const MUTATIONS: Mutation[] = [
811828 apply ( fileName , source ) {
812829 const sf = parse ( fileName , source ) ;
813830 const edits : Edit [ ] = [ ] ;
814- for ( const body of entryBodies ( sf ) ) {
831+ for ( const body of entryBlocks ( sf ) ) {
815832 edits . push (
816833 insert (
817834 body . getStart ( ) + 1 ,
@@ -831,7 +848,7 @@ export const MUTATIONS: Mutation[] = [
831848 apply ( fileName , source ) {
832849 const sf = parse ( fileName , source ) ;
833850 const edits : Edit [ ] = [ ] ;
834- for ( const body of entryBodies ( sf ) ) {
851+ for ( const body of entryBlocks ( sf ) ) {
835852 edits . push (
836853 insert (
837854 body . getStart ( ) + 1 ,
@@ -999,7 +1016,7 @@ export const MUTATIONS: Mutation[] = [
9991016 const sf = parse ( fileName , source ) ;
10001017 const edits : Edit [ ] = [ ] ;
10011018 let n = 0 ;
1002- for ( const body of entryBodies ( sf ) ) {
1019+ for ( const body of entryBlocks ( sf ) ) {
10031020 forEachNode ( body , ( node ) => {
10041021 if ( ! ts . isTryStatement ( node ) ) return ;
10051022 if ( ! node . parent || ! ts . isBlock ( node . parent ) ) return ;
0 commit comments