@@ -12,39 +12,52 @@ import 'package:analyzer/src/pubspec/validators/flutter_validator.dart';
12
12
import 'package:analyzer/src/pubspec/validators/name_validator.dart' ;
13
13
import 'package:analyzer/src/pubspec/validators/platforms_validator.dart' ;
14
14
import 'package:analyzer/src/pubspec/validators/screenshot_validator.dart' ;
15
- import 'package:source_span/source_span.dart' ;
16
15
import 'package:yaml/yaml.dart' ;
17
16
18
- class BasePubspecValidator {
19
- /// The resource provider used to access the file system.
20
- final ResourceProvider provider;
21
-
22
- /// The source representing the file being validated.
23
- final Source source;
24
-
25
- BasePubspecValidator (this .provider, this .source);
17
+ /// List of [PubspecValidator] implementations.
18
+ const _pubspecValidators = < PubspecValidator > [
19
+ dependencyValidator,
20
+ fieldValidator,
21
+ flutterValidator,
22
+ nameValidator,
23
+ screenshotsValidator,
24
+ platformsValidator,
25
+ ];
26
+
27
+ /// Validate pubspec with given [contents] .
28
+ ///
29
+ /// The [source] argument must be the source of the file being validated.
30
+ /// The [provider] argument must provide access to the file-system.
31
+ List <AnalysisError > validatePubspec ({
32
+ // TODO(brianwilkerson) This method needs to take a `YamlDocument` rather
33
+ // than the contents of the document so that it can validate an empty file.
34
+ required Map <dynamic , YamlNode > contents,
35
+ required Source source,
36
+ required ResourceProvider provider,
37
+ }) {
38
+ final recorder = RecordingErrorListener ();
39
+ final ctx = PubspecValidationContext ._(
40
+ contents: contents,
41
+ source: source,
42
+ reporter: ErrorReporter (
43
+ recorder,
44
+ source,
45
+ isNonNullableByDefault: false ,
46
+ ),
47
+ provider: provider,
48
+ );
26
49
27
- String ? asString (dynamic node) {
28
- if (node is String ) {
29
- return node;
30
- }
31
- if (node is YamlScalar && node.value is String ) {
32
- return node.value as String ;
33
- }
34
- return null ;
50
+ for (final validator in _pubspecValidators) {
51
+ validator (ctx);
35
52
}
36
53
37
- /// Report an error for the given node.
38
- void reportErrorForNode (
39
- ErrorReporter reporter, YamlNode node, ErrorCode errorCode,
40
- [List <Object >? arguments]) {
41
- SourceSpan span = node.span;
42
- reporter.reportErrorForOffset (
43
- errorCode, span.start.offset, span.length, arguments);
44
- }
54
+ return recorder.errors;
45
55
}
46
56
47
- class PubspecField {
57
+ /// A function that can validate a `pubspec.yaml` .
58
+ typedef PubspecValidator = void Function (PubspecValidationContext ctx);
59
+
60
+ final class PubspecField {
48
61
/// The name of the sub-field (under `flutter` ) whose value is a list of
49
62
/// assets available to Flutter apps at runtime.
50
63
static const String ASSETS_FIELD = 'assets' ;
@@ -81,48 +94,49 @@ class PubspecField {
81
94
static const String VERSION_FIELD = 'version' ;
82
95
}
83
96
84
- class PubspecValidator {
85
- /// The resource provider used to access the file system.
86
- final ResourceProvider provider;
97
+ /// Context given to function that implement [PubspecValidator] .
98
+ final class PubspecValidationContext {
99
+ /// Yaml document being validated
100
+ final Map <dynamic , YamlNode > contents;
87
101
88
102
/// The source representing the file being validated.
89
103
final Source source;
90
104
91
- final DependencyValidator _dependencyValidator;
92
- final FieldValidator _fieldValidator;
93
- final FlutterValidator _flutterValidator;
94
- final NameValidator _nameValidator;
95
- final ScreenshotsValidator _screenshotsValidator;
96
- final PlatformsValidator _platformsValidator;
97
-
98
- /// Initialize a newly create validator to validate the content of the given
99
- /// [source] .
100
- PubspecValidator (this .provider, this .source)
101
- : _dependencyValidator = DependencyValidator (provider, source),
102
- _fieldValidator = FieldValidator (provider, source),
103
- _flutterValidator = FlutterValidator (provider, source),
104
- _nameValidator = NameValidator (provider, source),
105
- _screenshotsValidator = ScreenshotsValidator (provider, source),
106
- _platformsValidator = PlatformsValidator (provider, source);
107
-
108
- /// Validate the given [contents] .
109
- List <AnalysisError > validate (Map <dynamic , YamlNode > contents) {
110
- // TODO(brianwilkerson) This method needs to take a `YamlDocument` rather
111
- // than the contents of the document so that it can validate an empty file.
112
- RecordingErrorListener recorder = RecordingErrorListener ();
113
- ErrorReporter reporter = ErrorReporter (
114
- recorder,
115
- source,
116
- isNonNullableByDefault: false ,
117
- );
105
+ /// The reporter to which errors should be reported.
106
+ final ErrorReporter reporter;
118
107
119
- _dependencyValidator.validate (reporter, contents);
120
- _fieldValidator.validate (reporter, contents);
121
- _flutterValidator.validate (reporter, contents);
122
- _nameValidator.validate (reporter, contents);
123
- _screenshotsValidator.validate (reporter, contents);
124
- _platformsValidator.validate (reporter, contents);
108
+ /// The resource provider used to access the file system.
109
+ final ResourceProvider provider;
110
+
111
+ PubspecValidationContext ._({
112
+ required this .contents,
113
+ required this .source,
114
+ required this .reporter,
115
+ required this .provider,
116
+ });
117
+
118
+ String ? asString (dynamic node) {
119
+ if (node is String ) {
120
+ return node;
121
+ }
122
+ if (node is YamlScalar && node.value is String ) {
123
+ return node.value as String ;
124
+ }
125
+ return null ;
126
+ }
125
127
126
- return recorder.errors;
128
+ /// Report an error for the given node.
129
+ void reportErrorForNode (
130
+ YamlNode node,
131
+ ErrorCode errorCode, [
132
+ List <Object >? arguments,
133
+ ]) {
134
+ final span = node.span;
135
+ reporter.reportErrorForOffset (
136
+ errorCode,
137
+ span.start.offset,
138
+ span.length,
139
+ arguments,
140
+ );
127
141
}
128
142
}
0 commit comments