Skip to content

Commit

Permalink
Fix bug where ts transformer did not output key types
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Mar 20, 2024
1 parent 09b38e5 commit 66e2510
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
24 changes: 21 additions & 3 deletions src/Support/TypeScriptTransformer/DataTypeScriptTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use phpDocumentor\Reflection\Type;
use phpDocumentor\Reflection\Types\Array_;
use phpDocumentor\Reflection\Types\Boolean;
use phpDocumentor\Reflection\Types\Compound;
use phpDocumentor\Reflection\Types\Integer;
use phpDocumentor\Reflection\Types\Nullable;
use phpDocumentor\Reflection\Types\Object_;
Expand Down Expand Up @@ -87,8 +88,8 @@ function (string $carry, ReflectionProperty $property) use ($isOptional, $dataCl
}

return $isOptional
? "{$carry}{$propertyName}?: {$transformed};" . PHP_EOL
: "{$carry}{$propertyName}: {$transformed};" . PHP_EOL;
? "{$carry}{$propertyName}?: {$transformed};".PHP_EOL
: "{$carry}{$propertyName}: {$transformed};".PHP_EOL;
},
''
);
Expand All @@ -108,7 +109,10 @@ protected function resolveTypeForProperty(
}

$collectionType = match ($dataProperty->type->kind) {
DataTypeKind::DataCollection, DataTypeKind::DataArray, DataTypeKind::DataEnumerable, DataTypeKind::Array, DataTypeKind::Enumerable => $this->defaultCollectionType($dataProperty->type->dataClass),
DataTypeKind::DataCollection, DataTypeKind::DataArray, DataTypeKind::DataEnumerable, DataTypeKind::Array, DataTypeKind::Enumerable => $this->dataCollectionType(
$dataProperty->type->dataClass,
$dataProperty->type->iterableKeyType
),
DataTypeKind::DataPaginator, DataTypeKind::DataPaginatedCollection, DataTypeKind::Paginator => $this->paginatedCollectionType($dataProperty->type->dataClass),
DataTypeKind::DataCursorPaginator, DataTypeKind::DataCursorPaginatedCollection, DataTypeKind::CursorPaginator => $this->cursorPaginatedCollectionType($dataProperty->type->dataClass),
default => throw new RuntimeException('Cannot end up here since the type is dataCollectable')
Expand All @@ -121,6 +125,20 @@ protected function resolveTypeForProperty(
return $collectionType;
}

protected function dataCollectionType(string $class, ?string $keyType): Type
{
$keyType = match ($keyType) {
'string' => new String_(),
'int' => new Integer(),
default => new Compound([new String_(), new Integer()]),
};

return new Array_(
new Object_(new Fqsen("\\{$class}")),
$keyType
);
}

protected function defaultCollectionType(string $class): Type
{
return new Array_(new Object_(new Fqsen("\\{$class}")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ public function __construct(
public string $name,
) {
}
};
}

;

$transformer = new DataTypeScriptTransformer($config);
$reflection = new ReflectionClass(DummyTypeScriptOptionalClass::class);
Expand All @@ -233,3 +235,29 @@ public function __construct(
$transformer->transform($reflection, 'DataObject')->transformed
);
});

it('can transform a collection as a TypeScript record', function () {
$config = TypeScriptTransformerConfig::create();

$data = new class () extends Data {
/** @var \Spatie\LaravelData\Tests\Fakes\SimpleData[] */
public array $collectionAsArray;

/** @var array<string, \Spatie\LaravelData\Tests\Fakes\SimpleData> */
public array $collectionAsRecord;
};

$transformer = new DataTypeScriptTransformer($config);
$reflection = new ReflectionClass($data);

$this->assertTrue($transformer->canTransform($reflection));
$this->assertEquals(
<<<TXT
{
collectionAsArray: Array<{%Spatie\LaravelData\Tests\Fakes\SimpleData%}>;
collectionAsRecord: { [key: string]: {%Spatie\LaravelData\Tests\Fakes\SimpleData%} };
}
TXT,
$transformer->transform($reflection, 'DataObject')->transformed
);
});

0 comments on commit 66e2510

Please sign in to comment.