From c90d8ee3cdfb3a0eca65272310254b7b6903cf89 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 15 Jul 2018 23:11:08 -0400 Subject: [PATCH] more tests --- src/core/create_routes.ts | 22 +++++++++++++------ test/unit/create_routes/index.js | 13 ++++++++++- .../samples/invalid-params/[foo][bar].js | 0 .../invalid-qualifier/[foo([a-z][0-9]?)].js | 0 4 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 test/unit/create_routes/samples/invalid-params/[foo][bar].js create mode 100644 test/unit/create_routes/samples/invalid-qualifier/[foo([a-z][0-9]?)].js diff --git a/src/core/create_routes.ts b/src/core/create_routes.ts index a4f75bc..0c4f2b2 100644 --- a/src/core/create_routes.ts +++ b/src/core/create_routes.ts @@ -51,6 +51,16 @@ export default function create_routes(cwd = locations.routes()) { const is_index = is_dir ? false : basename.startsWith('index.'); const is_page = path.extname(basename) === '.html'; + parts.forEach(part => { + if (/\]\[/.test(part.content)) { + throw new Error(`Invalid route ${file} — parameters must be separated`); + } + + if (part.qualifier && /[\(\)\?\:]/.test(part.qualifier.slice(1, -1))) { + throw new Error(`Invalid route ${file} — cannot use (, ), ? or : in route qualifiers`); + } + }); + return { basename, parts, @@ -260,17 +270,15 @@ function comparator( } } -const qualifier_pattern = /\(.+\)$/; - function get_parts(part: string): Part[] { return part.split(/\[(.+)\]/) - .map((content, i) => { - if (!content) return null; + .map((str, i) => { + if (!str) return null; const dynamic = i % 2 === 1; - const qualifier = dynamic && qualifier_pattern.test(content) - ? qualifier_pattern.exec(content)[0] - : null; + const [, content, qualifier] = dynamic + ? /([^(]+)(\(.+\))?$/.exec(str) + : [, str, null]; return { content, diff --git a/test/unit/create_routes/index.js b/test/unit/create_routes/index.js index ab695fb..4b37bd3 100644 --- a/test/unit/create_routes/index.js +++ b/test/unit/create_routes/index.js @@ -135,7 +135,6 @@ describe.only('create_routes', () => { it('fails on clashes', () => { assert.throws(() => { const { pages } = create_routes(path.join(__dirname, 'samples/clash-pages')); - console.log(pages); }, /The \[bar\]\/index\.html and \[foo\]\.html pages clash/); assert.throws(() => { @@ -143,4 +142,16 @@ describe.only('create_routes', () => { console.log(server_routes); }, /The \[bar\]\/index\.js and \[foo\]\.js routes clash/); }); + + it('fails if dynamic params are not separated', () => { + assert.throws(() => { + create_routes(path.join(__dirname, 'samples/invalid-params')); + }, /Invalid route \[foo\]\[bar\]\.js — parameters must be separated/); + }); + + it('errors when trying to use reserved characters in route regexp', () => { + assert.throws(() => { + create_routes(path.join(__dirname, 'samples/invalid-qualifier')); + }, /Invalid route \[foo\(\[a-z\]\[0-9\]\?\)\].js — cannot use \(, \), \? or \: in route qualifiers/); + }); }); \ No newline at end of file diff --git a/test/unit/create_routes/samples/invalid-params/[foo][bar].js b/test/unit/create_routes/samples/invalid-params/[foo][bar].js new file mode 100644 index 0000000..e69de29 diff --git a/test/unit/create_routes/samples/invalid-qualifier/[foo([a-z][0-9]?)].js b/test/unit/create_routes/samples/invalid-qualifier/[foo([a-z][0-9]?)].js new file mode 100644 index 0000000..e69de29