Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 25 additions & 2 deletions 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", "WILDCARD"]
* - "*" → ["Permissions", "WILDCARD"]
*
* @return array{0: string, 1: string}
*/
Expand All @@ -317,17 +319,38 @@ private function parseNameToGroupAndConst(string $name, string $defaultGroup): a
$groupName = Str::studly($prefix).$defaultGroup;

// Convert action to UPPER_SNAKE_CASE
$constName = Str::upper(Str::snake($action));
$constName = $this->sanitizeConstName(Str::upper(Str::snake($action)));

return [$groupName, $constName];
}

// No prefix - use default group
$constName = Str::upper(Str::snake($name));
$constName = $this->sanitizeConstName(Str::upper(Str::snake($name)));

return [$defaultGroup, $constName];
}

/**
* Sanitize a constant name to be a valid TypeScript identifier.
*
* Replaces wildcards and other invalid characters with valid alternatives.
*/
private function sanitizeConstName(string $name): string
{
// Replace standalone wildcard or wildcard portions
$name = str_replace('*', 'WILDCARD', $name);

// Replace any remaining characters that aren't valid in identifiers
$name = (string) preg_replace('/[^A-Z0-9_]/', '_', $name);

// Ensure it doesn't start with a digit
if ($name !== '' && ctype_digit($name[0])) {
$name = '_'.$name;
}

return $name === '' ? 'UNKNOWN' : $name;
}

/**
* Group definitions by their source class.
*
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('sanitizes wildcard permissions to valid TypeScript identifiers', 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 should be sanitized to WILDCARD
expect($content)->toContain('WILDCARD: "*"');
expect($content)->toContain('WILDCARD: "article:*"');

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

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

Expand Down