Skip to content

Commit

Permalink
fix: Allow tab as indent for line comments before nodes (fixes #548)
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Jun 8, 2024
1 parent 0318fc5 commit 99fc94d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
16 changes: 13 additions & 3 deletions src/compose/resolve-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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': {
Expand Down Expand Up @@ -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,
Expand Down
16 changes: 15 additions & 1 deletion tests/doc/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<YAML.YAMLSeq, false>(source`
[ a, #c0
Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit 99fc94d

Please sign in to comment.