From b4a078aafd7d85d2b59e982e2c5001ffd893f331 Mon Sep 17 00:00:00 2001 From: Jonas Finnemann Jensen Date: Mon, 6 May 2024 13:57:01 +0200 Subject: [PATCH] Consume until end of document if there is no newline when removing from block map. Fixes #55. --- lib/src/map_mutations.dart | 6 ++++++ test/remove_test.dart | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/lib/src/map_mutations.dart b/lib/src/map_mutations.dart index 07a2c54..32f47b3 100644 --- a/lib/src/map_mutations.dart +++ b/lib/src/map_mutations.dart @@ -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, '{}'); } @@ -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); diff --git a/test/remove_test.dart b/test/remove_test.dart index cdb195c..4742b56 100644 --- a/test/remove_test.dart +++ b/test/remove_test.dart @@ -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', () {