Skip to content

Commit 49edb3b

Browse files
Merge pull request #52 from laravel/ts-cleanup
Blade + TypeScript clean up
2 parents 84be2c6 + 5b5bcdd commit 49edb3b

File tree

6 files changed

+129
-78
lines changed

6 files changed

+129
-78
lines changed

resources/docblock.blade.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**{!! when(!str_contains($controller, '\\Closure'), "\n * @see {$controller}::{$method}") !!}
1+
/**{!! when(!str_contains($controller, '\\Closure'), PHP_EOL . " * @see {$controller}::" . ($isInvokable ? '__invoke' : $docblock_method ?? $method)) !!}
22
* @see {!! $path !!}:{!! $line !!}
33
@foreach ($parameters as $parameter)
44
@if ($parameter->default !== null)

resources/method.blade.ts

Lines changed: 68 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -19,51 +19,51 @@
1919
args = { {!! $parameters->first()->name !!}: args }
2020
}
2121

22-
@if ($parameters->first()->key)
23-
if (typeof args === 'object' && !Array.isArray(args) && @js($parameters->first()->key) in args) {
24-
args = { {!! $parameters->first()->name !!}: args.{!! $parameters->first()->key !!} }
25-
}
26-
27-
@endif
22+
@if ($parameters->first()->key)
23+
if (typeof args === 'object' && !Array.isArray(args) && @js($parameters->first()->key) in args) {
24+
args = { {!! $parameters->first()->name !!}: args.{!! $parameters->first()->key !!} }
25+
}
26+
@endif
2827
@endif
28+
2929
@if ($parameters->isNotEmpty())
3030
if (Array.isArray(args)) {
3131
args = {
32-
@foreach ($parameters as $parameter)
32+
@foreach ($parameters as $parameter)
3333
{!! $parameter->name !!}: args[{!! $loop->index !!}],
34-
@endforeach
34+
@endforeach
3535
}
3636
}
37-
3837
@endif
38+
3939
@if ($parameters->where('optional')->isNotEmpty())
4040
validateParameters(args, [
41-
@foreach ($parameters->where('optional') as $parameter)
41+
@foreach ($parameters->where('optional') as $parameter)
4242
"{!! $parameter->name !!}",
43-
@endforeach
43+
@endforeach
4444
])
45-
4645
@endif
46+
4747
@if ($parameters->isNotEmpty())
4848
const parsedArgs = {
49-
@foreach ($parameters as $parameter)
50-
@if ($parameter->key)
51-
{!! $parameter->name !!}: {!! when($parameter->default !== null, '(') !!}typeof args{!! when($parameters->every->optional, '?') !!}.{!! $parameter->name !!} === 'object'
52-
? args.{!! $parameter->name !!}.{!! $parameter->key ?? 'id' !!}
53-
: args{!! when($parameters->every->optional, '?') !!}.{!! $parameter->name !!}{!! when($parameter->default !== null, ') ?? ') !!}@if ($parameter->default !== null)@js($parameter->default)@endif,
54-
@else
55-
{!! $parameter->name !!}: args{!! when($parameters->every->optional, '?') !!}.{!! $parameter->name !!}{!! when($parameter->default !== null, ' ?? ') !!}@if ($parameter->default !== null)@js($parameter->default)@endif,
56-
@endif
57-
@endforeach
49+
@foreach ($parameters as $parameter)
50+
@if ($parameter->key)
51+
{!! $parameter->name !!}: {!! when($parameter->default !== null, '(') !!}typeof args{!! when($parameters->every->optional, '?') !!}.{!! $parameter->name !!} === 'object'
52+
? args.{!! $parameter->name !!}.{!! $parameter->key ?? 'id' !!}
53+
: args{!! when($parameters->every->optional, '?') !!}.{!! $parameter->name !!}{!! when($parameter->default !== null, ') ?? ') !!}@if ($parameter->default !== null)@js($parameter->default)@endif,
54+
@else
55+
{!! $parameter->name !!}: args{!! when($parameters->every->optional, '?') !!}.{!! $parameter->name !!}{!! when($parameter->default !== null, ' ?? ') !!}@if ($parameter->default !== null)@js($parameter->default)@endif,
56+
@endif
57+
@endforeach
5858
}
59-
6059
@endif
60+
6161
return {!! $method !!}.definition.url
6262
@foreach ($parameters as $parameter)
6363
.replace(@js($parameter->placeholder), parsedArgs.{!! $parameter->name !!}{!! when($parameter->optional, '?') !!}.toString(){!! when($parameter->optional, " ?? ''") !!})
64-
@if ($loop->last)
64+
@if ($loop->last)
6565
.replace(/\/+$/, '')
66-
@endif
66+
@endif
6767
@endforeach + queryParams(options)
6868
}
6969

@@ -76,47 +76,52 @@
7676
url: {!! $method !!}.url({!! when($parameters->isNotEmpty(), 'args, ') !!}options),
7777
method: @js($verb->actual),
7878
})
79-
8079
@endforeach
81-
@if ($withForm)
82-
@include('wayfinder::docblock')
83-
const {!! $method !!}Form = (@include('wayfinder::function-arguments')): {
84-
action: string,
85-
method: @js($verbs->first()->formSafe),
86-
} => ({
87-
action: {!! $method !!}.url({!! when($parameters->isNotEmpty(), 'args, ') !!}@if ($verbs->first()->formSafe !== $verbs->first()->actual)
88-
{
89-
[options?.mergeQuery ? 'mergeQuery' : 'query']: {
90-
_method: @js(strtoupper($verbs->first()->actual)),
91-
...(options?.query ?? options?.mergeQuery ?? {}),
92-
}
93-
}
94-
@else
95-
options
96-
@endif),
97-
method: @js($verbs->first()->formSafe),
98-
})
9980

100-
@foreach ($verbs as $verb)
101-
@include('wayfinder::docblock')
102-
{!! $method !!}Form.{!! $verb->actual !!} = (@include('wayfinder::function-arguments')): {
103-
action: string,
104-
method: @js($verb->formSafe),
105-
} => ({
106-
action: {!! $method !!}.url({!! when($parameters->isNotEmpty(), 'args, ') !!}@if ($verb->formSafe !== $verb->actual)
107-
{
108-
[options?.mergeQuery ? 'mergeQuery' : 'query']: {
109-
_method: @js(strtoupper($verb->actual)),
110-
...(options?.query ?? options?.mergeQuery ?? {}),
111-
}
112-
}
113-
@else
114-
options
115-
@endif),
116-
method: @js($verb->formSafe),
117-
})
81+
@if ($withForm)
82+
@include('wayfinder::docblock')
83+
const {!! $method !!}Form = (@include('wayfinder::function-arguments')): {
84+
action: string,
85+
method: @js($verbs->first()->formSafe),
86+
} => ({
87+
action: {!! $method !!}.url(
88+
{!! when($parameters->isNotEmpty(), 'args, ') !!}
89+
@if ($verbs->first()->formSafe === $verbs->first()->actual)
90+
options
91+
@else
92+
{
93+
[options?.mergeQuery ? 'mergeQuery' : 'query']: {
94+
_method: @js(strtoupper($verbs->first()->actual)),
95+
...(options?.query ?? options?.mergeQuery ?? {}),
96+
}
97+
}
98+
@endif
99+
),
100+
method: @js($verbs->first()->formSafe),
101+
})
118102

119-
@endforeach
120-
{!! $method !!}.form = {!! $method !!}Form
103+
@foreach ($verbs as $verb)
104+
@include('wayfinder::docblock')
105+
{!! $method !!}Form.{!! $verb->actual !!} = (@include('wayfinder::function-arguments')): {
106+
action: string,
107+
method: @js($verb->formSafe),
108+
} => ({
109+
action: {!! $method !!}.url(
110+
{!! when($parameters->isNotEmpty(), 'args, ') !!}
111+
@if ($verb->formSafe === $verb->actual)
112+
options
113+
@else
114+
{
115+
[options?.mergeQuery ? 'mergeQuery' : 'query']: {
116+
_method: @js(strtoupper($verb->actual)),
117+
...(options?.query ?? options?.mergeQuery ?? {}),
118+
}
119+
}
120+
@endif
121+
),
122+
method: @js($verb->formSafe),
123+
})
124+
@endforeach
121125

126+
{!! $method !!}.form = {!! $method !!}Form
122127
@endif

resources/multi-method.blade.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
@use('Illuminate\Support\HtmlString')
2-
31
@foreach ($routes as $route)
4-
@include('wayfinder::method', [
5-
...$route,
6-
'method' => $route['tempMethod'],
7-
'export' => false,
8-
])
2+
@include('wayfinder::method', [
3+
...$route,
4+
'method' => $route['tempMethod'],
5+
'docblock_method' => $route['method'],
6+
'export' => false,
7+
])
98
@endforeach
9+
1010
{!! when(!$isInvokable, 'export ') !!}const {!! $method !!} = {
1111
@foreach ($routes as $route)
1212
@js($route['uri']): {!! $route['tempMethod'] !!},
1313
@endforeach
14-
}{{"\n"}}
14+
}{{PHP_EOL}}

src/GenerateCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ private function writeMultiRouteControllerMethodExport(Collection $routes, strin
170170
'isInvokable' => $routes->first()->hasInvokableController(),
171171
'withForm' => $this->option('with-form') ?? false,
172172
'routes' => $routes->map(fn ($r) => [
173+
'method' => $r->jsMethod(),
173174
'tempMethod' => $r->jsMethod().md5($r->uri()),
174175
'parameters' => $r->parameters(),
175176
'verbs' => $r->verbs(),

src/Route.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,13 @@ public function controllerPath(): string
129129
$controller = $this->controller();
130130

131131
if ($controller === '\\Closure') {
132-
return $this->relativePath((new ReflectionClosure($this->closure()))->getFileName());
132+
$path = $this->relativePath((new ReflectionClosure($this->closure()))->getFileName());
133+
134+
if (str_contains($path, 'laravel-serializable-closure')) {
135+
return '[serialized-closure]';
136+
}
137+
138+
return $path;
133139
}
134140

135141
if (! class_exists($controller)) {

src/TypeScript.php

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,27 +70,66 @@ public static function cleanUp(string $view): string
7070
$replacements = [
7171
' ,' => ',',
7272
'[ ' => '[',
73-
' ]' => ']',
7473
', }' => ' }',
7574
'} )' => '})',
76-
' )' => ' )',
75+
' )' => ')',
7776
'( ' => '(',
7877
'( ' => '(',
79-
"\n +" => ' +',
78+
PHP_EOL.' +' => ' +',
79+
'})'.PHP_EOL.'/**' => '})'.PHP_EOL.PHP_EOL.'/**',
80+
'}'.PHP_EOL.'/**' => '}'.PHP_EOL.PHP_EOL.'/**',
81+
];
82+
83+
$regexReplacements = [
84+
'/\=\> \{\n{2,}/' => '=> {'.PHP_EOL,
85+
'/\\s+\.replace/' => sprintf('%s%s.replace', PHP_EOL, str_repeat(' ', 12)),
86+
'/\s+\+ queryParams\(options\)/' => ' + queryParams(options)',
87+
'/\n{3,}/' => "\n\n",
8088
];
8189

8290
return str($view)
8391
->pipe(function (Stringable $str) {
8492
// Clean up function arguments
85-
$matches = $str->matchAll('/ = \(([^)]+\))/');
93+
$matches = $str->matchAll('/ = \(([^)]+\))/')
94+
->concat($str->matchAll('/\.url\(\s*args,\s+\{/'))
95+
->concat($str->matchAll('/\.url\(\s*args,\s+options\s*\)/'))
96+
->concat($str->matchAll('/\.url\(\s*options\s*\)/'))
97+
->concat($str->matchAll('/\(\s+\{/'))
98+
->concat($str->matchAll('/\}\s+\)/'));
8699

87100
foreach ($matches as $match) {
88101
$str = $str->replaceFirst($match, preg_replace('/\s+/', ' ', $match));
89102
}
90103

91104
return $str;
92105
})
93-
->replaceMatches('/\n{3,}/', "\n\n")
106+
->pipe(function (Stringable $str) {
107+
$depth = 0;
108+
109+
return str(
110+
$str->explode(PHP_EOL)
111+
->map(fn ($s) => trim($s))
112+
->map(function ($s) use (&$depth) {
113+
if ($s === '') {
114+
return $s;
115+
}
116+
117+
if (str_starts_with($s, '}') || str_starts_with($s, ']')) {
118+
$depth--;
119+
}
120+
121+
$line = str_repeat(' ', $depth * 4).$s;
122+
123+
if (str_ends_with($s, '{') || str_ends_with($s, '[')) {
124+
$depth++;
125+
}
126+
127+
return $line;
128+
})
129+
->implode(PHP_EOL)
130+
);
131+
})
132+
->replaceMatches(array_keys($regexReplacements), array_values($regexReplacements))
94133
->replace(array_keys($replacements), array_values($replacements))
95134
->toString();
96135
}

0 commit comments

Comments
 (0)