Skip to content

Commit 062f38e

Browse files
authored
unify matcher name restrictions and clearer error on bad param/matcher names (#5460)
* unify matcher name restrictions and clearer errors lint tf * changeset
1 parent 75dd398 commit 062f38e

File tree

5 files changed

+52
-6
lines changed

5 files changed

+52
-6
lines changed

.changeset/smooth-hornets-think.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
clearer error on bad matcher names

packages/kit/src/core/sync/create_manifest_data/index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,18 @@ export default function create_manifest_data({
270270
if (!config.kit.moduleExtensions.includes(ext)) continue;
271271
const type = file.slice(0, -ext.length);
272272

273-
if (/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(type)) {
274-
matchers[type] = path.join(params_base, file);
273+
if (/^\w+$/.test(type)) {
274+
const matcher_file = path.join(params_base, file);
275+
276+
// Disallow same matcher with different extensions
277+
if (matchers[type]) {
278+
throw new Error(`Duplicate matchers: ${matcher_file} and ${matchers[type]}`);
279+
} else {
280+
matchers[type] = matcher_file;
281+
}
275282
} else {
276283
throw new Error(
277-
`Matcher names must match /^[a-zA-Z_][a-zA-Z0-9_]*$/ — "${file}" is invalid`
284+
`Matcher names can only have underscores and alphanumeric characters — "${file}" is invalid`
278285
);
279286
}
280287
}

packages/kit/src/core/sync/create_manifest_data/index.spec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,4 +638,28 @@ test('creates param matchers', () => {
638638
});
639639
});
640640

641+
test('errors on param matchers with bad names', () => {
642+
const boogaloo = path.resolve(cwd, 'params', 'boo-galoo.js');
643+
fs.writeFileSync(boogaloo, '');
644+
try {
645+
assert.throws(() => create('samples/basic'), /Matcher names can only have/);
646+
} finally {
647+
fs.unlinkSync(boogaloo);
648+
}
649+
});
650+
651+
test('errors on duplicate matchers', () => {
652+
const ts_foo = path.resolve(cwd, 'params', 'foo.ts');
653+
fs.writeFileSync(ts_foo, '');
654+
try {
655+
assert.throws(() => {
656+
create('samples/basic', {
657+
extensions: ['.js', '.ts']
658+
});
659+
}, /Duplicate matchers/);
660+
} finally {
661+
fs.unlinkSync(ts_foo);
662+
}
663+
});
664+
641665
test.run();

packages/kit/src/utils/routing.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,14 @@ export function parse_route_id(id) {
3636
.split(/\[(.+?)\]/)
3737
.map((content, i) => {
3838
if (i % 2) {
39-
const [, rest, name, type] = /** @type {RegExpMatchArray} */ (
40-
param_pattern.exec(content)
41-
);
39+
const match = param_pattern.exec(content);
40+
if (!match) {
41+
throw new Error(
42+
`Invalid param: ${content}. Params and matcher names can only have underscores and alphanumeric characters.`
43+
);
44+
}
45+
46+
const [, rest, name, type] = match;
4247
names.push(name);
4348
types.push(type);
4449
return rest ? '(.*?)' : '([^/]+?)';

packages/kit/src/utils/routing.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,9 @@ for (const [key, expected] of Object.entries(tests)) {
5555
});
5656
}
5757

58+
test('errors on bad param name', () => {
59+
assert.throws(() => parse_route_id('abc/[b-c]'), /Invalid param: b-c/);
60+
assert.throws(() => parse_route_id('abc/[bc=d-e]'), /Invalid param: bc=d-e/);
61+
});
62+
5863
test.run();

0 commit comments

Comments
 (0)