Skip to content

Resolve multiple bugs #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion resources/method.blade.ts
Original file line number Diff line number Diff line change
@@ -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),
})
Expand Down
4 changes: 2 additions & 2 deletions resources/multi-method.blade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 11 additions & 10 deletions src/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,12 @@ 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(),
'tempMethod' => $r->jsMethod().md5($r->uri()),
'tempMethod' => $r->jsMethod().hash('xxh3', $r->uri()),
'parameters' => $r->parameters(),
'verbs' => $r->verbs(),
'uri' => $r->uri(),
Expand All @@ -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(),
Expand Down Expand Up @@ -245,10 +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' => false,
'isInvokable' => $isInvokable = $route->hasInvokableController(),
'shouldExport' => (! $isInvokable) || str_contains($controller, '\\Closure'),
'path' => $route->controllerPath(),
'line' => $route->controllerMethodLineNumber(),
'parameters' => $route->parameters(),
Expand All @@ -266,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()),
],
]);

Expand All @@ -290,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, <<<JAVASCRIPT

Expand Down
13 changes: 12 additions & 1 deletion src/TypeScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class TypeScript
{
public const RESERVED_KEYWORDS = [
'await',
'break',
'case',
'catch',
Expand All @@ -18,19 +19,28 @@ class TypeScript
'delete',
'do',
'else',
'enum',
'export',
'extends',
'false',
'finally',
'for',
'function',
'if',
'implements',
'import',
'in',
'instanceof',
'interface',
'let',
'new',
'null',
'package',
'private',
'protected',
'public',
'return',
'static',
'super',
'switch',
'this',
Expand All @@ -42,6 +52,7 @@ class TypeScript
'void',
'while',
'with',
'yield',
];

public static function safeMethod(string $method, string $suffix): string
Expand All @@ -58,7 +69,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);
}

Expand Down