From 52c1769e0dd42c6daf0a67c96c60e6327b88ab62 Mon Sep 17 00:00:00 2001 From: jon Date: Wed, 14 Aug 2024 22:34:10 +0100 Subject: [PATCH] fix: support multiple transforms on typescript writter --- src/Helpers/TypeDefinitionWriter.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Helpers/TypeDefinitionWriter.php b/src/Helpers/TypeDefinitionWriter.php index 4eceeab..29f07d0 100644 --- a/src/Helpers/TypeDefinitionWriter.php +++ b/src/Helpers/TypeDefinitionWriter.php @@ -13,8 +13,11 @@ public function format(TypesCollection $collection): string $replacements = config('laravel-helpers.typescript.replace', []); return Str::of(parent::format($collection)) - ->replaceMatches('/(\w+)\??: ([\w\.<>]+) \| null;/', function ($matches) { - return sprintf('%s?: %s', $matches[1], $matches[2]); + ->replaceMatches('/(\w+)(\??:)\s*([\w\s\[\]:<>,{}\.]+)(\s*\|\s*null)?;/', function ($matches) { + $name = $matches[1]; + $type = $matches[3]; + $isOptional = $matches[2] === '?:' || isset($matches[4]); + return sprintf('%s%s %s', $name, $isOptional ? '?:' : ':', trim($type)); }) // @phpstan-ignore-next-line ->replace(search: array_keys($replacements), replace: array_values($replacements))