Skip to content

Commit e68357a

Browse files
Merge pull request #32 from aktasumut34/js-reserved-route-names
Add route prefix to reserved js keyword route names
2 parents c848a90 + 169f8a2 commit e68357a

File tree

5 files changed

+69
-56
lines changed

5 files changed

+69
-56
lines changed

src/GenerateCommand.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,8 @@ private function writeNamedFile(Collection $routes, string $namespace): void
202202
$imports = $routes->map(fn (Route $route) => $route->namedMethod())->implode(', ');
203203

204204
$basename = basename($path, '.ts');
205-
$base = Str::of($basename)->when(
206-
str_contains($basename, '-'),
207-
fn ($s) => $s->camel()
208-
)->toString();
205+
206+
$base = TypeScript::safeMethod($basename, 'Route');
209207

210208
if ($base !== $imports) {
211209
$this->appendContent($path, "const {$base} = { {$imports} }\n");

src/Route.php

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,7 @@ public function originalJsMethod()
5656

5757
public function namedMethod(): string
5858
{
59-
$base = Str::afterLast($this->name(), '.');
60-
61-
return $this->finalJsMethod(
62-
str_contains($base, '-') ?
63-
Str::camel($base) : $base
64-
);
59+
return $this->finalJsMethod(Str::afterLast($this->name(), '.'));
6560
}
6661

6762
public function controller(): string
@@ -165,51 +160,7 @@ public function controllerMethodLineNumber(): int
165160

166161
private function finalJsMethod(string $method): string
167162
{
168-
$reserved = [
169-
'break',
170-
'case',
171-
'catch',
172-
'class',
173-
'const',
174-
'continue',
175-
'debugger',
176-
'default',
177-
'delete',
178-
'do',
179-
'else',
180-
'export',
181-
'extends',
182-
'false',
183-
'finally',
184-
'for',
185-
'function',
186-
'if',
187-
'import',
188-
'in',
189-
'instanceof',
190-
'new',
191-
'null',
192-
'return',
193-
'super',
194-
'switch',
195-
'this',
196-
'throw',
197-
'true',
198-
'try',
199-
'typeof',
200-
'var',
201-
'void',
202-
'while',
203-
'with',
204-
];
205-
206-
$method = in_array($method, $reserved) ? $method.'Method' : $method;
207-
208-
if (is_numeric($method)) {
209-
return 'method'.$method;
210-
}
211-
212-
return $method;
163+
return TypeScript::safeMethod($method, 'Method');
213164
}
214165

215166
private function relativePath(string $path)

src/TypeScript.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,65 @@
66

77
class TypeScript
88
{
9+
public const RESERVED_KEYWORDS = [
10+
'break',
11+
'case',
12+
'catch',
13+
'class',
14+
'const',
15+
'continue',
16+
'debugger',
17+
'default',
18+
'delete',
19+
'do',
20+
'else',
21+
'export',
22+
'extends',
23+
'false',
24+
'finally',
25+
'for',
26+
'function',
27+
'if',
28+
'import',
29+
'in',
30+
'instanceof',
31+
'new',
32+
'null',
33+
'return',
34+
'super',
35+
'switch',
36+
'this',
37+
'throw',
38+
'true',
39+
'try',
40+
'typeof',
41+
'var',
42+
'void',
43+
'while',
44+
'with',
45+
];
46+
47+
public static function safeMethod(string $method, string $suffix): string
48+
{
49+
$method = str($method);
50+
51+
if ($method->contains('-')) {
52+
$method = $method->camel();
53+
}
54+
55+
$suffix = strtolower($suffix);
56+
57+
if (in_array($method, self::RESERVED_KEYWORDS)) {
58+
return $method->append(ucfirst($suffix));
59+
}
60+
61+
if (is_numeric((string) $method)) {
62+
return $method->prepend($suffix);
63+
}
64+
65+
return $method;
66+
}
67+
968
public static function cleanUp(string $view): string
1069
{
1170
$replacements = [

tests/DisallowedMethodNames.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import DisallowedMethodNameController, {
33
deleteMethod,
44
method404,
55
} from "../workbench/resources/js/actions/App/Http/Controllers/DisallowedMethodNameController";
6+
import route404 from "../workbench/resources/js/routes/disallowed/404";
67

78
test("will append `method` to invalid methods", () => {
89
expect(method404.url()).toBe("/disallowed/404");
@@ -12,3 +13,7 @@ test("will append `method` to invalid methods", () => {
1213
);
1314
expect(DisallowedMethodNameController[404].url()).toBe("/disallowed/404");
1415
});
16+
17+
test("will append `method` to invalid methods", () => {
18+
expect(route404.method404.url()).toBe("/disallowed/404");
19+
});

workbench/routes/web.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
Route::get('/two-routes-one-action-2', [TwoRoutesSameActionController::class, 'same']);
6767

6868
Route::get('/disallowed/delete', [DisallowedMethodNameController::class, 'delete']);
69-
Route::get('/disallowed/404', [DisallowedMethodNameController::class, '404']);
69+
Route::get('/disallowed/404', [DisallowedMethodNameController::class, '404'])->name('disallowed.404');
7070

7171
Route::get('/anonymous-middleware', [AnonymousMiddlewareController::class, 'show']);
7272

0 commit comments

Comments
 (0)