Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 55 #75

Merged
merged 3 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## 2.2.1-wip
## 2.2.1

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

## 2.2.0

Expand Down
19 changes: 15 additions & 4 deletions lib/src/map_mutations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ SourceEdit _replaceInBlockMap(
}

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

Expand Down Expand Up @@ -175,9 +176,19 @@ SourceEdit _removeFromBlockMap(
final keySpan = keyNode.span;
var end = getContentSensitiveEnd(valueNode);
final yaml = yamlEdit.toString();
final lineEnding = getLineEnding(yaml);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side-note - we should probably cache the result of getLineEnding somewhere.
Not in this PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, some of this could benefit from more structure.
But performance isn't really much of a concern.


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

Expand All @@ -187,16 +198,16 @@ SourceEdit _removeFromBlockMap(
///
/// We do this because we suspect that our users will want the inline
/// comments to disappear too.
final nextNewLine = yaml.indexOf('\n', end);
final nextNewLine = yaml.indexOf(lineEnding, end);
if (nextNewLine != -1) {
end = nextNewLine + 1;
end = nextNewLine + lineEnding.length;
}

final nextNode = getNextKeyNode(map, keyNode);

if (start > 0) {
final lastHyphen = yaml.lastIndexOf('-', start - 1);
final lastNewLine = yaml.lastIndexOf('\n', start - 1);
final lastNewLine = yaml.lastIndexOf(lineEnding, start - 1);
if (lastHyphen > lastNewLine) {
start = lastHyphen + 2;

Expand All @@ -208,7 +219,7 @@ SourceEdit _removeFromBlockMap(
end += nextNode.span.start.column;
}
} else if (lastNewLine > lastHyphen) {
start = lastNewLine + 1;
start = lastNewLine + lineEnding.length;
}
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: yaml_edit
version: 2.2.1-wip
version: 2.2.1
description: >-
A library for YAML manipulation with comment and whitespace preservation.
repository: https://github.com/dart-lang/yaml_edit
Expand Down
11 changes: 11 additions & 0 deletions test/testdata/input/issue_55.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
TEST FOR ISSUE #55 -- https://github.com/dart-lang/yaml_edit/issues/55
---
name: sample
version: 0.1.0
environment:
sdk: ^3.0.0
dependencies:
dev_dependencies:
retry:
---
- [remove, ['dev_dependencies', 'retry']]
6 changes: 6 additions & 0 deletions test/testdata/input/remove_key.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
REMOVE KEY FROM MAP
---
foo: true
bar: false
---
- [remove, [bar]]
6 changes: 6 additions & 0 deletions test/testdata/input/remove_key_with_trailing_comma.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
REMOVE KEY FROM MAP WITH TRAILING COMMA
---
foo: true
bar: false # remove this comment
---
- [remove, [bar]]
9 changes: 9 additions & 0 deletions test/testdata/input/remove_nested_key.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
REMOVE NESTED KEY FROM MAP
---
A: true
B:
foo: true
bar: true
---
- [remove, [B, foo]]
- [remove, [B, bar]]
7 changes: 7 additions & 0 deletions test/testdata/input/remove_nested_key_with_null.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
REMOVE NESTED KEY FROM MAP WITH NULL
---
A: true
B:
foo:
---
- [remove, [B, foo]]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
REMOVE NESTED KEY FROM MAP WITH TRAILING COMMA
---
A: true
B:
bar: false # remove this comment
---
- [remove, [B, bar]]
15 changes: 15 additions & 0 deletions test/testdata/output/issue_55.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: sample
version: 0.1.0
environment:
sdk: ^3.0.0
dependencies:
dev_dependencies:
retry:
---
name: sample
version: 0.1.0
environment:
sdk: ^3.0.0
dependencies:
dev_dependencies:
{}
jonasfj marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 4 additions & 0 deletions test/testdata/output/remove_key.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
foo: true
bar: false
---
foo: true
4 changes: 4 additions & 0 deletions test/testdata/output/remove_key_with_trailing_comma.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
foo: true
bar: false # remove this comment
---
foo: true
12 changes: 12 additions & 0 deletions test/testdata/output/remove_nested_key.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
A: true
B:
foo: true
bar: true
---
A: true
B:
bar: true
---
A: true
B:
{}
7 changes: 7 additions & 0 deletions test/testdata/output/remove_nested_key_with_null.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
A: true
B:
foo:
---
A: true
B:
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
A: true
B:
bar: false # remove this comment
---
A: true
B:
{}