Skip to content

Commit 26a937d

Browse files
committed
fix: tests for @boostsnippet and conditional assistant inclusion
1 parent abd2ca5 commit 26a937d

File tree

7 files changed

+37
-33
lines changed

7 files changed

+37
-33
lines changed

.ai/inertia-laravel/2/core.blade.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@
2121
@else
2222
- This version of Inertia does NOT support `resetOnError`, `resetOnSuccess`, or `setDefaultsOnSuccess` on the `<Form>` component. Using these will cause errors.
2323
@endif
24+
@else
25+
- Build forms using the `useForm` helper. Use the code examples and `search-docs` tool with the `useForm helper` query for guidance.
2426
@endif

.ai/inertia-react/core.blade.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
- Use `router.visit()` or `<Link>` for navigation instead of traditional links.
44

5-
<code-snippet lang="react" name="Inertia Client Navigation">
6-
import { Link } from '@inertiajs/react'
7-
8-
<Link href="/">Home</Link>
9-
</code-snippet>
5+
@boostsnippet("Inertia Client Navigation", "react")
6+
import { Link } from '@inertiajs/react'
7+
<Link href="/">Home</Link>
8+
@endboostsnippet

.ai/inertia-vue/core.blade.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
- Vue components must have a single root element.
44
- Use `router.visit()` or `<Link>` for navigation instead of traditional links.
55

6-
<code-snippet lang="vue" name="Inertia Client Navigation">
6+
@boostsnippet("Inertia Client Navigation", "vue")
77
import { Link } from '@inertiajs/vue3'
8-
98
<Link href="/">Home</Link>
10-
</code-snippet>
9+
@endboostsnippet

src/BoostServiceProvider.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -166,22 +166,6 @@ private function registerBrowserLogger(): void
166166
private function registerBladeDirectives(BladeCompiler $bladeCompiler): void
167167
{
168168
$bladeCompiler->directive('boostJs', fn () => '<?php echo \\Laravel\\Boost\\Services\\BrowserLogger::getScript(); ?>');
169-
170-
$bladeCompiler->directive('boostsnippet', function (string $expression) {
171-
preg_match('/^(.*?),\s*(.*?)$/', $expression, $matches);
172-
$name = isset($matches[1]) ? trim($matches[1], '\'"` ') : '';
173-
$lang = isset($matches[2]) ? trim($matches[2], '\'"` ') : 'html';
174-
$name = str_replace('___SINGLE_BACKTICK___', '`', $name);
175-
176-
return "<?php \$__snippetStack[] = ['name' => '{$name}', 'lang' => '{$lang}']; ob_start(); ?>";
177-
});
178-
179-
$bladeCompiler->directive('endboostsnippet', function () {
180-
return '<?php $__snippetContent = ob_get_clean(); $__snippet = array_pop($__snippetStack); ?>'
181-
.'<code-snippet name="<?php echo $__snippet[\'name\']; ?>" lang="<?php echo $__snippet[\'lang\']; ?>">'."\n"
182-
.'<?php echo $__snippetContent; ?>'
183-
.'</code-snippet>'."\n\n";
184-
});
185169
}
186170

187171
private static function mapJsTypeToPsr3Level(string $type): string

src/Install/Assists/Inertia.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function __construct(private Roster $roster)
1414
public function gte(string $version): bool
1515
{
1616
return
17-
$this->roster->usesVersion(Packages::INERTIA, $version, '>=') ||
17+
$this->roster->usesVersion(Packages::INERTIA_LARAVEL, $version, '>=') ||
1818
$this->roster->usesVersion(Packages::INERTIA_REACT, $version, '>=') ||
1919
$this->roster->usesVersion(Packages::INERTIA_SVELTE, $version, '>=') ||
2020
$this->roster->usesVersion(Packages::INERTIA_VUE, $version, '>=');

src/Install/GuidelineComposer.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static function composeGuidelines(Collection $guidelines): string
5353
// We want to allow indentation in the guideline blade files
5454
// But we don't want the indentation in the outputted file
5555

56-
return trim($guidelines
56+
return str_replace("\n\n\n\n", "\n\n", trim($guidelines
5757
->filter(fn ($content) => ! empty(trim($content)))
5858
->map(function ($content, $key) {
5959
// Remove preceding indentation from `- guidelines`
@@ -63,7 +63,7 @@ public static function composeGuidelines(Collection $guidelines): string
6363

6464
return "\n=== {$key} rules ===\n\n".trim($content);
6565
})
66-
->join("\n\n"));
66+
->join("\n\n")));
6767
}
6868

