diff --git a/src/compose/resolve-props.ts b/src/compose/resolve-props.ts index 2780ba98..d00fe52e 100644 --- a/src/compose/resolve-props.ts +++ b/src/compose/resolve-props.ts @@ -100,7 +100,8 @@ export function resolveProps( case 'newline': if (atNewline) { if (comment) comment += token.source - else if (!found || indicator !== 'seq-item-ind') spaceBefore = true + else if ((!found || indicator !== 'seq-item-ind') && !tag && !anchor) + spaceBefore = true } else commentSep += token.source atNewline = true hasNewline = true diff --git a/src/stringify/stringify.ts b/src/stringify/stringify.ts index 81c109ce..aff50283 100644 --- a/src/stringify/stringify.ts +++ b/src/stringify/stringify.ts @@ -192,6 +192,8 @@ export function stringify( : node.toString(ctx, onComment, onChompKeep) if (!props) return str return node instanceof Scalar || str[0] === '{' || str[0] === '[' - ? `${props} ${str}` + ? str !== '' + ? `${props} ${str}` + : props + '\n' : `${props}\n${ctx.indent}${str}` } diff --git a/tests/tag-fixer.ts b/tests/tag-fixer.ts new file mode 100644 index 00000000..99886c06 --- /dev/null +++ b/tests/tag-fixer.ts @@ -0,0 +1,10 @@ +import { parseDocument, stringify } from 'yaml' + +test('tag test', () => { + const broken = `- x: !!null + +- y +` + + expect(stringify(parseDocument(broken))).toEqual(broken) +})