Skip to content

Commit d6e1fca

Browse files
author
Dart CI
committed
Version 3.2.0-78.0.dev
Merge eb54326 into dev
2 parents cc5eeac + eb54326 commit d6e1fca

File tree

57 files changed

+1143
-460
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1143
-460
lines changed

pkg/analysis_server/lib/src/context_manager.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -446,10 +446,13 @@ class ContextManagerImpl implements ContextManager {
446446
var content = _readFile(path);
447447
var node = loadYamlNode(content);
448448
if (node is YamlMap) {
449-
var validator = PubspecValidator(
450-
resourceProvider, resourceProvider.getFile(path).createSource());
451449
var lineInfo = LineInfo.fromContent(content);
452-
var errors = validator.validate(node.nodes);
450+
var errors = validatePubspec(
451+
contents: node.nodes,
452+
source: resourceProvider.getFile(path).createSource(),
453+
provider: resourceProvider,
454+
);
455+
453456
var converter = AnalyzerConverter();
454457
convertedErrors = converter.convertAnalysisErrors(errors,
455458
lineInfo: lineInfo, options: driver.analysisOptions);

pkg/analysis_server/lib/src/handler/legacy/edit_get_fixes.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,11 @@ error.errorCode: ${error.errorCode}
219219
if (yamlContent is! YamlMap) {
220220
yamlContent = YamlMap();
221221
}
222-
var validator =
223-
PubspecValidator(resourceProvider, pubspecFile.createSource());
224-
var errors = validator.validate(yamlContent.nodes);
222+
final errors = validatePubspec(
223+
contents: yamlContent.nodes,
224+
source: pubspecFile.createSource(),
225+
provider: resourceProvider,
226+
);
225227
for (var error in errors) {
226228
var generator =
227229
PubspecFixGenerator(resourceProvider, error, content, document);

pkg/analysis_server/lib/src/lsp/handlers/code_actions/pubspec.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ class PubspecCodeActionsProducer extends AbstractCodeActionsProducer {
5555
yamlContent = YamlMap();
5656
}
5757

58-
final validator =
59-
PubspecValidator(resourceProvider, pubspecFile.createSource());
60-
final errors = validator.validate(yamlContent.nodes);
58+
final errors = validatePubspec(
59+
contents: yamlContent.nodes,
60+
source: pubspecFile.createSource(),
61+
provider: resourceProvider,
62+
);
6163

6264
final codeActions = <CodeActionWithPriority>[];
6365
for (final error in errors) {

pkg/analysis_server/test/src/services/correction/fix/pubspec/test_support.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import 'package:analysis_server/plugin/edit/fix/fix_core.dart';
66
import 'package:analysis_server/src/protocol_server.dart' show SourceEdit;
77
import 'package:analysis_server/src/services/correction/fix/pubspec/fix_generator.dart';
88
import 'package:analyzer/error/error.dart';
9-
import 'package:analyzer/src/pubspec/pubspec_validator.dart';
9+
import 'package:analyzer/src/pubspec/pubspec_validator.dart'
10+
as pubspec_validator;
1011
import 'package:analyzer/src/test_utilities/resource_provider_mixin.dart';
1112
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
1213
import 'package:test/test.dart';
@@ -49,9 +50,11 @@ abstract class PubspecFixTest with ResourceProviderMixin {
4950
if (yamlContent is! YamlMap) {
5051
yamlContent = YamlMap();
5152
}
52-
var validator =
53-
PubspecValidator(resourceProvider, pubspecFile.createSource());
54-
var errors = validator.validate(yamlContent.nodes);
53+
final errors = pubspec_validator.validatePubspec(
54+
source: pubspecFile.createSource(),
55+
contents: yamlContent.nodes,
56+
provider: resourceProvider,
57+
);
5558
expect(errors.length, 1);
5659
error = errors[0];
5760
}

pkg/analyzer/doc/implementation/pub_diagnostics.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ The basic recipe for implementing a new pubspec diagnostic is as follows:
1818
3. Add corresponding tests to a new test library in
1919
`test/src/pubspec/diagnostics`.
2020
4. Implement analysis in a new validator in `lib/src/pubspec/validators` and
21-
invoke it from PubspecValidator (or enhance an existing one).
21+
add it to list of validators in `pubspec_validator.dart`
22+
(or enhance an existing one).
2223

2324
Once implemented, you’ll want to look for ecosystem breakages. Useful bots to
2425
watch:

pkg/analyzer/lib/src/pubspec/pubspec_validator.dart

Lines changed: 77 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,52 @@ import 'package:analyzer/src/pubspec/validators/flutter_validator.dart';
1212
import 'package:analyzer/src/pubspec/validators/name_validator.dart';
1313
import 'package:analyzer/src/pubspec/validators/platforms_validator.dart';
1414
import 'package:analyzer/src/pubspec/validators/screenshot_validator.dart';
15-
import 'package:source_span/source_span.dart';
1615
import 'package:yaml/yaml.dart';
1716

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+
);
2649

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);
3552
}
3653

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;
4555
}
4656

47-
class PubspecField {
57+
/// A function that can validate a `pubspec.yaml`.
58+
typedef PubspecValidator = void Function(PubspecValidationContext ctx);
59+
60+
final class PubspecField {
4861
/// The name of the sub-field (under `flutter`) whose value is a list of
4962
/// assets available to Flutter apps at runtime.
5063
static const String ASSETS_FIELD = 'assets';
@@ -81,48 +94,49 @@ class PubspecField {
8194
static const String VERSION_FIELD = 'version';
8295
}
8396

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;
87101

88102
/// The source representing the file being validated.
89103
final Source source;
90104

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;
118107

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+
}
125127

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+
);
127141
}
128142
}

0 commit comments

Comments
 (0)