10000 [AssetMapper] Fix import map package parsing with an @ namespace by weaverryan · Pull Request #50206 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[AssetMapper] Fix import map package parsing with an @ namespace #50206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
8000 Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ public function update(): array
*/
public static function parsePackageName(string $packageName): ?array
{
// https://regex101.com/r/58bl9L/1
$regex = '/(?:(?P<registry>[^:\n]+):)?(?P<package>[^@\n]+)(?:@(?P<version>[^\s\n]+))?/';
// https://regex101.com/r/d99BEc/1
Copy link
Member

Choose a reason for hiding this comment

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

I would remove this comment entirely. Someone wanting to get the regex explanation on regex101.com can copy-paste the regex instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

I got this idea from @dunglas - the nice thing about it is that the regex includes a bunch of examples. So if there is a problem in the future, you can quickly open this to see what the passing test cases are and then either validate that (A) the situation you are having trouble with SHOULD be working or (B) that your test case was never included.

Copy link
Member

Choose a reason for hiding this comment

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

Exactly, the main benefit it that we can add tests for the regex.

$regex = '/(?:(?P<registry>[^:\n]+):)?((?P<package>@?[^@\n]+))(?:@(?P<version>[^\s\n]+))?/';

return preg_match($regex, $packageName, $matches) ? $matches : null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,40 @@ public static function getPackageNameTests(): iterable
'version' => '^1.2.3',
],
];

yield 'namespaced_package_simple' => [
'@hotwired/stimulus',
[
'package' => '@hotwired/stimulus',
'registry' => '',
],
];

yield 'namespaced_package_with_version_constraint' => [
'@hotwired/stimulus@^1.2.3',
[
'package' => '@hotwired/stimulus',
'registry' => '',
'version' => '^1.2.3',
],
];

yield 'namespaced_package_with_registry' => [
'npm:@hotwired/stimulus',
[
'package' => '@hotwired/stimulus',
'registry' => 'npm',
],
];

yield 'namespaced_package_with_registry_and_version' => [
'npm:@hotwired/stimulus@^1.2.3',
[
'package' => '@hotwired/stimulus',
'registry' => 'npm',
'version' => '^1.2.3',
],
];
}

private function createImportMapManager(array $dirs, string $rootDir, string $publicPrefix = '/assets/', string $publicDirName = 'public'): ImportMapManager
Expand Down
0