allow reserved words as route names - fixes #315

This commit is contained in:
Rich Harris
2018-09-02 21:46:25 -04:00
parent 4a92fbbbfa
commit 2c507b5a2e
5 changed files with 67 additions and 3 deletions

View File

@@ -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) {

View File

@@ -22,4 +22,55 @@ export function fudge_mtime(file: string) {
new Date(atime.getTime() - 999999),
new Date(mtime.getTime() - 999999)
);
}
}
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',
]);

View File

@@ -0,0 +1 @@
<h1>reserved words are okay as routes</h1>

View File

@@ -14,6 +14,7 @@
<a href='blog/throw-an-error'>error link</a>
<a href='credentials?creds=include'>credentials</a>
<a rel=prefetch class='{page === "blog" ? "selected" : ""}' href='blog'>blog</a>
<a href="const">const</a>
<div class='hydrate-test'></div>

View File

@@ -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', () => {