Skip to content

Commit

Permalink
Merge pull request #367 from morphsteve/quote-typescript-property-names
Browse files Browse the repository at this point in the history
Quote non-standard object keys when transforming to Typescript
  • Loading branch information
rubenvanassche authored Mar 2, 2023
2 parents 2c7fe45 + 3419ca9 commit ef671d7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ function (string $carry, ReflectionProperty $property) use ($isOptional, $dataCl

$propertyName = $dataProperty->outputMappedName ?? $dataProperty->name;

if (! preg_match('/^[$_a-zA-Z][$_a-zA-Z0-9]*$/', $propertyName)) {
$propertyName = "'{$propertyName}'";
}

return $isOptional
? "{$carry}{$propertyName}?: {$transformed};" . PHP_EOL
: "{$carry}{$propertyName}: {$transformed};" . PHP_EOL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,12 @@ public function __construct(
it('outputs types with properties using their mapped name', function () {
$config = TypeScriptTransformerConfig::create();

$data = new class ('Good job Ruben') extends Data {
$data = new class ('Good job Ruben', 'Hi Ruben') extends Data {
public function __construct(
#[MapOutputName(SnakeCaseMapper::class)]
public string $someCamelCaseProperty,
#[MapOutputName('some:non:standard:property')]
public string $someNonStandardProperty,
) {
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
some_camel_case_property: string;
'some:non:standard:property': string;
}

0 comments on commit ef671d7

Please sign in to comment.