Skip to content

Commit 3849511

Browse files
authored
Fix alphabetical ordering single element map (#2259)
1 parent d03c394 commit 3849511

File tree

6 files changed

+56
-30
lines changed

6 files changed

+56
-30
lines changed

pkgs/yaml_edit/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.2.3
2+
3+
- Fix alphabetical ordering when inserting into a single-element maps.
4+
([#2258](https://github.com/dart-lang/tools/issues/2258))
5+
16
## 2.2.2
27

38
- Suppress warnings previously printed to `stdout` when parsing YAML internally.

pkgs/yaml_edit/lib/src/utils.dart

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,7 @@ bool isFlowYamlCollectionNode(Object value) =>
148148
int getMapInsertionIndex(YamlMap map, Object newKey) {
149149
final keys = map.nodes.keys.map((k) => k.toString()).toList();
150150

151-
// We can't deduce ordering if list is empty, so then we just we just append
152-
if (keys.length <= 1) {
153-
return map.length;
154-
}
155-
151+
// Detect if the keys are not already sorted, append new entry to the end
156152
for (var i = 1; i < keys.length; i++) {
157153
if (keys[i].compareTo(keys[i - 1]) < 0) {
158154
return map.length;

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.2
2+
version: 2.2.3
33
description: >-
44
A library for YAML manipulation with comment and whitespace preservation.
55
repository: https://github.com/dart-lang/tools/tree/main/pkgs/yaml_edit

pkgs/yaml_edit/test/editor_test.dart

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ void main() {
2929

3030
expect(yamlEditor.edits, [
3131
SourceEdit(5, 5, " YAML Ain't Markup Language"),
32-
SourceEdit(32, 0, '\nXML: Extensible Markup Language\n'),
33-
SourceEdit(0, 33, '')
32+
SourceEdit(0, 0, 'XML: Extensible Markup Language\n'),
33+
SourceEdit(32, 32, '')
3434
]);
35+
expect(
36+
yamlEditor.toString(), equals('XML: Extensible Markup Language\n'));
3537
});
3638

3739
test('that do not automatically update with internal list', () {
@@ -48,9 +50,10 @@ void main() {
4850
expect(firstEdits, [SourceEdit(5, 5, " YAML Ain't Markup Language")]);
4951
expect(yamlEditor.edits, [
5052
SourceEdit(5, 5, " YAML Ain't Markup Language"),
51-
SourceEdit(32, 0, '\nXML: Extensible Markup Language\n'),
52-
SourceEdit(0, 33, '')
53+
SourceEdit(0, 0, 'XML: Extensible Markup Language\n'),
54+
SourceEdit(32, 32, '')
5355
]);
56+
expect(yamlEditor.toString(), 'XML: Extensible Markup Language\n');
5457
});
5558
});
5659
}
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
test: test
22
---
3-
test: test
43
"a!\"#$%&'()*+,-.\/09:;<=>?@AZ[\\]^_`az{|}~": safe
5-
---
64
test: test
7-
"a!\"#$%&'()*+,-.\/09:;<=>?@AZ[\\]^_`az{|}~": safe
8-
?foo: safe question mark
95
---
10-
test: test
11-
"a!\"#$%&'()*+,-.\/09:;<=>?@AZ[\\]^_`az{|}~": safe
126
?foo: safe question mark
13-
:foo: safe colon
14-
---
15-
test: test
167
"a!\"#$%&'()*+,-.\/09:;<=>?@AZ[\\]^_`az{|}~": safe
17-
?foo: safe question mark
18-
:foo: safe colon
19-
-foo: safe dash
20-
---
218
test: test
22-
"a!\"#$%&'()*+,-.\/09:;<=>?@AZ[\\]^_`az{|}~": safe
9+
---
10+
:foo: safe colon
2311
?foo: safe question mark
12+
"a!\"#$%&'()*+,-.\/09:;<=>?@AZ[\\]^_`az{|}~": safe
13+
test: test
14+
---
15+
-foo: safe dash
2416
:foo: safe colon
17+
?foo: safe question mark
18+
"a!\"#$%&'()*+,-.\/09:;<=>?@AZ[\\]^_`az{|}~": safe
19+
test: test
20+
---
2521
-foo: safe dash
22+
:foo: safe colon
23+
?foo: safe question mark
24+
"a!\"#$%&'()*+,-.\/09:;<=>?@AZ[\\]^_`az{|}~": safe
25+
test: test
2626
this is#not: a comment

pkgs/yaml_edit/test/update_test.dart

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -715,12 +715,9 @@ c: 3
715715
doc.update(['XML'], 'Extensible Markup Language');
716716

717717
expect(
718-
doc.toString(),
719-
equals(
720-
"{YAML: YAML Ain't Markup Language, "
721-
'XML: Extensible Markup Language}',
722-
),
723-
);
718+
doc.toString(),
719+
'{XML: Extensible Markup Language, '
720+
"YAML: YAML Ain't Markup Language}");
724721
expectYamlBuilderValue(doc, {
725722
'XML': 'Extensible Markup Language',
726723
'YAML': "YAML Ain't Markup Language",
@@ -745,6 +742,31 @@ d: 4
745742
expectYamlBuilderValue(doc, {'a': 1, 'b': 2, 'c': 3, 'd': 4});
746743
});
747744

745+
test('Preserves alphabetical order single', () {
746+
{
747+
final doc = YamlEditor('''
748+
b: 2
749+
''');
750+
doc.update(['a'], 1);
751+
expect(doc.toString(), '''
752+
a: 1
753+
b: 2
754+
''');
755+
expectYamlBuilderValue(doc, {'a': 1, 'b': 2});
756+
}
757+
{
758+
final doc = YamlEditor('''
759+
a: 1
760+
''');
761+
doc.update(['b'], 2);
762+
expect(doc.toString(), '''
763+
a: 1
764+
b: 2
765+
''');
766+
expectYamlBuilderValue(doc, {'a': 1, 'b': 2});
767+
}
768+
});
769+
748770
// Regression testing to ensure it works without leading whitespace
749771
test('(2)', () {
750772
final doc = YamlEditor('a: 1');

0 commit comments

Comments
 (0)