Improve the YamlScalar.span values with trailing whitespace Fixes https://github.com/dart-lang/yaml/issues/51 Also remove dead line from changelog
diff --git a/pkgs/yaml/CHANGELOG.md b/pkgs/yaml/CHANGELOG.md index 143e1c2..c0b3063 100644 --- a/pkgs/yaml/CHANGELOG.md +++ b/pkgs/yaml/CHANGELOG.md
@@ -1,3 +1,8 @@ +## 2.1.16 + +* Improve the `span` associated with `YamlScalar` values with trailing + whitespace. + ## 2.1.15 * Set max SDK version to `<3.0.0`, and adjust other dependencies. @@ -110,12 +115,10 @@ constructors make it possible to use the same API to access non-YAML data as YAML data. -* Make `YamlException` inherit from source_map's [`SpanFormatException`][]. This +* Make `YamlException` inherit from source_map's `SpanFormatException`. This improves the error formatting and allows callers access to source range information. -[SpanFormatException]: (http://www.dartdocs.org/documentation/source_maps/0.9.2/index.html#source_maps/source_maps.SpanFormatException) - ## 1.0.0+1 * Fix a variable name typo.
diff --git a/pkgs/yaml/lib/src/parser.dart b/pkgs/yaml/lib/src/parser.dart index bffaa17..0971e7f 100644 --- a/pkgs/yaml/lib/src/parser.dart +++ b/pkgs/yaml/lib/src/parser.dart
@@ -303,7 +303,22 @@ _state = _states.removeLast(); _scanner.scan(); - return ScalarEvent(span.expand(token.span), token.value, token.style, + + span = span.expand(token.span); + if (span.text != token.value) { + // If the only difference between the span and the token is trailing + // whitespace + if (span.text.trimRight() == token.value) { + span = span.file.span( + span.start.offset, + // TODO(kevmoo): The length of `token.value` may be incorrect + // with some UNICODE values. Find a more clean solution. + span.start.offset + token.value.length, + ); + } + } + + return ScalarEvent(span, token.value, token.style, anchor: anchor, tag: tag); }
diff --git a/pkgs/yaml/pubspec.yaml b/pkgs/yaml/pubspec.yaml index da41f3a..d0fb672 100644 --- a/pkgs/yaml/pubspec.yaml +++ b/pkgs/yaml/pubspec.yaml
@@ -1,5 +1,5 @@ name: yaml -version: 2.1.16-dev +version: 2.1.16 description: A parser for YAML. author: Dart Team <misc@dartlang.org>
diff --git a/pkgs/yaml/test/span_test.dart b/pkgs/yaml/test/span_test.dart index 7ef61d3..09e461c 100644 --- a/pkgs/yaml/test/span_test.dart +++ b/pkgs/yaml/test/span_test.dart
@@ -8,13 +8,6 @@ import 'package:test/test.dart'; import 'package:yaml/yaml.dart'; -void _expectSpan(SourceSpan source, String expected) { - final result = source.message('message'); - printOnFailure("r'''\n$result'''"); - - expect(result, expected); -} - void main() { YamlMap yaml; @@ -33,7 +26,6 @@ _expectSpan( yaml.nodes['num'].span, r''' -line 2, column 9: message ╷ 2 │ "num": 42, │ ^^ @@ -45,7 +37,6 @@ _expectSpan( yaml.nodes['null'].span, r''' -line 7, column 10: message ╷ 7 │ "null": null │ ^^^^ @@ -64,7 +55,6 @@ _expectSpan( nestedMap.nodes['null'].span, r''' -line 4, column 11: message ╷ 4 │ "null": null, │ ^^^^ @@ -76,14 +66,15 @@ _expectSpan( nestedMap.nodes['num'].span, r''' -line 5, column 10: message ╷ -5 │ "num": 42 - │ ┌──────────^ -6 │ │ }, - │ └─^ +5 │ "num": 42 + │ ^^ ╵''', ); }); }); } + +void _expectSpan(SourceSpan source, String expected) { + expect(source.highlight(), expected); +}