Skip to content

Commit

Permalink
Consume until end of document if there is no newline when removing fr…
Browse files Browse the repository at this point in the history
…om (#76)

block map.

Fixes #55.
  • Loading branch information
jonasfj authored May 6, 2024
1 parent fe2699d commit d15b44c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/src/map_mutations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ SourceEdit _removeFromBlockMap(
// because there is no value (e.g. `key: \n`). Because [valueNode.span] in
// such cases point to the colon `:`.
end = nextNewLine;
} else {
// Remove everything until the end of the document, if there is no newline
end = yaml.length;
}
return SourceEdit(start, end - start, '{}');
}
Expand All @@ -201,6 +204,9 @@ SourceEdit _removeFromBlockMap(
final nextNewLine = yaml.indexOf(lineEnding, end);
if (nextNewLine != -1) {
end = nextNewLine + lineEnding.length;
} else {
// Remove everything until the end of the document, if there is no newline
end = yaml.length;
}

final nextNode = getNextKeyNode(map, keyNode);
Expand Down
24 changes: 24 additions & 0 deletions test/remove_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,30 @@ b:
c: 3
'''));
});

test('issue #55 reopend', () {
final doc = YamlEditor('''name: sample
version: 0.1.0
environment:
sdk: ^3.0.0
dependencies:
retry: ^3.1.2
dev_dependencies:
retry:''');
doc.remove(['dev_dependencies']);
});

test('issue #55 reopend, variant 2', () {
final doc = YamlEditor('''name: sample
version: 0.1.0
environment:
sdk: ^3.0.0
dependencies:
retry: ^3.1.2
dev_dependencies:
retry:''');
doc.remove(['dev_dependencies', 'retry']);
});
});

group('flow map', () {
Expand Down

0 comments on commit d15b44c

Please sign in to comment.