Skip to content
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
5 changes: 5 additions & 0 deletions pkgs/yaml_edit/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.2.3

- Fix alphabetical ordering when inserting into a single-element maps.
([#2258](https://github.com/dart-lang/tools/issues/2258))

## 2.2.2

- Suppress warnings previously printed to `stdout` when parsing YAML internally.
Expand Down
6 changes: 1 addition & 5 deletions pkgs/yaml_edit/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,7 @@ bool isFlowYamlCollectionNode(Object value) =>
int getMapInsertionIndex(YamlMap map, Object newKey) {
final keys = map.nodes.keys.map((k) => k.toString()).toList();

// We can't deduce ordering if list is empty, so then we just we just append
if (keys.length <= 1) {
return map.length;
}

// Detect if the keys are not already sorted, append new entry to the end
for (var i = 1; i < keys.length; i++) {
if (keys[i].compareTo(keys[i - 1]) < 0) {
return map.length;
Expand Down
2 changes: 1 addition & 1 deletion pkgs/yaml_edit/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: yaml_edit
version: 2.2.2
version: 2.2.3
description: >-
A library for YAML manipulation with comment and whitespace preservation.
repository: https://github.com/dart-lang/tools/tree/main/pkgs/yaml_edit
Expand Down
11 changes: 7 additions & 4 deletions pkgs/yaml_edit/test/editor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ void main() {

expect(yamlEditor.edits, [
SourceEdit(5, 5, " YAML Ain't Markup Language"),
SourceEdit(32, 0, '\nXML: Extensible Markup Language\n'),
SourceEdit(0, 33, '')
SourceEdit(0, 0, 'XML: Extensible Markup Language\n'),
SourceEdit(32, 32, '')
]);
expect(
yamlEditor.toString(), equals('XML: Extensible Markup Language\n'));
});

test('that do not automatically update with internal list', () {
Expand All @@ -48,9 +50,10 @@ void main() {
expect(firstEdits, [SourceEdit(5, 5, " YAML Ain't Markup Language")]);
expect(yamlEditor.edits, [
SourceEdit(5, 5, " YAML Ain't Markup Language"),
SourceEdit(32, 0, '\nXML: Extensible Markup Language\n'),
SourceEdit(0, 33, '')
SourceEdit(0, 0, 'XML: Extensible Markup Language\n'),
SourceEdit(32, 32, '')
]);
expect(yamlEditor.toString(), 'XML: Extensible Markup Language\n');
});
});
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
test: test
---
test: test
"a!\"#$%&'()*+,-.\/09:;<=>?@AZ[\\]^_`az{|}~": safe
---
test: test
"a!\"#$%&'()*+,-.\/09:;<=>?@AZ[\\]^_`az{|}~": safe
?foo: safe question mark
---
test: test
"a!\"#$%&'()*+,-.\/09:;<=>?@AZ[\\]^_`az{|}~": safe
?foo: safe question mark
:foo: safe colon
---
test: test
"a!\"#$%&'()*+,-.\/09:;<=>?@AZ[\\]^_`az{|}~": safe
?foo: safe question mark
:foo: safe colon
-foo: safe dash
---
test: test
"a!\"#$%&'()*+,-.\/09:;<=>?@AZ[\\]^_`az{|}~": safe
---
:foo: safe colon
?foo: safe question mark
"a!\"#$%&'()*+,-.\/09:;<=>?@AZ[\\]^_`az{|}~": safe
test: test
---
-foo: safe dash
:foo: safe colon
?foo: safe question mark
"a!\"#$%&'()*+,-.\/09:;<=>?@AZ[\\]^_`az{|}~": safe
test: test
---
-foo: safe dash
:foo: safe colon
?foo: safe question mark
"a!\"#$%&'()*+,-.\/09:;<=>?@AZ[\\]^_`az{|}~": safe
test: test
this is#not: a comment
34 changes: 28 additions & 6 deletions pkgs/yaml_edit/test/update_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -715,12 +715,9 @@ c: 3
doc.update(['XML'], 'Extensible Markup Language');

expect(
doc.toString(),
equals(
"{YAML: YAML Ain't Markup Language, "
'XML: Extensible Markup Language}',
),
);
doc.toString(),
'{XML: Extensible Markup Language, '
"YAML: YAML Ain't Markup Language}");
expectYamlBuilderValue(doc, {
'XML': 'Extensible Markup Language',
'YAML': "YAML Ain't Markup Language",
Expand All @@ -745,6 +742,31 @@ d: 4
expectYamlBuilderValue(doc, {'a': 1, 'b': 2, 'c': 3, 'd': 4});
});

test('Preserves alphabetical order single', () {
{
final doc = YamlEditor('''
b: 2
''');
doc.update(['a'], 1);
expect(doc.toString(), '''
a: 1
b: 2
''');
expectYamlBuilderValue(doc, {'a': 1, 'b': 2});
}
{
final doc = YamlEditor('''
a: 1
''');
doc.update(['b'], 2);
expect(doc.toString(), '''
a: 1
b: 2
''');
expectYamlBuilderValue(doc, {'a': 1, 'b': 2});
}
});

// Regression testing to ensure it works without leading whitespace
test('(2)', () {
final doc = YamlEditor('a: 1');
Expand Down