diff --git a/src/core/create_manifest_data.ts b/src/core/create_manifest_data.ts index 367317b..fd29a9b 100644 --- a/src/core/create_manifest_data.ts +++ b/src/core/create_manifest_data.ts @@ -2,7 +2,7 @@ import * as fs from 'fs'; import * as path from 'path'; import { locations } from '../config'; import { Page, PageComponent, ServerRoute, ManifestData } from '../interfaces'; -import { posixify } from './utils'; +import { posixify, reserved_words } from './utils'; export default function create_manifest_data(cwd = locations.routes()): ManifestData { const components: PageComponent[] = []; @@ -269,7 +269,7 @@ function get_parts(part: string): Part[] { } function get_slug(file: string) { - return file + let name = file .replace(/[\\\/]index/, '') .replace(/_default([\/\\index])?\.html$/, 'index') .replace(/[\/\\]/g, '_') @@ -278,6 +278,9 @@ function get_slug(file: string) { .replace(/[^a-zA-Z0-9_$]/g, c => { return c === '.' ? '_' : `$${c.charCodeAt(0)}` }); + + if (reserved_words.has(name)) name += '_'; + return name; } function get_pattern(segments: Part[][], add_trailing_slash: boolean) { diff --git a/src/core/utils.ts b/src/core/utils.ts index fb963ae..2668e68 100644 --- a/src/core/utils.ts +++ b/src/core/utils.ts @@ -22,4 +22,55 @@ export function fudge_mtime(file: string) { new Date(atime.getTime() - 999999), new Date(mtime.getTime() - 999999) ); -} \ No newline at end of file +} + +export const reserved_words = new Set([ + 'arguments', + 'await', + 'break', + 'case', + 'catch', + 'class', + 'const', + 'continue', + 'debugger', + 'default', + 'delete', + 'do', + 'else', + 'enum', + 'eval', + 'export', + 'extends', + 'false', + 'finally', + 'for', + 'function', + 'if', + 'implements', + 'import', + 'in', + 'instanceof', + 'interface', + 'let', + 'new', + 'null', + 'package', + 'private', + 'protected', + 'public', + 'return', + 'static', + 'super', + 'switch', + 'this', + 'throw', + 'true', + 'try', + 'typeof', + 'var', + 'void', + 'while', + 'with', + 'yield', +]); \ No newline at end of file diff --git a/test/app/routes/const.html b/test/app/routes/const.html new file mode 100644 index 0000000..6975aae --- /dev/null +++ b/test/app/routes/const.html @@ -0,0 +1 @@ +

reserved words are okay as routes

\ No newline at end of file diff --git a/test/app/routes/index.html b/test/app/routes/index.html index e5eecde..4678fbd 100644 --- a/test/app/routes/index.html +++ b/test/app/routes/index.html @@ -14,6 +14,7 @@ error link credentials blog +const
diff --git a/test/common/test.js b/test/common/test.js index 2ad9730..abf3b27 100644 --- a/test/common/test.js +++ b/test/common/test.js @@ -743,6 +743,14 @@ function run({ mode, basepath = '' }) { assert.equal(title, 'root preload function ran: true'); }); }); + + it('allows reserved words as route names', () => { + return nightmare.goto(`${base}/const`).init() + .then(() => nightmare.page.title()) + .then(title => { + assert.equal(title, 'reserved words are okay as routes'); + }); + }); }); describe('headers', () => {