Skip to content
1 change: 1 addition & 0 deletions pkgs/yaml_edit/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## 2.2.5-wip
- Fix bug where appending to a nested block list before an outdented sibling threw an exception ([#2481](https://github.com/dart-lang/tools/issues/2481)).
Comment thread
slashblog marked this conversation as resolved.
Outdated

## 2.2.4

Expand Down
4 changes: 2 additions & 2 deletions pkgs/yaml_edit/lib/src/list_mutations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ SourceEdit _appendToBlockList(
// Adjusts offset to after the trailing newline of the last entry, if it
// exists
if (list.isNotEmpty) {
final lastValueSpanEnd = list.nodes.last.span.end.offset;
final nextNewLineIndex = yaml.indexOf('\n', lastValueSpanEnd - 1);
final lastValueSpanEnd = getContentSensitiveEnd(list.nodes.last);
final nextNewLineIndex = yaml.indexOf('\n', lastValueSpanEnd);
if (nextNewLineIndex == -1) {
formattedValue = getLineEnding(yaml) + formattedValue;
} else {
Expand Down
29 changes: 29 additions & 0 deletions pkgs/yaml_edit/test/append_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,35 @@ b:
'''));
});

test('block append nested to list', () {
final yamlEditor = YamlEditor('''
family:
- title: Parent
children:
- title: kid1
- title: kid2
- title: Uncle
''');

expect(
() => yamlEditor
.appendToList(['family', 0, 'children'], {'title': 'kid3'}),
returnsNormally);
expectYamlBuilderValue(yamlEditor, {
'family': [
{
'title': 'Parent',
'children': [
{'title': 'kid1'},
{'title': 'kid2'},
{'title': 'kid3'},
],
},
{'title': 'Uncle'},
]
});
});

test('block append nested and with comments', () {
final yamlEditor = YamlEditor('''
a:
Expand Down
Loading