Skip to content

Commit

Permalink
fix type generation for matched params (#4472)
Browse files Browse the repository at this point in the history
* fix type generation for matched params

* Update .changeset/serious-ducks-exercise.md
  • Loading branch information
Rich-Harris committed Apr 1, 2022
1 parent e962c82 commit b8bd5c6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/serious-ducks-exercise.md
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Generate correct types for routes with parameter matchers
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

0 comments on commit b8bd5c6

Please sign in to comment.