Skip to content

feat: add children route names generic to generated RouteNamedMap and adjust docs and tests #602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

Anoesj
Copy link
Contributor

@Anoesj Anoesj commented Mar 13, 2025

Description

Note

This PR is intended to be paired with the following vue-router PR: vuejs/router#2475.
This PR itself contains a temporary pnpm patch file that applies the changes in above-mentioned PR to work on this feature and make sure you can run tests and generate docs properly. Before merging, this patch needs to be removed and vue-router should be updated if the vue-router PR is merged.

This extends generated RouteRecordInfo objects in RouteNamedMap to make sure every route defines the route names of their children routes. Now useRoute('/parent') can return the typings of both the parent route and its children routes, as can be seen in the example in the adjusted docs:
image

Summary by CodeRabbit

  • Documentation

    • Enhanced TypeScript documentation to clarify route metadata typing and dynamic child route support.
    • Improved example code and comments for typed routes, highlighting autocompletion and type safety.
    • Added comments indicating auto-generation of route name maps.
  • New Features

    • Expanded type definitions for routes to include metadata (RouteMeta) and explicit child route names for better type safety and expressiveness, especially for dynamic and nested routes.
    • Added support for nested child route names and metadata in route records, including dynamic runtime routes.
    • Introduced a new npm script to preview documentation.
  • Chores

    • Updated vue-router dependency versions to ^4.5.1.
    • Configured pnpm to restrict builds to specified dependencies.
  • Tests

    • Added and updated tests to verify correct typing of child routes and metadata in route records.
  • Refactor

    • Improved route record generation logic to dynamically include metadata and child route names based on route structure.
    • Added a method to recursively retrieve all descendant routes sorted by path for internal use.

Copy link

pkg-pr-new bot commented Mar 13, 2025

Open in StackBlitz

npm i https://pkg.pr.new/unplugin-vue-router@602

commit: 13706a2

Copy link

codecov bot commented Mar 13, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 61.80%. Comparing base (70e454d) to head (c32ca68).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #602      +/-   ##
==========================================
+ Coverage   61.72%   61.80%   +0.08%     
==========================================
  Files          32       32              
  Lines        3135     3142       +7     
  Branches      580      583       +3     
==========================================
+ Hits         1935     1942       +7     
  Misses       1194     1194              
  Partials        6        6              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Contributor Author

@Anoesj Anoesj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some extra info 😄

@posva posva moved this from 🆕 New to 🏗 In progress in unplugin-vue-router Apr 22, 2025
Copy link

coderabbitai bot commented Apr 24, 2025

Walkthrough

The changes update route type declarations throughout the codebase to include two new generic parameters: RouteMeta and a child route name union or never in each RouteRecordInfo entry within RouteNamedMap. Imports are adjusted to include RouteMeta. Route definitions, both static and dynamic, now explicitly specify these parameters for improved type safety and metadata support. The code generator and its tests are modified to emit and verify these extended type signatures. Documentation and example code are updated to reflect and explain the new type structure. A new npm script and dependency changes are also introduced.

Changes

Files/Paths Change Summary
docs/.vitepress/twoslash-files.ts, docs/.vitepress/twoslash/code/typed-router.ts Updated RouteNamedMap interface: all RouteRecordInfo entries now include a fifth generic parameter for child route names or never. Added documentation comment and adjusted imports accordingly.
docs/guide/typescript.md, docs/introduction.md Documentation updated: examples and explanations now show/import RouteMeta, use full generic parameters in route declarations, clarify usage of new type structure, and add new dynamic child route.
playground/src/router.ts, playground/typed-router.d.ts Dynamic and static route type augmentations updated to include RouteMeta and child route names; new dynamic child route added with explicit metadata typing.
src/codegen/generateDTS.ts Added import of RouteMeta to generated module declaration.
src/codegen/generateRouteMap.ts generateRouteRecordInfo updated to emit an additional generic parameter for child route names union when children with components exist; changed condition to explicit size check.
src/codegen/generateRouteMap.spec.ts Added tests verifying child route name unions in RouteNamedMap; reformatted snapshots for consistency.
src/core/tree.ts Added getSortedChildrenDeep() method to TreeNode class to recursively collect and sort all descendant nodes by path.
package.json Added docs:preview script, updated vue-router version to ^4.5.1 in peer and dev dependencies, added pnpm.onlyBuiltDependencies configuration.
playground/package.json Updated vue-router dependency version to ^4.5.1.

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3696626 and 13706a2.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (12)
  • docs/.vitepress/twoslash-files.ts (1 hunks)
  • docs/.vitepress/twoslash/code/typed-router.ts (1 hunks)
  • docs/guide/typescript.md (2 hunks)
  • docs/introduction.md (1 hunks)
  • package.json (3 hunks)
  • playground/package.json (1 hunks)
  • playground/src/router.ts (2 hunks)
  • playground/typed-router.d.ts (2 hunks)
  • src/codegen/generateDTS.ts (1 hunks)
  • src/codegen/generateRouteMap.spec.ts (4 hunks)
  • src/codegen/generateRouteMap.ts (2 hunks)
  • src/core/tree.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (11)
  • src/codegen/generateDTS.ts
  • playground/package.json
  • docs/.vitepress/twoslash/code/typed-router.ts
  • playground/typed-router.d.ts
  • package.json
  • docs/.vitepress/twoslash-files.ts
  • src/codegen/generateRouteMap.ts
  • src/codegen/generateRouteMap.spec.ts
  • docs/guide/typescript.md
  • docs/introduction.md
  • src/core/tree.ts
🧰 Additional context used
🧬 Code Graph Analysis (1)
playground/src/router.ts (2)
src/codegen/generateRouteParams.ts (1)
  • ParamValue (55-57)
src/data-loaders/meta-extensions.ts (1)
  • RouteMeta (58-84)
🔇 Additional comments (3)
playground/src/router.ts (3)

3-3: Added RouteMeta import to support route metadata typing.

The import statement now includes the RouteMeta type which is required for the updated route type definitions below.


34-37: Updated RouteRecordInfo generic parameters to include RouteMeta and child route.

The route type definition for 'custom-dynamic-name' now properly includes:

  • Typed query parameters
  • Route metadata typing
  • Reference to its child route 'custom-dynamic-child-name'

This change aligns with the PR objective of extending RouteRecordInfo to include children routes.


38-45: Added child route type definition with proper parent-child relationship.

The new 'custom-dynamic-child-name' route type is correctly defined with:

  • Path that extends the parent route path
  • Consistent parameter typing with the parent
  • Support for route metadata
  • 'never' as the last parameter indicating no further children

This implementation properly demonstrates the parent-child relationship in route types.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@posva posva force-pushed the feat/add-child-route-names-in-generated-dts branch from 3696626 to 13706a2 Compare April 25, 2025 13:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🏗 In progress
Development

Successfully merging this pull request may close these issues.

2 participants