Skip to content

Commit

Permalink
unify matcher name restrictions and clearer error on bad param/matche…
Browse files Browse the repository at this point in the history
…r names (#5460)

* unify matcher name restrictions and clearer errors

lint

tf

* changeset
  • Loading branch information
gtm-nayan authored Jul 11, 2022
1 parent 75dd398 commit 062f38e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/smooth-hornets-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

clearer error on bad matcher names
13 changes: 10 additions & 3 deletions packages/kit/src/core/sync/create_manifest_data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,18 @@ export default function create_manifest_data({
if (!config.kit.moduleExtensions.includes(ext)) continue;
const type = file.slice(0, -ext.length);

if (/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(type)) {
matchers[type] = path.join(params_base, file);
if (/^\w+$/.test(type)) {
const matcher_file = path.join(params_base, file);

// Disallow same matcher with different extensions
if (matchers[type]) {
throw new Error(`Duplicate matchers: ${matcher_file} and ${matchers[type]}`);
} else {
matchers[type] = matcher_file;
}
} else {
throw new Error(
`Matcher names must match /^[a-zA-Z_][a-zA-Z0-9_]*$/ — "${file}" is invalid`
`Matcher names can only have underscores and alphanumeric characters — "${file}" is invalid`
);
}
}
Expand Down
24 changes: 24 additions & 0 deletions packages/kit/src/core/sync/create_manifest_data/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -638,4 +638,28 @@ test('creates param matchers', () => {
});
});

test('errors on param matchers with bad names', () => {
const boogaloo = path.resolve(cwd, 'params', 'boo-galoo.js');
fs.writeFileSync(boogaloo, '');
try {
assert.throws(() => create('samples/basic'), /Matcher names can only have/);
} finally {
fs.unlinkSync(boogaloo);
}
});

test('errors on duplicate matchers', () => {
const ts_foo = path.resolve(cwd, 'params', 'foo.ts');
fs.writeFileSync(ts_foo, '');
try {
assert.throws(() => {
create('samples/basic', {
extensions: ['.js', '.ts']
});
}, /Duplicate matchers/);
} finally {
fs.unlinkSync(ts_foo);
}
});

test.run();
11 changes: 8 additions & 3 deletions packages/kit/src/utils/routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@ export function parse_route_id(id) {
.split(/\[(.+?)\]/)
.map((content, i) => {
if (i % 2) {
const [, rest, name, type] = /** @type {RegExpMatchArray} */ (
param_pattern.exec(content)
);
const match = param_pattern.exec(content);
if (!match) {
throw new Error(
`Invalid param: ${content}. Params and matcher names can only have underscores and alphanumeric characters.`
);
}
const [, rest, name, type] = match;
names.push(name);
types.push(type);
return rest ? '(.*?)' : '([^/]+?)';
Expand Down
5 changes: 5 additions & 0 deletions packages/kit/src/utils/routing.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,9 @@ for (const [key, expected] of Object.entries(tests)) {
});
}

test('errors on bad param name', () => {
assert.throws(() => parse_route_id('abc/[b-c]'), /Invalid param: b-c/);
assert.throws(() => parse_route_id('abc/[bc=d-e]'), /Invalid param: bc=d-e/);
});

test.run();

0 comments on commit 062f38e

Please sign in to comment.