From cb3cf763349f1f7997a4fc7b2b402062b2a8f915 Mon Sep 17 00:00:00 2001 From: Kevin Moore Date: Tue, 9 Apr 2019 14:26:22 -0700 Subject: [PATCH] Improve the YamlScalar.span values with trailing whitespace Fixes https://github.com/dart-lang/yaml/issues/51 Also remove dead line from changelog --- pkgs/yaml/CHANGELOG.md | 9 ++++++--- pkgs/yaml/lib/src/parser.dart | 17 ++++++++++++++++- pkgs/yaml/pubspec.yaml | 2 +- pkgs/yaml/test/span_test.dart | 21 ++++++--------------- 4 files changed, 29 insertions(+), 20 deletions(-) diff --git a/pkgs/yaml/CHANGELOG.md b/pkgs/yaml/CHANGELOG.md index 143e1c2d0..c0b306308 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 bffaa174e..0971e7f47 100644 --- a/pkgs/yaml/lib/src/parser.dart +++ b/pkgs/yaml/lib/src/parser.dart @@ -303,7 +303,22 @@ class Parser { _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 da41f3a45..d0fb672d8 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 diff --git a/pkgs/yaml/test/span_test.dart b/pkgs/yaml/test/span_test.dart index 7ef61d306..09e461cab 100644 --- a/pkgs/yaml/test/span_test.dart +++ b/pkgs/yaml/test/span_test.dart @@ -8,13 +8,6 @@ import 'package:source_span/source_span.dart'; 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 @@ void main() { _expectSpan( yaml.nodes['num'].span, r''' -line 2, column 9: message ╷ 2 │ "num": 42, │ ^^ @@ -45,7 +37,6 @@ line 2, column 9: message _expectSpan( yaml.nodes['null'].span, r''' -line 7, column 10: message ╷ 7 │ "null": null │ ^^^^ @@ -64,7 +55,6 @@ line 7, column 10: message _expectSpan( nestedMap.nodes['null'].span, r''' -line 4, column 11: message ╷ 4 │ "null": null, │ ^^^^ @@ -76,14 +66,15 @@ line 4, column 11: message _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); +}