Skip to content
Merged
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
16 changes: 15 additions & 1 deletion src/Commands/TypeScriptCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ private function collectCapabilities(DefinitionDiscoverer $discoverer): array
* - "article:view" → ["ArticlePermissions", "VIEW"]
* - "user.create" → ["UserPermissions", "CREATE"]
* - "admin" → ["Roles", "ADMIN"]
* - "article:*" → ["ArticlePermissions", "*"]
* - "*" → ["Permissions", "*"]
*
* @return array{0: string, 1: string}
*/
Expand All @@ -328,6 +330,14 @@ private function parseNameToGroupAndConst(string $name, string $defaultGroup): a
return [$defaultGroup, $constName];
}

/**
* Check if a string is a valid JavaScript/TypeScript identifier.
*/
private function isValidIdentifier(string $name): bool
{
return (bool) preg_match('/^[a-zA-Z_$][a-zA-Z0-9_$]*$/', $name);
}

/**
* Group definitions by their source class.
*
Expand Down Expand Up @@ -364,7 +374,11 @@ private function generateConstObject(string $groupName, array $items): string
$lines = ["export const {$groupName} = {"];

foreach ($items as $constName => $value) {
$lines[] = " {$constName}: \"{$value}\",";
// Quote keys that aren't valid identifiers (e.g., wildcards like "*")
// Use json_encode for proper escaping of special characters
$key = $this->isValidIdentifier($constName) ? $constName : json_encode($constName);
$escapedValue = json_encode($value);
$lines[] = " {$key}: {$escapedValue},";
Comment on lines +379 to +381
Copy link

Copilot AI Jan 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

json_encode() can return false (e.g., on invalid UTF-8) which would produce invalid TypeScript output (empty key/value or false string coercion). Consider using json_encode(..., JSON_THROW_ON_ERROR) (and handling the exception) or otherwise asserting the return type is a string before writing it into the generated file.

Copilot uses AI. Check for mistakes.
}

$lines[] = '} as const;';
Expand Down
22 changes: 22 additions & 0 deletions tests/Feature/Commands/TypeScriptCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,28 @@
expect($content)->toContain('export type Role');
});

it('quotes wildcard permissions as valid TypeScript property keys', function () {
config(['mandate.code_first.enabled' => false]);

// Create wildcard permissions in the database
OffloadProject\Mandate\Models\Permission::create(['name' => '*']);
OffloadProject\Mandate\Models\Permission::create(['name' => 'article:*']);

$outputFile = $this->outputPath.'/mandate.ts';

$this->artisan('mandate:typescript', ['--output' => $outputFile])
->assertSuccessful();

$content = file_get_contents($outputFile);

// Wildcard keys should be quoted
expect($content)->toContain('"*": "*"');
expect($content)->toContain('"*": "article:*"');

// Should NOT contain unquoted * as a key
expect($content)->not->toMatch('/^\s+\*:/m');
});

it('uses as const for objects', function () {
$outputFile = $this->outputPath.'/mandate.ts';

Expand Down