diff --git a/pkgs/yaml/CHANGELOG.md b/pkgs/yaml/CHANGELOG.md index 628826071b..66db5994dc 100644 --- a/pkgs/yaml/CHANGELOG.md +++ b/pkgs/yaml/CHANGELOG.md @@ -1,3 +1,9 @@ +## 3.2.0-wip + +* Export `ErrorListener` and `ErrorCollector` from `package:yaml/yaml.dart`. +* Mark internal constructors in `YamlDocument`, `YamlMap`, `YamlList`, and + `YamlScalar` with `@internal`. + ## 3.1.4 * Improve recovery for list entries without `-` prefix. When recovering, provide diff --git a/pkgs/yaml/lib/src/yaml_document.dart b/pkgs/yaml/lib/src/yaml_document.dart index da6aa1ec34..d0474d0cf7 100644 --- a/pkgs/yaml/lib/src/yaml_document.dart +++ b/pkgs/yaml/lib/src/yaml_document.dart @@ -7,6 +7,7 @@ import 'dart:collection'; +import 'package:meta/meta.dart'; import 'package:source_span/source_span.dart'; import 'yaml_node.dart'; @@ -33,6 +34,9 @@ class YamlDocument { final bool endImplicit; /// Users of the library should not use this constructor. + /// + /// @nodoc + @internal YamlDocument.internal(this.contents, this.span, this.versionDirective, List tagDirectives, {this.startImplicit = false, this.endImplicit = false}) diff --git a/pkgs/yaml/lib/src/yaml_node.dart b/pkgs/yaml/lib/src/yaml_node.dart index bd17b6cb99..dce94c28e9 100644 --- a/pkgs/yaml/lib/src/yaml_node.dart +++ b/pkgs/yaml/lib/src/yaml_node.dart @@ -8,6 +8,7 @@ import 'dart:collection' as collection; import 'package:collection/collection.dart'; +import 'package:meta/meta.dart'; import 'package:source_span/source_span.dart'; import 'event.dart'; @@ -86,6 +87,7 @@ class YamlMap extends YamlNode with collection.MapMixin, UnmodifiableMapMixin { YamlMapWrapper(dartMap, sourceUrl, style: style); /// Users of the library should not use this constructor. + @internal YamlMap.internal(Map nodes, super.span, this.style) : nodes = UnmodifiableMapView(nodes), super._(); @@ -136,6 +138,7 @@ class YamlList extends YamlNode with collection.ListMixin { YamlListWrapper(dartList, sourceUrl, style: style); /// Users of the library should not use this constructor. + @internal YamlList.internal(List nodes, super.span, this.style) : nodes = UnmodifiableListView(nodes), super._(); @@ -170,11 +173,13 @@ class YamlScalar extends YamlNode { } /// Users of the library should not use this constructor. + @internal YamlScalar.internal(this.value, ScalarEvent scalar) : style = scalar.style, super._(scalar.span); /// Users of the library should not use this constructor. + @internal YamlScalar.internalWithSpan(this.value, SourceSpan span) : style = ScalarStyle.ANY, super._(span); diff --git a/pkgs/yaml/lib/yaml.dart b/pkgs/yaml/lib/yaml.dart index 26cc9b86f5..dc1f14ad55 100644 --- a/pkgs/yaml/lib/yaml.dart +++ b/pkgs/yaml/lib/yaml.dart @@ -12,6 +12,7 @@ import 'src/yaml_document.dart'; import 'src/yaml_exception.dart'; import 'src/yaml_node.dart'; +export 'src/error_listener.dart'; export 'src/style.dart'; export 'src/utils.dart' show YamlWarningCallback, yamlWarningCallback; export 'src/yaml_document.dart'; diff --git a/pkgs/yaml/pubspec.yaml b/pkgs/yaml/pubspec.yaml index c832c8e017..992a8e78cb 100644 --- a/pkgs/yaml/pubspec.yaml +++ b/pkgs/yaml/pubspec.yaml @@ -1,5 +1,5 @@ name: yaml -version: 3.1.4 +version: 3.2.0-wip description: A parser for YAML, a human-friendly data serialization standard repository: https://github.com/dart-lang/tools/tree/main/pkgs/yaml issue_tracker: https://github.com/dart-lang/tools/labels/package%3Ayaml @@ -13,6 +13,7 @@ environment: dependencies: collection: ^1.15.0 + meta: ^1.9.0 source_span: ^1.8.0 string_scanner: ^1.2.0 diff --git a/pkgs/yaml/test/error_listener_test.dart b/pkgs/yaml/test/error_listener_test.dart new file mode 100644 index 0000000000..19c21dab5d --- /dev/null +++ b/pkgs/yaml/test/error_listener_test.dart @@ -0,0 +1,72 @@ +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:test/test.dart'; +import 'package:yaml/yaml.dart'; + +class _CustomErrorListener implements ErrorListener { + final List messages = []; + + @override + void onError(YamlException error) { + messages.add(error.message); + } +} + +void main() { + const invalidYamlWithMissingPrefix = ''' +linter: + rules: + - annotate_overrides + alway +'''; + + test('ErrorCollector collects errors during recovery parsing', () { + final collector = ErrorCollector(); + final result = loadYaml( + invalidYamlWithMissingPrefix, + recover: true, + errorListener: collector, + ); + + expect( + result, + equals({ + 'linter': { + 'rules': ['annotate_overrides', 'alway'], + }, + }), + ); + expect(collector.errors, hasLength(1)); + expect( + collector.errors.first.message, + contains("Expected ':'"), + ); + }); + + test( + 'custom ErrorListener implementation receives errors during recovery ' + 'parsing', () { + final listener = _CustomErrorListener(); + final result = loadYaml( + invalidYamlWithMissingPrefix, + recover: true, + errorListener: listener, + ); + + expect( + result, + equals({ + 'linter': { + 'rules': ['annotate_overrides', 'alway'], + }, + }), + ); + expect(listener.messages, hasLength(1)); + expect( + listener.messages.first, + contains("Expected ':'"), + ); + }); +} diff --git a/pkgs/yaml/test/yaml_test.dart b/pkgs/yaml/test/yaml_test.dart index 72f985bef6..e2b5264895 100644 --- a/pkgs/yaml/test/yaml_test.dart +++ b/pkgs/yaml/test/yaml_test.dart @@ -8,7 +8,6 @@ // ignore_for_file: avoid_dynamic_calls import 'package:test/test.dart'; -import 'package:yaml/src/error_listener.dart'; import 'package:yaml/yaml.dart'; import 'utils.dart';