6969
/**
@@ -206,8 +206,8 @@ protected function guideline(string $path): ?string
206206
return null;
207207
}
208208

209-
// Read the file content
210209
$content = file_get_contents($path);
210+
$content = $this->processBoostSnippets($content);
211211

212212
// Temporarily replace backticks and PHP opening tags with placeholders before Blade processing
213213
// This prevents Blade from trying to execute PHP code examples and supports inline code
@@ -221,7 +221,26 @@ protected function guideline(string $path): ?string
221221
'assist' => $this->guidelineAssist,
222222
]);
223223
$rendered = str_replace(array_values($placeholders), array_keys($placeholders), $rendered);
224+
$rendered = str_replace(array_keys($this->storedSnippets), array_values($this->storedSnippets), $rendered);
225+
$this->storedSnippets = []; // Clear for next use
224226

225227
return trim($rendered);
226228
}
229+
230+
private array $storedSnippets = [];
231+
232+
private function processBoostSnippets(string $content): string
233+
{
234+
return preg_replace_callback('/(?<!@)@boostsnippet\(\s*(?P<nameQuote>[\'"])(?P<name>[^\1]*?)\1(?:\s*,\s*(?P<langQuote>[\'"])(?P<lang>[^\3]*?)\3)?\s*\)(?P<content>.*?)@endboostsnippet/s', function ($matches) {
235+
$name = $matches['name'];
236+
$lang = ! empty($matches['lang']) ? $matches['lang'] : 'html';
237+
$snippetContent = $matches['content'];
238+
239+
$placeholder = '___BOOST_SNIPPET_'.count($this->storedSnippets).'___';
240+
241+
$this->storedSnippets[$placeholder] = '<code-snippet name="'.$name.'" lang="'.$lang.'">'."\n".$snippetContent."\n".'</code-snippet>'."\n\n";
242+
243+
return $placeholder;
244+
}, $content);
245+
}
227246
}

tests/Feature/Install/GuidelineComposerTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@
2525
$packages = new PackageCollection([
2626
new Package(Packages::LARAVEL, 'laravel/framework', '11.0.0'),
2727
new Package(Packages::INERTIA_REACT, 'inertiajs/inertia-react', $version),
28+
new Package(Packages::INERTIA_LARAVEL, 'inertiajs/inertia-laravel', $shouldInclude212Features ? '2.1.2' : '2.1.0'),
2829
]);
2930

3031
$this->roster->shouldReceive('packages')->andReturn($packages);
3132
// Mock all Inertia package version checks
3233
$this->roster->shouldReceive('usesVersion')
33-
->with(Packages::INERTIA, '2.1.0', '>=')
34-
->andReturn(false);
34+
->with(Packages::INERTIA_LARAVEL, '2.1.0', '>=')
35+
->andReturn($shouldIncludeForm);
3536
$this->roster->shouldReceive('usesVersion')
3637
->with(Packages::INERTIA_REACT, '2.1.0', '>=')
3738
->andReturn($shouldIncludeForm);
@@ -43,8 +44,8 @@
4344
->andReturn(false);
4445

4546
$this->roster->shouldReceive('usesVersion')
46-
->with(Packages::INERTIA, '2.1.2', '>=')
47-
->andReturn(false);
47+
->with(Packages::INERTIA_LARAVEL, '2.1.2', '>=')
48+
->andReturn($shouldInclude212Features);
4849
$this->roster->shouldReceive('usesVersion')
4950
->with(Packages::INERTIA_REACT, '2.1.2', '>=')
5051
->andReturn($shouldInclude212Features);
@@ -176,7 +177,7 @@
176177
$this->roster->shouldReceive('packages')->andReturn($packages);
177178
// Mock all Inertia package version checks for this test too
178179
$this->roster->shouldReceive('usesVersion')
179-
->with(Packages::INERTIA, '2.1.0', '>=')
180+
->with(Packages::INERTIA_LARAVEL, '2.1.0', '>=')
180181
->andReturn(false);
181182
$this->roster->shouldReceive('usesVersion')
182183
->with(Packages::INERTIA_REACT, '2.1.0', '>=')

0 commit comments

Comments
 (0)