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

unify matcher name restrictions and clearer error on bad param/matcher names #5460

Merged
merged 2 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();