Skip to content

Commit e81707f

Browse files
authored
Consume until end of document if there is no newline when removing from (dart-archive/yaml_edit#76)
block map. Fixes dart-lang/yaml_edit#55.
1 parent 43110ad commit e81707f

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

pkgs/yaml_edit/lib/src/map_mutations.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ SourceEdit _removeFromBlockMap(
188188
// because there is no value (e.g. `key: \n`). Because [valueNode.span] in
189189
// such cases point to the colon `:`.
190190
end = nextNewLine;
191+
} else {
192+
// Remove everything until the end of the document, if there is no newline
193+
end = yaml.length;
191194
}
192195
return SourceEdit(start, end - start, '{}');
193196
}
@@ -201,6 +204,9 @@ SourceEdit _removeFromBlockMap(
201204
final nextNewLine = yaml.indexOf(lineEnding, end);
202205
if (nextNewLine != -1) {
203206
end = nextNewLine + lineEnding.length;
207+
} else {
208+
// Remove everything until the end of the document, if there is no newline
209+
end = yaml.length;
204210
}
205211

206212
final nextNode = getNextKeyNode(map, keyNode);

pkgs/yaml_edit/test/remove_test.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,30 @@ b:
217217
c: 3
218218
'''));
219219
});
220+
221+
test('issue #55 reopend', () {
222+
final doc = YamlEditor('''name: sample
223+
version: 0.1.0
224+
environment:
225+
sdk: ^3.0.0
226+
dependencies:
227+
retry: ^3.1.2
228+
dev_dependencies:
229+
retry:''');
230+
doc.remove(['dev_dependencies']);
231+
});
232+
233+
test('issue #55 reopend, variant 2', () {
234+
final doc = YamlEditor('''name: sample
235+
version: 0.1.0
236+
environment:
237+
sdk: ^3.0.0
238+
dependencies:
239+
retry: ^3.1.2
240+
dev_dependencies:
241+
retry:''');
242+
doc.remove(['dev_dependencies', 'retry']);
243+
});
220244
});
221245

222246
group('flow map', () {

0 commit comments

Comments
 (0)