Skip to content

Commit 43110ad

Browse files
authored
Fix removal of last key from map in block-mode when value is empty. Fixes dart-lang/yaml_edit#55. When the value is empty the `SourceSpan` for the `YamlNode` representing the value in a map points to the colon. Example: ```yaml foo: bar: ``` The `YamlNode` for `foo.bar` has a value of `null` and starts and ends at the colon `:` following `bar`. This means that removal might leave the colon behind, which causes invalid YAML. We have the same issue when removing `foo.bar` from the following YAML document: ```yaml foo: baz: true bar: ``` However, in this case, we have a hack that ensures we always strip away the any comments that follows `bar`. We do this by deleting up-to the next newline. If we apply the same hack when removing `foo.bar` in the first example, then it works. One could argue that it works by accident, but it's kind of desired that trailing comments are removed, when the value they are trailing is removed.
1 parent 5cf17f7 commit 43110ad

15 files changed

+114
-6
lines changed

pkgs/yaml_edit/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
## 2.2.1-wip
1+
## 2.2.1
22

33
- Require Dart 3.0
4+
- Fix removal of last key in blockmap when key has no value
5+
([#55](https://github.com/dart-lang/yaml_edit/issues/55)).
46

57
## 2.2.0
68

pkgs/yaml_edit/lib/src/map_mutations.dart

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ SourceEdit _replaceInBlockMap(
145145
}
146146

147147
/// +1 accounts for the colon
148+
// TODO: What if here is a whitespace following the key, before the colon?
148149
final start = keyNode.span.end.offset + 1;
149150
var end = getContentSensitiveEnd(map.nodes[key]!);
150151

@@ -175,9 +176,19 @@ SourceEdit _removeFromBlockMap(
175176
final keySpan = keyNode.span;
176177
var end = getContentSensitiveEnd(valueNode);
177178
final yaml = yamlEdit.toString();
179+
final lineEnding = getLineEnding(yaml);
178180

179181
if (map.length == 1) {
180182
final start = map.span.start.offset;
183+
final nextNewLine = yaml.indexOf(lineEnding, end);
184+
if (nextNewLine != -1) {
185+
// Remove everything up to the next newline, this strips comments that
186+
// follows on the same line as the value we're removing.
187+
// It also ensures we consume colon when [valueNode.value] is `null`
188+
// because there is no value (e.g. `key: \n`). Because [valueNode.span] in
189+
// such cases point to the colon `:`.
190+
end = nextNewLine;
191+
}
181192
return SourceEdit(start, end - start, '{}');
182193
}
183194

@@ -187,16 +198,16 @@ SourceEdit _removeFromBlockMap(
187198
///
188199
/// We do this because we suspect that our users will want the inline
189200
/// comments to disappear too.
190-
final nextNewLine = yaml.indexOf('\n', end);
201+
final nextNewLine = yaml.indexOf(lineEnding, end);
191202
if (nextNewLine != -1) {
192-
end = nextNewLine + 1;
203+
end = nextNewLine + lineEnding.length;
193204
}
194205

195206
final nextNode = getNextKeyNode(map, keyNode);
196207

197208
if (start > 0) {
198209
final lastHyphen = yaml.lastIndexOf('-', start - 1);
199-
final lastNewLine = yaml.lastIndexOf('\n', start - 1);
210+
final lastNewLine = yaml.lastIndexOf(lineEnding, start - 1);
200211
if (lastHyphen > lastNewLine) {
201212
start = lastHyphen + 2;
202213

@@ -208,7 +219,7 @@ SourceEdit _removeFromBlockMap(
208219
end += nextNode.span.start.column;
209220
}
210221
} else if (lastNewLine > lastHyphen) {
211-
start = lastNewLine + 1;
222+
start = lastNewLine + lineEnding.length;
212223
}
213224
}
214225

pkgs/yaml_edit/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: yaml_edit
2-
version: 2.2.1-wip
2+
version: 2.2.1
33
description: >-
44
A library for YAML manipulation with comment and whitespace preservation.
55
repository: https://github.com/dart-lang/yaml_edit
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
TEST FOR ISSUE #55 -- https://github.com/dart-lang/yaml_edit/issues/55
2+
---
3+
name: sample
4+
version: 0.1.0
5+
environment:
6+
sdk: ^3.0.0
7+
dependencies:
8+
dev_dependencies:
9+
retry:
10+
---
11+
- [remove, ['dev_dependencies', 'retry']]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
REMOVE KEY FROM MAP
2+
---
3+
foo: true
4+
bar: false
5+
---
6+
- [remove, [bar]]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
REMOVE KEY FROM MAP WITH TRAILING COMMA
2+
---
3+
foo: true
4+
bar: false # remove this comment
5+
---
6+
- [remove, [bar]]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
REMOVE NESTED KEY FROM MAP
2+
---
3+
A: true
4+
B:
5+
foo: true
6+
bar: true
7+
---
8+
- [remove, [B, foo]]
9+
- [remove, [B, bar]]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
REMOVE NESTED KEY FROM MAP WITH NULL
2+
---
3+
A: true
4+
B:
5+
foo:
6+
---
7+
- [remove, [B, foo]]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
REMOVE NESTED KEY FROM MAP WITH TRAILING COMMA
2+
---
3+
A: true
4+
B:
5+
bar: false # remove this comment
6+
---
7+
- [remove, [B, bar]]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: sample
2+
version: 0.1.0
3+
environment:
4+
sdk: ^3.0.0
5+
dependencies:
6+
dev_dependencies:
7+
retry:
8+
---
9+
name: sample
10+
version: 0.1.0
11+
environment:
12+
sdk: ^3.0.0
13+
dependencies:
14+
dev_dependencies:
15+
{}

0 commit comments

Comments
 (0)