From 8d491e62772b43811b36de72aec2daf42425828d Mon Sep 17 00:00:00 2001 From: Nayan Gautam Date: Sun, 10 Jul 2022 07:55:28 +0000 Subject: [PATCH 1/2] unify matcher name restrictions and clearer errors lint tf --- .../core/sync/create_manifest_data/index.js | 13 +++++++--- .../sync/create_manifest_data/index.spec.js | 24 +++++++++++++++++++ packages/kit/src/utils/routing.js | 11 ++++++--- packages/kit/src/utils/routing.spec.js | 5 ++++ 4 files changed, 47 insertions(+), 6 deletions(-) diff --git a/packages/kit/src/core/sync/create_manifest_data/index.js b/packages/kit/src/core/sync/create_manifest_data/index.js index 9081e651da24..01f5a0d567fc 100644 --- a/packages/kit/src/core/sync/create_manifest_data/index.js +++ b/packages/kit/src/core/sync/create_manifest_data/index.js @@ -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` ); } } diff --git a/packages/kit/src/core/sync/create_manifest_data/index.spec.js b/packages/kit/src/core/sync/create_manifest_data/index.spec.js index d7b06bf71fe9..03e9ed21b118 100644 --- a/packages/kit/src/core/sync/create_manifest_data/index.spec.js +++ b/packages/kit/src/core/sync/create_manifest_data/index.spec.js @@ -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(); diff --git a/packages/kit/src/utils/routing.js b/packages/kit/src/utils/routing.js index 38395f7a7a27..fb6d219ad4fb 100644 --- a/packages/kit/src/utils/routing.js +++ b/packages/kit/src/utils/routing.js @@ -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 ? '(.*?)' : '([^/]+?)'; diff --git a/packages/kit/src/utils/routing.spec.js b/packages/kit/src/utils/routing.spec.js index d08bc37dd53f..dd14241c0e23 100644 --- a/packages/kit/src/utils/routing.spec.js +++ b/packages/kit/src/utils/routing.spec.js @@ -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(); From ff965e1caa339d7cfd0d833b38bb38450e0a6d15 Mon Sep 17 00:00:00 2001 From: Nayan Gautam Date: Mon, 11 Jul 2022 13:55:23 +0000 Subject: [PATCH 2/2] changeset --- .changeset/smooth-hornets-think.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/smooth-hornets-think.md diff --git a/.changeset/smooth-hornets-think.md b/.changeset/smooth-hornets-think.md new file mode 100644 index 000000000000..0d26e80f1589 --- /dev/null +++ b/.changeset/smooth-hornets-think.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +clearer error on bad matcher names