From b9a0d891e165f040726c2a7b90800f1e12338394 Mon Sep 17 00:00:00 2001 From: Istiak Tridip <13367189+istiak-tridip@users.noreply.github.com> Date: Thu, 22 May 2025 17:32:46 +0600 Subject: [PATCH 1/7] fix: invalid JS identifier route names --- src/TypeScript.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TypeScript.php b/src/TypeScript.php index b44538f..f36a8b5 100644 --- a/src/TypeScript.php +++ b/src/TypeScript.php @@ -58,7 +58,7 @@ public static function safeMethod(string $method, string $suffix): string return $method->append(ucfirst($suffix)); } - if (is_numeric((string) $method)) { + if ($method->match('/^[a-zA-Z_$]/')->isEmpty()) { return $method->prepend($suffix); } From 37bd58b94771cc8810b83799078758e2cb3a9ba6 Mon Sep 17 00:00:00 2001 From: Istiak Tridip <13367189+istiak-tridip@users.noreply.github.com> Date: Thu, 22 May 2025 17:43:57 +0600 Subject: [PATCH 2/7] fix: updated reserved keywords list --- src/TypeScript.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/TypeScript.php b/src/TypeScript.php index f36a8b5..8ec934a 100644 --- a/src/TypeScript.php +++ b/src/TypeScript.php @@ -42,6 +42,17 @@ class TypeScript 'void', 'while', 'with', + 'public', + 'private', + 'protected', + 'static', + 'package', + 'let', + 'enum', + 'await', + 'implements', + 'interface', + 'yield', ]; public static function safeMethod(string $method, string $suffix): string From 4044f033e211d56b87028cce9e43202d77ecd3be Mon Sep 17 00:00:00 2001 From: Istiak Tridip <13367189+istiak-tridip@users.noreply.github.com> Date: Thu, 22 May 2025 18:06:04 +0600 Subject: [PATCH 3/7] refactor: use faster hashing algorithm --- src/GenerateCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GenerateCommand.php b/src/GenerateCommand.php index 40f3d1f..3605bac 100644 --- a/src/GenerateCommand.php +++ b/src/GenerateCommand.php @@ -183,7 +183,7 @@ private function writeMultiRouteControllerMethodExport(Collection $routes, strin 'withForm' => $this->option('with-form') ?? false, 'routes' => $routes->map(fn ($r) => [ 'method' => $r->jsMethod(), - 'tempMethod' => $r->jsMethod().md5($r->uri()), + 'tempMethod' => $r->jsMethod().hash('xxh3', $r->uri()), 'parameters' => $r->parameters(), 'verbs' => $r->verbs(), 'uri' => $r->uri(), From 4c6da7c5198350572654a0f52068379bd1a329a3 Mon Sep 17 00:00:00 2001 From: Istiak Tridip <13367189+istiak-tridip@users.noreply.github.com> Date: Thu, 22 May 2025 20:32:35 +0600 Subject: [PATCH 4/7] fix: simplify `export` conditions --- resources/method.blade.ts | 2 +- resources/multi-method.blade.ts | 4 ++-- src/GenerateCommand.php | 9 ++++++--- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/resources/method.blade.ts b/resources/method.blade.ts index 37218c6..127ab10 100644 --- a/resources/method.blade.ts +++ b/resources/method.blade.ts @@ -1,5 +1,5 @@ @include('wayfinder::docblock') -{!! when(($export ?? true) && !$isInvokable, 'export ') !!}const {!! $method !!} = (@include('wayfinder::function-arguments')): RouteDefinition<@js($verbs->first()->actual)> => ({ +{!! when($shouldExport, 'export ') !!}const {!! $method !!} = (@include('wayfinder::function-arguments')): RouteDefinition<@js($verbs->first()->actual)> => ({ url: {!! $method !!}.url({!! when($parameters->isNotEmpty(), 'args, ') !!}options), method: @js($verbs->first()->actual), }) diff --git a/resources/multi-method.blade.ts b/resources/multi-method.blade.ts index 5445290..d212d56 100644 --- a/resources/multi-method.blade.ts +++ b/resources/multi-method.blade.ts @@ -3,11 +3,11 @@ ...$route, 'method' => $route['tempMethod'], 'docblock_method' => $route['method'], - 'export' => false, + 'shouldExport' => false, ]) @endforeach -{!! when(!$isInvokable, 'export ') !!}const {!! $method !!} = { +{!! when($shouldExport, 'export ') !!}const {!! $method !!} = { @foreach ($routes as $route) {!! $route['uri'] !!}: {!! $route['tempMethod'] !!}, @endforeach diff --git a/src/GenerateCommand.php b/src/GenerateCommand.php index 3605bac..73fdb61 100644 --- a/src/GenerateCommand.php +++ b/src/GenerateCommand.php @@ -179,7 +179,8 @@ private function writeMultiRouteControllerMethodExport(Collection $routes, strin 'path' => $routes->first()->controllerPath(), 'line' => $routes->first()->controllerMethodLineNumber(), 'controller' => $routes->first()->controller(), - 'isInvokable' => $routes->first()->hasInvokableController(), + 'isInvokable' => $isInvokable = $routes->first()->hasInvokableController(), + 'shouldExport' => ! $isInvokable, 'withForm' => $this->option('with-form') ?? false, 'routes' => $routes->map(fn ($r) => [ 'method' => $r->jsMethod(), @@ -197,7 +198,8 @@ private function writeControllerMethodExport(Route $route, string $path): void 'controller' => $route->controller(), 'method' => $route->jsMethod(), 'original_method' => $route->originalJsMethod(), - 'isInvokable' => $route->hasInvokableController(), + 'isInvokable' => $isInvokable = $route->hasInvokableController(), + 'shouldExport' => $isInvokable, 'path' => $route->controllerPath(), 'line' => $route->controllerMethodLineNumber(), 'parameters' => $route->parameters(), @@ -248,7 +250,8 @@ private function writeNamedMethodExport(Route $route, string $path): void 'controller' => $route->controller(), 'method' => $route->namedMethod(), 'original_method' => $route->originalJsMethod(), - 'isInvokable' => false, + 'isInvokable' => $isInvokable = $route->hasInvokableController(), + 'shouldExport' => $isInvokable, 'path' => $route->controllerPath(), 'line' => $route->controllerMethodLineNumber(), 'parameters' => $route->parameters(), From 09cf41d23d1c8ebe6de827bdecb8f587e56cbde3 Mon Sep 17 00:00:00 2001 From: Istiak Tridip <13367189+istiak-tridip@users.noreply.github.com> Date: Thu, 22 May 2025 22:20:04 +0600 Subject: [PATCH 5/7] fix: invalid JS identifier inside the command --- src/GenerateCommand.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/GenerateCommand.php b/src/GenerateCommand.php index 73fdb61..43bcae3 100644 --- a/src/GenerateCommand.php +++ b/src/GenerateCommand.php @@ -269,14 +269,12 @@ private function writeBarrelFiles(array|Collection $children, string $parent): v return; } - $normalizeToCamelCase = fn ($value) => str_contains($value, '-') ? Str::camel($value) : $value; - $indexPath = join_paths($this->base(), $parent, 'index.ts'); $childKeys = $children->keys()->mapWithKeys(fn ($child) => [ $child => [ - 'safe' => TypeScript::safeMethod($normalizeToCamelCase($child), 'Method'), - 'normalized' => $normalizeToCamelCase($child), + 'safe' => TypeScript::safeMethod($child, 'Method'), + 'normalized' => (string) str($child)->whenContains('-', fn ($s) => $s->camel()), ], ]); @@ -293,7 +291,7 @@ private function writeBarrelFiles(array|Collection $children, string $parent): v $keys = $childKeys->map(fn ($alias, $key) => str_repeat(' ', 4).implode(': ', array_unique([$alias['normalized'], $alias['safe']])))->implode(', '.PHP_EOL); - $varExport = $normalizeToCamelCase(Str::afterLast($parent, DIRECTORY_SEPARATOR)); + $varExport = TypeScript::safeMethod(Str::afterLast($parent, DIRECTORY_SEPARATOR), 'Method'); $this->appendContent($indexPath, << Date: Fri, 23 May 2025 00:45:16 +0600 Subject: [PATCH 6/7] fix: `export` conditions --- src/GenerateCommand.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/GenerateCommand.php b/src/GenerateCommand.php index 43bcae3..3215afa 100644 --- a/src/GenerateCommand.php +++ b/src/GenerateCommand.php @@ -199,7 +199,7 @@ private function writeControllerMethodExport(Route $route, string $path): void 'method' => $route->jsMethod(), 'original_method' => $route->originalJsMethod(), 'isInvokable' => $isInvokable = $route->hasInvokableController(), - 'shouldExport' => $isInvokable, + 'shouldExport' => ! $isInvokable, 'path' => $route->controllerPath(), 'line' => $route->controllerMethodLineNumber(), 'parameters' => $route->parameters(), @@ -247,11 +247,11 @@ private function appendCommonImports(Collection $routes, string $path, string $n private function writeNamedMethodExport(Route $route, string $path): void { $this->appendContent($path, $this->view->make('wayfinder::method', [ - 'controller' => $route->controller(), + 'controller' => $controller = $route->controller(), 'method' => $route->namedMethod(), 'original_method' => $route->originalJsMethod(), 'isInvokable' => $isInvokable = $route->hasInvokableController(), - 'shouldExport' => $isInvokable, + 'shouldExport' => (! $isInvokable) || str_contains($controller, '\\Closure'), 'path' => $route->controllerPath(), 'line' => $route->controllerMethodLineNumber(), 'parameters' => $route->parameters(), From 6907992f2622ade04524faaad42df40e2ff270ec Mon Sep 17 00:00:00 2001 From: Istiak Tridip <13367189+istiak-tridip@users.noreply.github.com> Date: Tue, 24 Jun 2025 18:10:18 +0600 Subject: [PATCH 7/7] refactor: fix reserved keyword sorting --- src/TypeScript.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/TypeScript.php b/src/TypeScript.php index 8ec934a..a0180c7 100644 --- a/src/TypeScript.php +++ b/src/TypeScript.php @@ -7,6 +7,7 @@ class TypeScript { public const RESERVED_KEYWORDS = [ + 'await', 'break', 'case', 'catch', @@ -18,6 +19,7 @@ class TypeScript 'delete', 'do', 'else', + 'enum', 'export', 'extends', 'false', @@ -25,12 +27,20 @@ class TypeScript 'for', 'function', 'if', + 'implements', 'import', 'in', 'instanceof', + 'interface', + 'let', 'new', 'null', + 'package', + 'private', + 'protected', + 'public', 'return', + 'static', 'super', 'switch', 'this', @@ -42,16 +52,6 @@ class TypeScript 'void', 'while', 'with', - 'public', - 'private', - 'protected', - 'static', - 'package', - 'let', - 'enum', - 'await', - 'implements', - 'interface', 'yield', ];