Skip to content

Commit e6d97d2

Browse files
feat(encode): emit canonical empty-array form key: []
Per §9.1 v3.2, empty arrays MUST emit as `key: []` (and `- []` after a list-item marker), not as `key[0]:`. The header-only emission is now reserved for non-empty arrays.
1 parent 1782076 commit e6d97d2

2 files changed

Lines changed: 5 additions & 6 deletions

File tree

‎packages/toon/src/encode/encoders.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ export function* encodeArrayLines(
128128
options: ResolvedEncodeOptions,
129129
): Generator<string> {
130130
if (value.length === 0) {
131-
const header = formatHeader(0, { key, delimiter: options.delimiter })
132-
yield indentedLine(depth, header, options.indent)
131+
const line = key != null ? `${encodeKey(key)}: []` : '[]'
132+
yield indentedLine(depth, line, options.indent)
133133
return
134134
}
135135

@@ -324,9 +324,8 @@ export function* encodeObjectAsListItemLines(
324324
}
325325
else if (isJsonArray(firstValue)) {
326326
if (firstValue.length === 0) {
327-
// Empty array: `- key[0]:`
328-
const header = formatHeader(0, { delimiter: options.delimiter })
329-
yield indentedListItem(depth, `${encodedKey}${header}`, options.indent)
327+
// Empty array: `- key: []`
328+
yield indentedListItem(depth, `${encodedKey}: []`, options.indent)
330329
}
331330
else if (isArrayOfPrimitives(firstValue)) {
332331
// Inline primitive array: `- key[N]: values`

‎packages/toon/test/normalization.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe('JavaScript-specific type normalization', () => {
4848

4949
it('converts empty Set to empty array', () => {
5050
const result = encode(new Set())
51-
expect(result).toBe('[0]:')
51+
expect(result).toBe('[]')
5252
})
5353
})
5454

0 commit comments

Comments
 (0)