Skip to content

Commit

Permalink
Improve the YamlScalar.span values with trailing whitespace
Browse files Browse the repository at this point in the history
Fixes https://github.com/dart-lang/yaml/issues/51

Also remove dead line from changelog
  • Loading branch information
kevmoo committed Apr 9, 2019
1 parent 9081110 commit cb3cf76
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
9 changes: 6 additions & 3 deletions pkgs/yaml/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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.
Expand Down
17 changes: 16 additions & 1 deletion pkgs/yaml/lib/src/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion pkgs/yaml/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: yaml
version: 2.1.16-dev
version: 2.1.16

description: A parser for YAML.
author: Dart Team <[email protected]>
Expand Down
21 changes: 6 additions & 15 deletions pkgs/yaml/test/span_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -33,7 +26,6 @@ void main() {
_expectSpan(
yaml.nodes['num'].span,
r'''
line 2, column 9: message
2 │ "num": 42,
│ ^^
Expand All @@ -45,7 +37,6 @@ line 2, column 9: message
_expectSpan(
yaml.nodes['null'].span,
r'''
line 7, column 10: message
7 │ "null": null
│ ^^^^
Expand All @@ -64,7 +55,6 @@ line 7, column 10: message
_expectSpan(
nestedMap.nodes['null'].span,
r'''
line 4, column 11: message
4 │ "null": null,
│ ^^^^
Expand All @@ -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);
}

0 comments on commit cb3cf76

Please sign in to comment.