Skip to content

Commit

Permalink
Merge pull request #76 from starter-dev/patch-2
Browse files Browse the repository at this point in the history
Fix: Ensure invalid typescript properties are surrounded with quotes
  • Loading branch information
tcampbPPU committed Jul 8, 2024
2 parents cf7a095 + e00f2ae commit 88c99b9
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/Actions/WriteColumnAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function __invoke(ReflectionClass $reflectionModel, array $attribute, arr
], $enumRef];
}

return ["{$indent} {$name}: {$type}\n", $enumRef];
return ["{$indent} {$this->ensurePropertyIsValid($name)}: {$type}\n", $enumRef];
}

protected function resolveEnum(string $returnTypeName): ?ReflectionClass
Expand All @@ -129,4 +129,25 @@ protected function resolveEnum(string $returnTypeName): ?ReflectionClass

return null;
}

/**
* Transforms invalid javascript property to valid one by surrounding it with quotes.
*
* This function checks if the property starts with a number or symbols. If it does, it
* surrounds the identifier with double quotes to make it a valid string key.
*/
private function ensurePropertyIsValid(string $identifier): string
{
$firstCharacter = substr($identifier, 0, 1);

if (! ctype_digit($identifier) && ctype_digit($firstCharacter)) {
return "'$identifier'";
}

if (preg_match('/^[^a-zA-Z0-9_$]/', $firstCharacter)) {
return "'$identifier'";
}

return $identifier;
}
}

0 comments on commit 88c99b9

Please sign in to comment.