Skip to content

Commit

Permalink
fix: support multiple transforms on typescript writter
Browse files Browse the repository at this point in the history
  • Loading branch information
nikuscs committed Aug 14, 2024
1 parent 968ad65 commit 52c1769
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Helpers/TypeDefinitionWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 52c1769

Please sign in to comment.