Skip to content
Source
Sign inSign up
TanStack/router

feat(router-generator): support higher-order wrappers around createFileRoute #7766

Opendhleong(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/7766/headopened

The route-file transform previously required the exported Route binding to be exactly createFileRoute('/path')({...}), so a route wrapped by a higher-order handler (e.g. wrap('x')(createFileRoute('/path')({...}))) was silently left untouched — no route-id injection, no import management.

This change will recursively locate the single nested createFileRoute(path)(options) call within the exported Route init, regardless of any wrapping calls. The wrapper stays opaque to the generator; only the inner constructor call is rewritten. Malformed wrapped routes now surface the same helpful error.

This is proposed as a first step toward unblocking https://github.com/TanStack/router/discussions/7619

Summary by CodeRabbit

  • New Features

    • Route generation now works with route definitions wrapped in higher-order expressions, preserving wrapper code while updating the route call inside.
    • Lazy route handling is now supported even when the route constructor appears within nested wrappers.
  • Bug Fixes

    • Improved error detection for malformed wrapped route definitions.
    • Added clearer failures when a wrapped route call is missing required options or contains multiple route constructor calls.
    1. Review Change Stack

      No actionable comments were generated in the recent review. 🎉

      ℹ️ Recent review info
      ⚙️ Run configuration

      Configuration used: defaults

      Review profile: CHILL

      Plan: Pro

      Run ID: 85054867-4ada-442a-8296-4022044ab7bd

      📥 Commits

      Reviewing files that changed from the base of the PR and between a3e24c35a58d9ac149a05d6321f68aa5d541b6b4 and 6620e350fc1295ddbf3b3995714eb745362bf317.

      📒 Files selected for processing (2)
      • packages/router-generator/src/transform/transform.ts
      • packages/router-generator/tests/transform.test.ts

      📝 Walkthrough

      Walkthrough

      The route-export analyzer in transform.ts now recursively searches wrapper expressions for nested createFileRoute/createLazyFileRoute calls instead of requiring direct initialization. New helper functions replace the prior direct-call detection, and new tests validate wrapped route id injection, lazy swapping, multiple wrappers, and error cases.

      Changes

      Higher-order Route Wrapper Support

      Layer / File(s)Summary
      Route-constructor detection helpers
      packages/router-generator/src/transform/transform.ts
      getRouteConstructorInit accepts wider node types; new findRouteConstructorInits and containsRouteConstructorCall recursively walk call-expression trees, replacing removed isDirectRouteConstructorCall.
      Exported route call discovery
      packages/router-generator/src/transform/transform.ts
      findExportedRouteCalls traverses the full initializer expression to find nested route-constructor calls and distinguishes malformed calls from unsupported/absent ones using the new helpers.
      Wrapper transform tests
      packages/router-generator/tests/transform.test.ts
      New "higher-order route wrappers" test suite covers route id injection, lazy swapping, nested wrappers, and error cases for missing options or multiple constructor calls.

      Estimated code review effort: 3 (Moderate) | ~25 minutes

      Sequence Diagram(s)

      sequenceDiagram
        participant Transform as transform()
        participant Finder as findExportedRouteCalls
        participant Walker as findRouteConstructorInits
        participant Checker as containsRouteConstructorCall
      
        Transform->>Finder: analyze exported binding initializer
        Finder->>Walker: traverse expression tree for route constructor inits
        Walker-->>Finder: matched inits (createFileRoute/createLazyFileRoute)
        alt no inits found
          Finder->>Checker: check for any constructor call in tree
          Checker-->>Finder: hasMalformedRouteCall or unsupported
        end
        Finder-->>Transform: calls list or error state
      🚥 Pre-merge checks | ✅ 5
      ✅ Passed checks (5 passed)
      Check nameStatusExplanation
      Description Check✅ PassedCheck skipped - CodeRabbit’s high-level summary is enabled.
      Title check✅ PassedThe title clearly and concisely describes the main change: supporting higher-order wrappers around createFileRoute.
      Docstring Coverage✅ PassedNo functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
      Linked Issues check✅ PassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes check✅ PassedCheck skipped because no linked issues were found for this pull request.
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create PR with unit tests

      Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

      ❤️ Share
      Comment `@coderabbitai help` to get the list of available commands.

Sign in to join this conversation.