Skip to content

Commit

Permalink
fix: Set correct node-end position for block collections with comments (
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Oct 5, 2022
1 parent acb5f47 commit 8b0a3a4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/compose/resolve-block-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export function resolveBlockMap(

if (ctx.atRoot) ctx.atRoot = false
let offset = bm.offset
let commentEnd: number | null = null
for (const collItem of bm.items) {
const { start, key, sep, value } = collItem

Expand All @@ -45,7 +46,7 @@ export function resolveBlockMap(
onError(offset, 'BAD_INDENT', startColMsg)
}
if (!keyProps.anchor && !keyProps.tag && !sep) {
// TODO: assert being at last item?
commentEnd = keyProps.end
if (keyProps.comment) {
if (map.comment) map.comment += '\n' + keyProps.comment
else map.comment = keyProps.comment
Expand Down Expand Up @@ -128,6 +129,8 @@ export function resolveBlockMap(
}
}

map.range = [bm.offset, offset, offset]
if (commentEnd && commentEnd < offset)
onError(commentEnd, 'IMPOSSIBLE', 'Map comment with trailing content')
map.range = [bm.offset, offset, commentEnd ?? offset]
return map as YAMLMap.Parsed
}
10 changes: 5 additions & 5 deletions src/compose/resolve-block-seq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export function resolveBlockSeq(

if (ctx.atRoot) ctx.atRoot = false
let offset = bs.offset
let commentEnd: number | null = null
for (const { start, value } of bs.items) {
const props = resolveProps(start, {
indicator: 'seq-item-ind',
Expand All @@ -23,30 +24,29 @@ export function resolveBlockSeq(
onError,
startOnNewline: true
})
offset = props.end
if (!props.found) {
if (props.anchor || props.tag || value) {
if (value && value.type === 'block-seq')
onError(
offset,
props.end,
'BAD_INDENT',
'All sequence items must start at the same column'
)
else
onError(offset, 'MISSING_CHAR', 'Sequence item without - indicator')
} else {
// TODO: assert being at last item?
commentEnd = props.end
if (props.comment) seq.comment = props.comment
continue
}
}
const node = value
? composeNode(ctx, value, props, onError)
: composeEmptyNode(ctx, offset, start, null, props, onError)
: composeEmptyNode(ctx, props.end, start, null, props, onError)
if (ctx.schema.compat) flowIndentCheck(bs.indent, value, onError)
offset = node.range[2]
seq.items.push(node)
}
seq.range = [bs.offset, offset, offset]
seq.range = [bs.offset, offset, commentEnd ?? offset]
return seq as YAMLSeq.Parsed
}
38 changes: 38 additions & 0 deletions tests/doc/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,44 @@ describe('block collections', () => {
`)
expect(doc.errors).toMatchObject([])
})

test('key after long comment on block map (eemeli/yaml#413)', () => {
const doc = YAML.parseDocument(source`
one:
sub: a
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
two: b
`)
expect(doc.errors).toMatchObject([])
})

test('key after long comment on block seq (eemeli/yaml#413)', () => {
const doc = YAML.parseDocument(source`
one:
- a
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
# large block of text, large block of text, large block of text, large block of text, large block of text,
two: b
`)
expect(doc.errors).toMatchObject([])
})
})

describe('flow collections', () => {
Expand Down

0 comments on commit 8b0a3a4

Please sign in to comment.