mirror of
https://github.com/flutter/packages.git
synced 2025-08-06 17:28:42 +08:00
[tools] Validate pubspec topic format (#5565)
Fixes https://github.com/flutter/flutter/issues/139305
This commit is contained in:
@ -332,7 +332,7 @@ class PubspecCheckCommand extends PackageLoopingCommand {
|
||||
false;
|
||||
}
|
||||
|
||||
// Validates the "implements" keyword for a plugin, returning an error
|
||||
// Validates the "topics" keyword for a plugin, returning an error
|
||||
// string if there are any issues.
|
||||
String? _checkTopics(
|
||||
Pubspec pubspec, {
|
||||
@ -343,6 +343,10 @@ class PubspecCheckCommand extends PackageLoopingCommand {
|
||||
return 'A published package should include "topics". '
|
||||
'See https://dart.dev/tools/pub/pubspec#topics.';
|
||||
}
|
||||
if (topics.length > 5) {
|
||||
return 'A published package should have maximum 5 topics. '
|
||||
'See https://dart.dev/tools/pub/pubspec#topics.';
|
||||
}
|
||||
if (isFlutterPlugin(package) && package.isFederated) {
|
||||
final String pluginName = package.directory.parent.basename;
|
||||
// '_' isn't allowed in topics, so convert to '-'.
|
||||
@ -352,6 +356,19 @@ class PubspecCheckCommand extends PackageLoopingCommand {
|
||||
'a topic. Add "$topicName" to the "topics" section.';
|
||||
}
|
||||
}
|
||||
|
||||
// Validates topic names according to https://dart.dev/tools/pub/pubspec#topics
|
||||
final RegExp expectedTopicFormat = RegExp(r'^[a-z](?:-?[a-z0-9]+)*$');
|
||||
final Iterable<String> invalidTopics = topics.where((String topic) =>
|
||||
!expectedTopicFormat.hasMatch(topic) ||
|
||||
topic.length < 2 ||
|
||||
topic.length > 32);
|
||||
if (invalidTopics.isNotEmpty) {
|
||||
return 'Invalid topic(s): ${invalidTopics.join(', ')} in "topics" section. '
|
||||
'Topics must consist of lowercase alphanumerical characters or dash (but no double dash), '
|
||||
'start with a-z and ending with a-z or 0-9, have a minimum of 2 characters '
|
||||
'and have a maximum of 32 characters.';
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user