Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(360)

Unified Diff: sdk/lib/_internal/pub/lib/src/validator/pubspec_field.dart

Issue 24246002: Lazily throw pubspec parsing exceptions. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Code review changes Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: sdk/lib/_internal/pub/lib/src/validator/pubspec_field.dart
diff --git a/sdk/lib/_internal/pub/lib/src/validator/pubspec_field.dart b/sdk/lib/_internal/pub/lib/src/validator/pubspec_field.dart
index e9d2d35452f87f98e005fc7ee28f5c5db7a14667..7ff8ec77c459ae1c04fba2ff45c1d03d44f62267 100644
--- a/sdk/lib/_internal/pub/lib/src/validator/pubspec_field.dart
+++ b/sdk/lib/_internal/pub/lib/src/validator/pubspec_field.dart
@@ -16,44 +16,91 @@ class PubspecFieldValidator extends Validator {
: super(entrypoint);
Future validate() {
- // The types of all fields are validated when the pubspec is parsed.
+ _validateAuthors();
+ _validateFieldIsString('description');
+ _validateFieldIsString('homepage');
+ _validateFieldUrl('homepage');
+ _validateFieldUrl('documentation');
+
+ // Any complex parsing errors in version will be exposed through
+ // [Pubspec.allErrors].
+ _validateFieldIsString('version');
+
+ // Pubspec errors are detected lazily, so we make sure there aren't any
+ // here.
+ for (var error in entrypoint.root.pubspec.allErrors) {
+ errors.add('In your pubspec.yaml, ${error.message}');
+ }
+
+ return new Future.value();
+ }
+
+ /// Adds an error if the "author" or "authors" field doesn't exist or has the
+ /// wrong type.
+ void _validateAuthors() {
var pubspec = entrypoint.root.pubspec;
var author = pubspec.fields['author'];
var authors = pubspec.fields['authors'];
if (author == null && authors == null) {
errors.add('Your pubspec.yaml must have an "author" or "authors" field.');
- } else {
- if (authors == null) authors = [author];
-
- var hasName = new RegExp(r"^ *[^< ]");
- var hasEmail = new RegExp(r"<[^>]+> *$");
- for (var authorName in authors) {
- if (!hasName.hasMatch(authorName)) {
- warnings.add('Author "$authorName" in pubspec.yaml should have a '
- 'name.');
- }
- if (!hasEmail.hasMatch(authorName)) {
- warnings.add('Author "$authorName" in pubspec.yaml should have an '
- 'email address\n(e.g. "name <email>").');
- }
- }
+ return;
+ }
+
+ if (author != null && author is! String) {
+ errors.add('Your pubspec.yaml\'s "author" field must be a string, but it '
+ 'was "$author".');
+ return;
}
- var homepage = pubspec.fields['homepage'];
- if (homepage == null) {
- errors.add('Your pubspec.yaml is missing a "homepage" field.');
+ if (authors != null &&
+ (authors is! List || authors.any((author) => author is! String))) {
+ errors.add('Your pubspec.yaml\'s "authors" field must be a string, but '
+ 'it was "$authors".');
+ return;
}
- var description = pubspec.fields['description'];
- if (description == null) {
- errors.add('Your pubspec.yaml is missing a "description" field.');
+ if (authors == null) authors = [author];
+
+ var hasName = new RegExp(r"^ *[^< ]");
+ var hasEmail = new RegExp(r"<[^>]+> *$");
+ for (var authorName in authors) {
+ if (!hasName.hasMatch(authorName)) {
+ warnings.add('Author "$authorName" in pubspec.yaml should have a '
+ 'name.');
+ }
+ if (!hasEmail.hasMatch(authorName)) {
+ warnings.add('Author "$authorName" in pubspec.yaml should have an '
+ 'email address\n(e.g. "name <email>").');
+ }
+ }
+ }
+
+ /// Adds an error if [field] doesn't exist or isn't a string.
+ void _validateFieldIsString(String field) {
+ var value = entrypoint.root.pubspec.fields[field];
+ if (value == null) {
+ errors.add('Your pubspec.yaml is missing a "$field" field.');
+ } else if (value is! String) {
+ errors.add('Your pubspec.yaml\'s "$field" field must be a string, but '
+ 'it was "$value".');
}
+ }
+
+ /// Adds an error if the URL for [field] is invalid.
+ void _validateFieldUrl(String field) {
+ var url = entrypoint.root.pubspec.fields[field];
+ if (url == null) return;
- var version = pubspec.fields['version'];
- if (version == null) {
- errors.add('Your pubspec.yaml is missing a "version" field.');
+ if (url is! String) {
+ errors.add('Your pubspec.yaml\'s "$field" field must be a string, but '
+ 'it was "$url".');
+ return;
}
- return new Future.value();
+ var goodScheme = new RegExp(r'^https?:');
+ if (!goodScheme.hasMatch(url)) {
+ errors.add('Your pubspec.yaml\'s "$field" field must be an "http:" or '
+ '"https:" URL, but it was "$url".');
+ }
}
}

Powered by Google App Engine
This is Rietveld 408576698