yaml_edit null safety minor refactorings (#128)
* Adding key assertion in removeInMap
* Change to late declaration and non-nullable in remove()
diff --git a/lib/src/editor.dart b/lib/src/editor.dart
index c6f86de..01d4b44 100644
--- a/lib/src/editor.dart
+++ b/lib/src/editor.dart
@@ -408,12 +408,13 @@
/// '''
/// ```
YamlNode remove(Iterable<Object?> path) {
- SourceEdit? edit;
- var expectedNode = wrapAsYamlNode(null);
+ late SourceEdit edit;
+ late YamlNode expectedNode;
final nodeToRemove = _traverse(path, checkAlias: true);
if (path.isEmpty) {
edit = SourceEdit(0, _yaml.length, '');
+ expectedNode = wrapAsYamlNode(null);
/// Parsing an empty YAML document returns YamlScalar with value `null`.
_performEdit(edit, path, expectedNode);
@@ -437,7 +438,7 @@
updatedYamlMap(parentNode, (nodes) => nodes.remove(keyOrIndex));
}
- _performEdit(edit!, collectionPath, expectedNode);
+ _performEdit(edit, collectionPath, expectedNode);
return nodeToRemove;
}
diff --git a/lib/src/map_mutations.dart b/lib/src/map_mutations.dart
index 2084b59..63e9ca2 100644
--- a/lib/src/map_mutations.dart
+++ b/lib/src/map_mutations.dart
@@ -45,6 +45,7 @@
/// Performs the string operation on [yaml] to achieve the effect of removing
/// the element at [key] when re-parsed.
SourceEdit removeInMap(YamlEditor yamlEdit, YamlMap map, Object? key) {
+ assert(containsKey(map, key));
final keyNode = getKeyNode(map, key);
final valueNode = map.nodes[keyNode]!;