Skip to content

Commit d2d82cd

Browse files
Ishad-M-I-Mjonasfj
andauthored
Fix YamlEditor.update method leaving trailing spaces at eol (dart-archive/yaml_edit#42)
* Fix for selecting the correct index in insertion * Add new test cases * Add more tests and format corrections * Update `CHANGELOG.md` * Fix leaving whitespace at eol on YamlEditor.update & update the testcases * Fix formatting * Apply proposed change * Update lib/src/map_mutations.dart Co-authored-by: Jonas Finnemann Jensen <[email protected]> --------- Co-authored-by: Jonas Finnemann Jensen <[email protected]>
1 parent 3930dad commit d2d82cd

File tree

9 files changed

+45
-40
lines changed

9 files changed

+45
-40
lines changed

pkgs/yaml_edit/lib/src/map_mutations.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ SourceEdit _replaceInBlockMap(
139139
valueAsString = lineEnding + valueAsString;
140140
}
141141

142+
if (!valueAsString.startsWith(lineEnding)) {
143+
// prepend whitespace to ensure there is space after colon.
144+
valueAsString = ' ' + valueAsString;
145+
}
146+
142147
/// +1 accounts for the colon
143148
final start = keyNode.span.end.offset + 1;
144149
var end = getContentSensitiveEnd(map.nodes[key]!);
@@ -148,7 +153,7 @@ SourceEdit _replaceInBlockMap(
148153
/// this.
149154
if (end < start) end = start;
150155

151-
return SourceEdit(start, end - start, ' ' + valueAsString);
156+
return SourceEdit(start, end - start, valueAsString);
152157
}
153158

154159
/// Performs the string operation on [yaml] to achieve the effect of replacing

pkgs/yaml_edit/test/alias_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ b: *SS
118118

119119
test('removing nested map alias anchor results in AliasError', () {
120120
final doc = YamlEditor('''
121-
a:
121+
a:
122122
c: &SS Sammy Sosa
123123
b: *SS
124124
''');
@@ -129,7 +129,7 @@ b: *SS
129129
test('removing nested map alias reference results in AliasError', () {
130130
final doc = YamlEditor('''
131131
a: &SS Sammy Sosa
132-
b:
132+
b:
133133
c: *SS
134134
''');
135135

pkgs/yaml_edit/test/preservation_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,16 @@ void main() {
3838
test('tilde', expectLoadPreservesYAML('~'));
3939
test('false', expectLoadPreservesYAML('false'));
4040

41-
test('block map', expectLoadPreservesYAML('''a:
41+
test('block map', expectLoadPreservesYAML('''a:
4242
b: 1
4343
'''));
44-
test('block list', expectLoadPreservesYAML('''a:
44+
test('block list', expectLoadPreservesYAML('''a:
4545
- 1
4646
'''));
4747
test('complicated example', () {
4848
expectLoadPreservesYAML('''verb: RecommendCafes
4949
map:
50-
a:
50+
a:
5151
b: 1
5252
recipe:
5353
- verb: Score

pkgs/yaml_edit/test/testdata/input/nested_block_map.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
NESTED BLOCK MAP
22
---
33
a: 1
4-
b:
4+
b:
55
d: 4
66
e: 5
77
c: 3

pkgs/yaml_edit/test/testdata/output/nested_block_map.golden

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
a: 1
2-
b:
2+
b:
33
d: 4
44
e: 5
55
c: 3
66
---
77
a: 1
8-
b:
8+
b:
99
d: 4
1010
e: 6
1111
c: 3
1212
---
1313
a: 1
14-
b:
14+
b:
1515
d: 4
16-
e:
16+
e:
1717
- 1
1818
- 2
1919
- 3
2020
c: 3
2121
---
2222
a: 1
23-
b:
23+
b:
2424
d: 4
25-
e:
25+
e:
2626
- 1
2727
- 2
2828
- 3

pkgs/yaml_edit/test/testdata/output/pubspec.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,6 @@ dependencies:
111111
retry: ^3.0.1
112112
yaml: ^3.2.0 # For YAML
113113

114-
dev_dependencies:
114+
dev_dependencies:
115115
test: ^1.14.4
116116

pkgs/yaml_edit/test/update_test.dart

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ void main() {
126126
test('nested', () {
127127
final doc = YamlEditor('''
128128
a: 1
129-
b:
129+
b:
130130
d: 4
131131
e: 5
132132
c: 3
@@ -135,7 +135,7 @@ c: 3
135135

136136
expect(doc.toString(), equals('''
137137
a: 1
138-
b:
138+
b:
139139
d: 4
140140
e: 6
141141
c: 3
@@ -201,14 +201,14 @@ a: 1
201201

202202
test('nested (5)', () {
203203
final doc = YamlEditor('''
204-
a:
204+
a:
205205
- a: 1
206206
b: 2
207207
- null
208208
''');
209209
doc.update(['a', 0], false);
210210
expect(doc.toString(), equals('''
211-
a:
211+
a:
212212
- false
213213
214214
- null
@@ -220,14 +220,14 @@ a:
220220

221221
test('nested (6)', () {
222222
final doc = YamlEditor('''
223-
a:
223+
a:
224224
- - 1
225225
- 2
226226
- null
227227
''');
228228
doc.update(['a', 0], false);
229229
expect(doc.toString(), equals('''
230-
a:
230+
a:
231231
- false
232232
233233
- null
@@ -261,7 +261,7 @@ b: false
261261
doc.update(['a'], {'retry': '3.0.1'});
262262

263263
expect(doc.toString(), equals('''
264-
a:
264+
a:
265265
retry: 3.0.1
266266
b: false
267267
'''));
@@ -277,7 +277,7 @@ a: # comment
277277

278278
expect(doc.toString(), equals('''
279279
# comment
280-
a:
280+
a:
281281
retry: 3.0.1 # comment
282282
# comment
283283
'''));
@@ -286,7 +286,7 @@ a:
286286
test('nested scalar -> flow list', () {
287287
final doc = YamlEditor('''
288288
a: 1
289-
b:
289+
b:
290290
d: 4
291291
e: 5
292292
c: 3
@@ -295,9 +295,9 @@ c: 3
295295

296296
expect(doc.toString(), equals('''
297297
a: 1
298-
b:
298+
b:
299299
d: 4
300-
e:
300+
e:
301301
- 1
302302
- 2
303303
- 3
@@ -316,7 +316,7 @@ c: 3
316316
test('nested block map -> scalar', () {
317317
final doc = YamlEditor('''
318318
a: 1
319-
b:
319+
b:
320320
d: 4
321321
e: 5
322322
c: 3
@@ -334,7 +334,7 @@ c: 3
334334
test('nested block map -> scalar with comments', () {
335335
final doc = YamlEditor('''
336336
a: 1
337-
b:
337+
b:
338338
d: 4
339339
e: 5
340340
@@ -359,7 +359,7 @@ b: 2
359359
test('nested scalar -> block map', () {
360360
final doc = YamlEditor('''
361361
a: 1
362-
b:
362+
b:
363363
d: 4
364364
e: 5
365365
c: 3
@@ -368,9 +368,9 @@ c: 3
368368

369369
expect(doc.toString(), equals('''
370370
a: 1
371-
b:
371+
b:
372372
d: 4
373-
e:
373+
e:
374374
x: 3
375375
y: 4
376376
c: 3
@@ -388,7 +388,7 @@ c: 3
388388
test('nested block map with comments', () {
389389
final doc = YamlEditor('''
390390
a: 1
391-
b:
391+
b:
392392
d: 4
393393
e: 5 # comment
394394
c: 3
@@ -397,7 +397,7 @@ c: 3
397397

398398
expect(doc.toString(), equals('''
399399
a: 1
400-
b:
400+
b:
401401
d: 4
402402
e: 6 # comment
403403
c: 3
@@ -412,7 +412,7 @@ c: 3
412412
test('nested block map with comments (2)', () {
413413
final doc = YamlEditor('''
414414
a: 1
415-
b:
415+
b:
416416
d: 4 # comment
417417
# comment
418418
e: 5 # comment
@@ -423,7 +423,7 @@ c: 3
423423

424424
expect(doc.toString(), equals('''
425425
a: 1
426-
b:
426+
b:
427427
d: 4 # comment
428428
# comment
429429
e: 6 # comment

pkgs/yaml_edit/test/utils_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ c:
213213
}));
214214

215215
expect(doc.toString(), equals('''
216-
strings:
216+
strings:
217217
plain: string
218218
folded: >-
219219
string
@@ -366,7 +366,7 @@ a:
366366
});
367367

368368
expect(doc.toString(), equals('''
369-
a:
369+
a:
370370
f: ""
371371
g: 1
372372
'''));
@@ -385,7 +385,7 @@ a:
385385
});
386386

387387
expect(doc.toString(), equals('''
388-
a:
388+
a:
389389
f: " a"
390390
g: 1
391391
'''));

pkgs/yaml_edit/test/windows_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ c: 3\r
6262
test('update nested scalar -> flow list', () {
6363
final doc = YamlEditor('''
6464
a: 1\r
65-
b: \r
65+
b:\r
6666
d: 4\r
6767
e: 5\r
6868
c: 3\r
@@ -71,9 +71,9 @@ c: 3\r
7171

7272
expect(doc.toString(), equals('''
7373
a: 1\r
74-
b: \r
74+
b:\r
7575
d: 4\r
76-
e: \r
76+
e:\r
7777
- 1\r
7878
- 2\r
7979
- 3\r

0 commit comments

Comments
 (0)