-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Feat/resolve route imp #11406
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
Draft
jycouet
wants to merge
13
commits into
sveltejs:main
Choose a base branch
from
jycouet:feat/resolveRoute-imp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Feat/resolve route imp #11406
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8418745
draft: add resolveRoute types
jycouet cc07732
add changeset
jycouet 891e63a
desc
jycouet 5d21b19
fix tsconfig
jycouet 64e96dc
testing
jycouet c8a8db6
learning JSDoc
jycouet 8c6f4b7
learning JSDoc (dts buddy?)
jycouet 87b95ab
resolve comment
jycouet 1a7771b
get generated types working, i think?
Rich-Harris 1392197
oops
Rich-Harris 6bc5ef3
remove some unused stuff
Rich-Harris 5ffc62a
clarify
Rich-Harris 0fdec66
make route_ids.d.ts work as an ambient module by ditching MatcherPara…
Rich-Harris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@sveltejs/kit': patch | ||
--- | ||
|
||
feat: add types to `resolveRoute` (id & params) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// These types live here because I can't figure out how to express them with JSDoc | ||
|
||
import { RouteIds } from '$types'; | ||
|
||
// Type utility to extract keys that correspond to routes | ||
type RouteWithParams = { | ||
[K in keyof RouteIds]: RouteIds[K] extends never ? never : K; | ||
}[keyof RouteIds]; | ||
|
||
type RouteWithoutParams = { | ||
[K in keyof RouteIds]: RouteIds[K] extends never ? K : never; | ||
}[keyof RouteIds]; | ||
|
||
/** | ||
* Populate a route ID with params to resolve a pathname. | ||
* @example | ||
* ```js | ||
* resolveRoute( | ||
* `/blog/[slug]/[...somethingElse]`, | ||
* { | ||
* slug: 'hello-world', | ||
* somethingElse: 'something/else' | ||
* } | ||
* ); // `/blog/hello-world/something/else` | ||
* ``` | ||
*/ | ||
export function resolveRoute<K extends RouteWithParams>(id: K, params: RouteIds[K]): string; | ||
export function resolveRoute<K extends RouteWithoutParams>(id: K): string; | ||
|
||
export { base, assets } from '__sveltekit/paths'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// this file is a placeholder `$types` module that exists purely for typechecking the codebase. | ||
// in actual use, the `$types` module will be declared in an ambient module generated by | ||
// SvelteKit into `.svelte-kit/types` | ||
export interface RouteIds {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
/** | ||
* @param {string} param | ||
* @returns {param is number} | ||
*/ | ||
export function match(param) { | ||
return !isNaN(parseInt(param)); | ||
} |
27 changes: 23 additions & 4 deletions
27
packages/kit/test/apps/basics/src/routes/routing/matched/+layout.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,25 @@ | ||
<a href="/routing/matched/a">/routing/matched/a</a> | ||
<a href="/routing/matched/B">/routing/matched/B</a> | ||
<a href="/routing/matched/1">/routing/matched/1</a> | ||
<a href="/routing/matched/everything-else">/routing/matched/everything-else</a> | ||
<script lang="ts"> | ||
import { resolveRoute } from '$app/paths'; | ||
</script> | ||
|
||
<!-- <a href="/routing/matched"> 🤞 matched </a> --> | ||
<!-- <a href={resolveRoute('/rooting/matched')}> ❌ matched </a> --> | ||
<a href={resolveRoute('/routing/matched')}> ✅ matched </a> | ||
|
||
<!-- <a href="/routing/matched/a"> 🤞 lowercase </a> --> | ||
<!-- <a href={resolveRoute('/routing/matched/[letter=lowercase]', { letter: true })}> ❌ lowercase </a> --> | ||
<a href={resolveRoute('/routing/matched/[letter=lowercase]', { letter: 'a' })}> ✅ lowercase </a> | ||
|
||
<!-- <a href="/routing/matched/B">/routing/matched/B</a> --> | ||
<a href={resolveRoute('/routing/matched/[letter=uppercase]', { letter: 'B' })}>uppercase</a> | ||
<!-- <a href="/routing/matched/1">/routing/matched/1</a> --> | ||
<a href={resolveRoute('/routing/matched/[number=numeric]', { number: '1' })}> numeric </a> | ||
<!-- <a href="/routing/matched/everything-else">/routing/matched/everything-else</a> --> | ||
<a href={resolveRoute('/routing/matched/[fallback]', { fallback: 'everything-else' })}> | ||
fallback | ||
</a> | ||
<a href={resolveRoute('/routing/matched/[[optional]]/withOption', { optional: 'sziaaa' })}> | ||
optional | ||
</a> | ||
|
||
<slot /> |
5 changes: 5 additions & 0 deletions
5
...ages/kit/test/apps/basics/src/routes/routing/matched/[[optional]]/withOption/+page.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<script> | ||
import { page } from '$app/stores'; | ||
</script> | ||
|
||
<h1>with option: {$page.params.optional}</h1> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lint is not passing because I wrote the type here directly. It seems that we have to do it in
JSDoc
, but I don't manage to do it.I would need some guidance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I too am somewhat flummoxed by JSDoc in this case. For now I've put this stuff in a src
.d.ts
file, which sort of works, though there are some other problems that need solving separately.I took the liberty of moving the
RouteId
type to$types
rather than$app/paths
— it's arguably more consistent with the various./$types
files, and it'll be convenient to have a single place to put all the generated types.Right now it doesn't seem like that ambient declaration is getting picked up — working on it...