Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Actions/TranspileTypeToTypeScriptAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use phpDocumentor\Reflection\PseudoType;
use phpDocumentor\Reflection\PseudoTypes\ArrayShape;
use phpDocumentor\Reflection\Type;
use phpDocumentor\Reflection\Types\AbstractList;
use phpDocumentor\Reflection\Types\Boolean;
Expand Down Expand Up @@ -51,6 +52,7 @@ public function execute(Type $type): string
$type instanceof AbstractList => $this->resolveListType($type),
$type instanceof Nullable => $this->resolveNullableType($type),
$type instanceof Object_ => $this->resolveObjectType($type),
$type instanceof ArrayShape => $this->resolveArrayShapeType($type),
$type instanceof StructType => $this->resolveStructType($type),
$type instanceof RecordType => $this->resolveRecordType($type),
$type instanceof TypeScriptType => (string) $type,
Expand Down Expand Up @@ -106,6 +108,18 @@ private function resolveObjectType(Object_ $object): string
);
}

private function resolveArrayShapeType(ArrayShape $type): string
{
$transformed = "{";

foreach ($type->getItems() as $type) {
$q = $type->isOptional() ? '?' : '';
$transformed .= "{$type->getKey()}{$q}:{$this->execute($type->getValue())};";
}

return "{$transformed}}";
}

private function resolveStructType(StructType $type): string
{
$transformed = "{";
Expand Down Expand Up @@ -133,6 +147,10 @@ private function resolveSelfReferenceType(): string

private function isTypeScriptArray(Type $keyType): bool
{
if ($keyType instanceof Integer) {
return true;
}

if (! $keyType instanceof Compound) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/TypeReflectors/PropertyTypeReflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected function getDocblock(): string

protected function docblockRegex(): string
{
return '/@var ((?:\s?[\\w?|\\\\<>,-]+(?:\[])?)+)/';
return '/@var ((?:\s?[\\w?|\\\\<>,-\{\}]+(?:\[])?)+)/';
}

protected function getReflectionType(): ?ReflectionType
Expand Down