Skip to content

Commit

Permalink
Fix YamlEditor.update method leaving trailing spaces at eol (dart-lan…
Browse files Browse the repository at this point in the history
…g/yaml_edit#42)

* Fix for selecting the correct index in insertion

* Add new test cases

* Add more tests and format corrections

* Update `CHANGELOG.md`

* Fix leaving whitespace at eol on YamlEditor.update & update the testcases

* Fix formatting

* Apply proposed change

* Update lib/src/map_mutations.dart

Co-authored-by: Jonas Finnemann Jensen <jopsen@gmail.com>

---------

Co-authored-by: Jonas Finnemann Jensen <jopsen@gmail.com>
  • Loading branch information
Ishad-M-I-M and jonasfj authored Mar 9, 2023
1 parent 3930dad commit d2d82cd
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 40 deletions.
7 changes: 6 additions & 1 deletion pkgs/yaml_edit/lib/src/map_mutations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ SourceEdit _replaceInBlockMap(
valueAsString = lineEnding + valueAsString;
}

if (!valueAsString.startsWith(lineEnding)) {
// prepend whitespace to ensure there is space after colon.
valueAsString = ' ' + valueAsString;
}

/// +1 accounts for the colon
final start = keyNode.span.end.offset + 1;
var end = getContentSensitiveEnd(map.nodes[key]!);
Expand All @@ -148,7 +153,7 @@ SourceEdit _replaceInBlockMap(
/// this.
if (end < start) end = start;

return SourceEdit(start, end - start, ' ' + valueAsString);
return SourceEdit(start, end - start, valueAsString);
}

/// Performs the string operation on [yaml] to achieve the effect of replacing
Expand Down
4 changes: 2 additions & 2 deletions pkgs/yaml_edit/test/alias_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ b: *SS

test('removing nested map alias anchor results in AliasError', () {
final doc = YamlEditor('''
a:
a:
c: &SS Sammy Sosa
b: *SS
''');
Expand All @@ -129,7 +129,7 @@ b: *SS
test('removing nested map alias reference results in AliasError', () {
final doc = YamlEditor('''
a: &SS Sammy Sosa
b:
b:
c: *SS
''');

Expand Down
6 changes: 3 additions & 3 deletions pkgs/yaml_edit/test/preservation_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ void main() {
test('tilde', expectLoadPreservesYAML('~'));
test('false', expectLoadPreservesYAML('false'));

test('block map', expectLoadPreservesYAML('''a:
test('block map', expectLoadPreservesYAML('''a:
b: 1
'''));
test('block list', expectLoadPreservesYAML('''a:
test('block list', expectLoadPreservesYAML('''a:
- 1
'''));
test('complicated example', () {
expectLoadPreservesYAML('''verb: RecommendCafes
map:
a:
a:
b: 1
recipe:
- verb: Score
Expand Down
2 changes: 1 addition & 1 deletion pkgs/yaml_edit/test/testdata/input/nested_block_map.test
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
NESTED BLOCK MAP
---
a: 1
b:
b:
d: 4
e: 5
c: 3
Expand Down
12 changes: 6 additions & 6 deletions pkgs/yaml_edit/test/testdata/output/nested_block_map.golden
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
a: 1
b:
b:
d: 4
e: 5
c: 3
---
a: 1
b:
b:
d: 4
e: 6
c: 3
---
a: 1
b:
b:
d: 4
e:
e:
- 1
- 2
- 3
c: 3
---
a: 1
b:
b:
d: 4
e:
e:
- 1
- 2
- 3
Expand Down
2 changes: 1 addition & 1 deletion pkgs/yaml_edit/test/testdata/output/pubspec.golden
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,6 @@ dependencies:
retry: ^3.0.1
yaml: ^3.2.0 # For YAML

dev_dependencies:
dev_dependencies:
test: ^1.14.4

40 changes: 20 additions & 20 deletions pkgs/yaml_edit/test/update_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void main() {
test('nested', () {
final doc = YamlEditor('''
a: 1
b:
b:
d: 4
e: 5
c: 3
Expand All @@ -135,7 +135,7 @@ c: 3

expect(doc.toString(), equals('''
a: 1
b:
b:
d: 4
e: 6
c: 3
Expand Down Expand Up @@ -201,14 +201,14 @@ a: 1

test('nested (5)', () {
final doc = YamlEditor('''
a:
a:
- a: 1
b: 2
- null
''');
doc.update(['a', 0], false);
expect(doc.toString(), equals('''
a:
a:
- false
- null
Expand All @@ -220,14 +220,14 @@ a:

test('nested (6)', () {
final doc = YamlEditor('''
a:
a:
- - 1
- 2
- null
''');
doc.update(['a', 0], false);
expect(doc.toString(), equals('''
a:
a:
- false
- null
Expand Down Expand Up @@ -261,7 +261,7 @@ b: false
doc.update(['a'], {'retry': '3.0.1'});

expect(doc.toString(), equals('''
a:
a:
retry: 3.0.1
b: false
'''));
Expand All @@ -277,7 +277,7 @@ a: # comment

expect(doc.toString(), equals('''
# comment
a:
a:
retry: 3.0.1 # comment
# comment
'''));
Expand All @@ -286,7 +286,7 @@ a:
test('nested scalar -> flow list', () {
final doc = YamlEditor('''
a: 1
b:
b:
d: 4
e: 5
c: 3
Expand All @@ -295,9 +295,9 @@ c: 3

expect(doc.toString(), equals('''
a: 1
b:
b:
d: 4
e:
e:
- 1
- 2
- 3
Expand All @@ -316,7 +316,7 @@ c: 3
test('nested block map -> scalar', () {
final doc = YamlEditor('''
a: 1
b:
b:
d: 4
e: 5
c: 3
Expand All @@ -334,7 +334,7 @@ c: 3
test('nested block map -> scalar with comments', () {
final doc = YamlEditor('''
a: 1
b:
b:
d: 4
e: 5
Expand All @@ -359,7 +359,7 @@ b: 2
test('nested scalar -> block map', () {
final doc = YamlEditor('''
a: 1
b:
b:
d: 4
e: 5
c: 3
Expand All @@ -368,9 +368,9 @@ c: 3

expect(doc.toString(), equals('''
a: 1
b:
b:
d: 4
e:
e:
x: 3
y: 4
c: 3
Expand All @@ -388,7 +388,7 @@ c: 3
test('nested block map with comments', () {
final doc = YamlEditor('''
a: 1
b:
b:
d: 4
e: 5 # comment
c: 3
Expand All @@ -397,7 +397,7 @@ c: 3

expect(doc.toString(), equals('''
a: 1
b:
b:
d: 4
e: 6 # comment
c: 3
Expand All @@ -412,7 +412,7 @@ c: 3
test('nested block map with comments (2)', () {
final doc = YamlEditor('''
a: 1
b:
b:
d: 4 # comment
# comment
e: 5 # comment
Expand All @@ -423,7 +423,7 @@ c: 3

expect(doc.toString(), equals('''
a: 1
b:
b:
d: 4 # comment
# comment
e: 6 # comment
Expand Down
6 changes: 3 additions & 3 deletions pkgs/yaml_edit/test/utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ c:
}));

expect(doc.toString(), equals('''
strings:
strings:
plain: string
folded: >-
string
Expand Down Expand Up @@ -366,7 +366,7 @@ a:
});

expect(doc.toString(), equals('''
a:
a:
f: ""
g: 1
'''));
Expand All @@ -385,7 +385,7 @@ a:
});

expect(doc.toString(), equals('''
a:
a:
f: " a"
g: 1
'''));
Expand Down
6 changes: 3 additions & 3 deletions pkgs/yaml_edit/test/windows_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ c: 3\r
test('update nested scalar -> flow list', () {
final doc = YamlEditor('''
a: 1\r
b: \r
b:\r
d: 4\r
e: 5\r
c: 3\r
Expand All @@ -71,9 +71,9 @@ c: 3\r

expect(doc.toString(), equals('''
a: 1\r
b: \r
b:\r
d: 4\r
e: \r
e:\r
- 1\r
- 2\r
- 3\r
Expand Down

0 comments on commit d2d82cd

Please sign in to comment.