diff --git a/src/compose/resolve-props.ts b/src/compose/resolve-props.ts index 593f2735..b78b33c6 100644 --- a/src/compose/resolve-props.ts +++ b/src/compose/resolve-props.ts @@ -22,6 +22,7 @@ export function resolveProps( let hasNewline = false let hasNewlineAfterProp = false let reqSpace = false + let tab: SourceToken | null = null let anchor: SourceToken | null = null let tag: SourceToken | null = null let comma: SourceToken | null = null @@ -41,6 +42,12 @@ export function resolveProps( ) reqSpace = false } + if (tab) { + if (token.type !== 'comment') { + onError(tab, 'TAB_AS_INDENT', 'Tabs are not allowed as indentation') + } + tab = null + } switch (token.type) { case 'space': // At the doc level, tabs at line start may be parsed @@ -51,8 +58,9 @@ export function resolveProps( atNewline && indicator !== 'doc-start' && token.source[0] === '\t' - ) - onError(token, 'TAB_AS_INDENT', 'Tabs are not allowed as indentation') + ) { + tab = token + } hasSpace = true break case 'comment': { @@ -152,12 +160,14 @@ export function resolveProps( next.type !== 'newline' && next.type !== 'comma' && (next.type !== 'scalar' || next.source !== '') - ) + ) { onError( next.offset, 'MISSING_CHAR', 'Tags and anchors must be separated from the next token by white space' ) + } + if (tab) onError(tab, 'TAB_AS_INDENT', 'Tabs are not allowed as indentation') return { comma, found, diff --git a/tests/doc/comments.ts b/tests/doc/comments.ts index eee981a0..f9aa4fff 100644 --- a/tests/doc/comments.ts +++ b/tests/doc/comments.ts @@ -266,7 +266,7 @@ describe('parse comments', () => { }) }) - describe('flow collection commens', () => { + describe('flow collection comments', () => { test('line comment after , in seq', () => { const doc = YAML.parseDocument(source` [ a, #c0 @@ -299,6 +299,20 @@ describe('parse comments', () => { ]) }) }) + + describe('line comments with leading tabs (#548)', () => { + for (const { name, src } of [ + { name: 'after scalar', src: 'foo\n\t#c' }, + { name: 'in seq', src: '- x\n\t#c\n- y\n\t #d\n- z' }, + { name: 'in map value', src: 'x:\n\t#c\ny:' }, + { name: 'in map', src: 'x: 1\n\t#c\ny: 2' } + ]) { + test(name, () => { + const doc = YAML.parseDocument(src) + expect(doc.errors).toHaveLength(0) + }) + } + }) }) describe('stringify comments', () => {