Skip to content

feat: allow filePath to be null on a route added in an editable tree #607

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 1 commit into
base: main
Choose a base branch
from

Conversation

Anoesj
Copy link
Contributor

@Anoesj Anoesj commented Mar 18, 2025

It seems that, when we only allow filePath to be null on a route added in an editable tree, that shouldn't cause any problems.

In Nuxt, we don't use file scanning and add routes by modifying the EditableTreeNode of the root page in the beforeWriteFiles callback. A NuxtPage can theoretically not have a corresponding file, although I'm not sure what the use case here is exactly. So currently, where we run parent.insert(path, page.file), page.file can be undefined and we currently use a @ts-expect-error before that line to suppress TS errors.

If this PR is safe to merge, we can remove that @ts-expect-error.

Copy link

pkg-pr-new bot commented Mar 18, 2025

Open in Stackblitz

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

commit: 2d0d1a2

Copy link

codecov bot commented Mar 18, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 61.72%. Comparing base (cd4a3f8) to head (2d0d1a2).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #607   +/-   ##
=======================================
  Coverage   61.72%   61.72%           
=======================================
  Files          32       32           
  Lines        3135     3135           
  Branches      580      580           
=======================================
  Hits         1935     1935           
  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
Owner

@posva posva left a comment

Choose a reason for hiding this comment

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

I know the comment is a bit weird. I didn't find any practical case where this was useful when inserting an already parsed node. I couldn't find any usage of this in Nuxt code. Where is it?

@Anoesj
Copy link
Contributor Author

Anoesj commented Apr 22, 2025

Hmm, it's been a while since I last looked at this. I think it's these two lines, where page.file can be undefined when the page is added programmatically:

Here you can see that file is optional in NuxtPage: https://github.com/nuxt/nuxt/blob/b0497b5b188fa1223db64982acb20e061846faa4/packages/schema/src/types/hooks.ts#L32

I am not entirely sure, but maybe Nuxt supports virtual pages that don't really correspond to files? If that's true and this PR is to be merged, then we need to adjust the lines mentioned above to fall back to null when page.file is nullish.

@Anoesj
Copy link
Contributor Author

Anoesj commented Apr 22, 2025

Here is an example in a test file, where no file has been specified: https://github.com/nuxt/nuxt/blob/b0497b5b188fa1223db64982acb20e061846faa4/test/fixtures/basic-types/nuxt.config.ts#L36

Not sure if this test case is actually realistic, but Nuxt does seem to support some form of file-less pages.

Copy link
Owner

@posva posva left a comment

Choose a reason for hiding this comment

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

Thanks for this. I will come back to it after merging other things like relative paths insertions. I'm not 100% sure about it yet but I think it makes sense in the context of the editable tree where a node can be created to have children afterwards

const isComponent = true
insertParsedPath(path: string, filePath: string | null = path): TreeNode {
// Allow null filePath to be handled
const isComponent = filePath !== null
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
const isComponent = filePath !== null
const isComponent = filePath != null

// TODO: allow null filePath?
const isComponent = true
insertParsedPath(path: string, filePath: string | null = path): TreeNode {
// Allow null filePath to be handled
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
// Allow null filePath to be handled

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