Skip to content
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

fix type generation for matched params #4472

Merged
merged 2 commits into from Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/serious-ducks-exercise.md
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Generate correct types for routes with paramter matchers
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
20 changes: 3 additions & 17 deletions packages/kit/src/core/sync/write_types.js
@@ -1,3 +1,4 @@
import { parse_route_id } from '../../utils/routing.js';
import { write_if_changed } from './utils.js';

/** @param {string} imports */
Expand All @@ -24,21 +25,6 @@ export function write_types(config, manifest_data) {
/** @type {Map<string, { params: string[], type: 'page' | 'endpoint' | 'both' }>} */
const shadow_types = new Map();

/** @param {string} key */
function extract_params(key) {
/** @type {string[]} */
const params = [];

const pattern = /\[(?:\.{3})?([^\]]+)\]/g;
let match;

while ((match = pattern.exec(key))) {
params.push(match[1]);
}

return params;
}

manifest_data.routes.forEach((route) => {
const file = route.type === 'endpoint' ? route.file : route.shadow;

Expand All @@ -48,7 +34,7 @@ export function write_types(config, manifest_data) {
);
const key = file.slice(0, -ext.length);
shadow_types.set(key, {
params: extract_params(key),
params: parse_route_id(key).names,
type: route.type === 'endpoint' ? 'endpoint' : 'both'
});
}
Expand All @@ -61,7 +47,7 @@ export function write_types(config, manifest_data) {
const key = component.slice(0, -ext.length);

if (!shadow_types.has(key)) {
shadow_types.set(key, { params: extract_params(key), type: 'page' });
shadow_types.set(key, { params: parse_route_id(key).names, type: 'page' });
}
});

Expand Down