From 5c1ead61f3ab3b77ea797a5eb70bb9a02926cd4e Mon Sep 17 00:00:00 2001 From: MONDESIR Malik Date: Sat, 19 Apr 2025 17:47:25 +0200 Subject: [PATCH 1/2] Add TypeScript variant for React adapter in InertiaPageGenerator + test fixture --- src/Generators/Statements/InertiaPageGenerator.php | 9 +++++++++ .../Generators/Statements/InertiaPageGeneratorTest.php | 1 + tests/fixtures/inertia-pages/customer-show.tsx | 10 ++++++++++ 3 files changed, 20 insertions(+) create mode 100644 tests/fixtures/inertia-pages/customer-show.tsx diff --git a/src/Generators/Statements/InertiaPageGenerator.php b/src/Generators/Statements/InertiaPageGenerator.php index 0d6c3b43..c8ca026e 100644 --- a/src/Generators/Statements/InertiaPageGenerator.php +++ b/src/Generators/Statements/InertiaPageGenerator.php @@ -15,6 +15,7 @@ class InertiaPageGenerator extends StatementGenerator implements Generator protected array $adapters = [ 'vue3' => ['framework' => 'vue', 'extension' => '.vue'], 'react' => ['framework' => 'react', 'extension' => '.jsx'], + 'reactts' => ['framework' => 'react', 'extension' => '.tsx'], 'svelte' => ['framework' => 'svelte', 'extension' => '.svelte'], ]; @@ -68,6 +69,14 @@ protected function getAdapter(): ?array if (preg_match('/@inertiajs\/(vue3|react|svelte)/i', $contents, $matches)) { $adapterKey = strtolower($matches[1]); + if ($adapterKey === 'react') { + $tsConfigPath = base_path('tsconfig.json'); + + if ($this->filesystem->exists($tsConfigPath) || preg_match('/"typescript"/i', $contents)) { + $adapterKey .= 'ts'; + } + } + return $this->adapters[$adapterKey] ?? null; } diff --git a/tests/Feature/Generators/Statements/InertiaPageGeneratorTest.php b/tests/Feature/Generators/Statements/InertiaPageGeneratorTest.php index 32eda257..40107c30 100644 --- a/tests/Feature/Generators/Statements/InertiaPageGeneratorTest.php +++ b/tests/Feature/Generators/Statements/InertiaPageGeneratorTest.php @@ -147,6 +147,7 @@ public static function inertiaAdaptersDataProvider(): array return [ ['vue', '"@inertiajs/vue3": "^2.0.0"', 'resources/js/Pages/Customer/Show.vue', '.vue'], ['react', '"@inertiajs/react": "^2.0.0"', 'resources/js/Pages/Customer/Show.jsx', '.jsx'], + ['react', '"@inertiajs/react": "^2.0.0", "typescript": "^5.0.0"', 'resources/js/Pages/Customer/Show.tsx', '.tsx'], ['svelte', '"@inertiajs/svelte": "^2.0.0"', 'resources/js/Pages/Customer/Show.svelte', '.svelte'], ]; } diff --git a/tests/fixtures/inertia-pages/customer-show.tsx b/tests/fixtures/inertia-pages/customer-show.tsx new file mode 100644 index 00000000..436d5a04 --- /dev/null +++ b/tests/fixtures/inertia-pages/customer-show.tsx @@ -0,0 +1,10 @@ +import { Head } from '@inertiajs/react' + +export default function Show({ customer, customers }) { + return ( +
+ +

Customer Show

+
+ ) +} From 3a6429dd2e60ed99a4caa63ddab2bcebc0934e43 Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Tue, 15 Jul 2025 09:11:25 -0400 Subject: [PATCH 2/2] Streamlines --- .../Statements/InertiaPageGenerator.php | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/Generators/Statements/InertiaPageGenerator.php b/src/Generators/Statements/InertiaPageGenerator.php index c8ca026e..a3006de7 100644 --- a/src/Generators/Statements/InertiaPageGenerator.php +++ b/src/Generators/Statements/InertiaPageGenerator.php @@ -15,7 +15,6 @@ class InertiaPageGenerator extends StatementGenerator implements Generator protected array $adapters = [ 'vue3' => ['framework' => 'vue', 'extension' => '.vue'], 'react' => ['framework' => 'react', 'extension' => '.jsx'], - 'reactts' => ['framework' => 'react', 'extension' => '.tsx'], 'svelte' => ['framework' => 'svelte', 'extension' => '.svelte'], ]; @@ -59,28 +58,25 @@ public function output(Tree $tree): array protected function getAdapter(): ?array { $packagePath = base_path('package.json'); - if (!$this->filesystem->exists($packagePath)) { return null; } $contents = $this->filesystem->get($packagePath); + if (!preg_match('/@inertiajs\/(vue3|react|svelte)/i', $contents, $matches)) { + return null; + } - if (preg_match('/@inertiajs\/(vue3|react|svelte)/i', $contents, $matches)) { - $adapterKey = strtolower($matches[1]); - - if ($adapterKey === 'react') { - $tsConfigPath = base_path('tsconfig.json'); - - if ($this->filesystem->exists($tsConfigPath) || preg_match('/"typescript"/i', $contents)) { - $adapterKey .= 'ts'; - } - } + $adapterKey = strtolower($matches[1]); + if (!isset($this->adapters[$adapterKey])) { + return null; + } - return $this->adapters[$adapterKey] ?? null; + if ($adapterKey === 'react' && ($this->filesystem->exists(base_path('tsconfig.json')) || preg_match('/"typescript"/i', $contents))) { + return array_replace($this->adapters[$adapterKey], ['extension' => '.tsx']); } - return null; + return $this->adapters[$adapterKey]; } protected function getStatementPath(string $view): string