mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-20 14:25:07 +00:00
Merge pull request #412 from sveltejs/gh-315
allow reserved words as route names
This commit is contained in:
@@ -2,7 +2,7 @@ import * as fs from 'fs';
|
|||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import { locations } from '../config';
|
import { locations } from '../config';
|
||||||
import { Page, PageComponent, ServerRoute, ManifestData } from '../interfaces';
|
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 {
|
export default function create_manifest_data(cwd = locations.routes()): ManifestData {
|
||||||
const components: PageComponent[] = [];
|
const components: PageComponent[] = [];
|
||||||
@@ -269,7 +269,7 @@ function get_parts(part: string): Part[] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function get_slug(file: string) {
|
function get_slug(file: string) {
|
||||||
return file
|
let name = file
|
||||||
.replace(/[\\\/]index/, '')
|
.replace(/[\\\/]index/, '')
|
||||||
.replace(/_default([\/\\index])?\.html$/, 'index')
|
.replace(/_default([\/\\index])?\.html$/, 'index')
|
||||||
.replace(/[\/\\]/g, '_')
|
.replace(/[\/\\]/g, '_')
|
||||||
@@ -278,6 +278,9 @@ function get_slug(file: string) {
|
|||||||
.replace(/[^a-zA-Z0-9_$]/g, c => {
|
.replace(/[^a-zA-Z0-9_$]/g, c => {
|
||||||
return c === '.' ? '_' : `$${c.charCodeAt(0)}`
|
return c === '.' ? '_' : `$${c.charCodeAt(0)}`
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (reserved_words.has(name)) name += '_';
|
||||||
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_pattern(segments: Part[][], add_trailing_slash: boolean) {
|
function get_pattern(segments: Part[][], add_trailing_slash: boolean) {
|
||||||
|
|||||||
@@ -23,3 +23,54 @@ export function fudge_mtime(file: string) {
|
|||||||
new Date(mtime.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',
|
||||||
|
]);
|
||||||
1
test/app/routes/const.html
Normal file
1
test/app/routes/const.html
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<h1>reserved words are okay as routes</h1>
|
||||||
@@ -14,6 +14,7 @@
|
|||||||
<a href='blog/throw-an-error'>error link</a>
|
<a href='blog/throw-an-error'>error link</a>
|
||||||
<a href='credentials?creds=include'>credentials</a>
|
<a href='credentials?creds=include'>credentials</a>
|
||||||
<a rel=prefetch class='{page === "blog" ? "selected" : ""}' href='blog'>blog</a>
|
<a rel=prefetch class='{page === "blog" ? "selected" : ""}' href='blog'>blog</a>
|
||||||
|
<a href="const">const</a>
|
||||||
|
|
||||||
<div class='hydrate-test'></div>
|
<div class='hydrate-test'></div>
|
||||||
|
|
||||||
|
|||||||
@@ -743,6 +743,14 @@ function run({ mode, basepath = '' }) {
|
|||||||
assert.equal(title, 'root preload function ran: true');
|
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', () => {
|
describe('headers', () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user