Skip to content

Commit

Permalink
fix: Set correct node-end position for empty values with comments (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Oct 2, 2022
1 parent e97948d commit 4ac0f5d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/compose/compose-node.ts
Expand Up @@ -22,6 +22,7 @@ interface Props {
comment: string
anchor: SourceToken | null
tag: SourceToken | null
end: number
}

const CN = { composeNode, composeEmptyNode }
Expand Down Expand Up @@ -93,7 +94,7 @@ export function composeEmptyNode(
offset: number,
before: Token[] | undefined,
pos: number | null,
{ spaceBefore, comment, anchor, tag }: Props,
{ spaceBefore, comment, anchor, tag, end }: Props,
onError: ComposeErrorHandler
) {
const token: FlowScalar = {
Expand All @@ -109,7 +110,10 @@ export function composeEmptyNode(
onError(anchor, 'BAD_ALIAS', 'Anchor cannot be an empty string')
}
if (spaceBefore) node.spaceBefore = true
if (comment) node.comment = comment
if (comment) {
node.comment = comment
node.range[2] = end
}
return node
}

Expand Down
18 changes: 18 additions & 0 deletions tests/doc/errors.js
Expand Up @@ -122,6 +122,24 @@ describe('block collections', () => {
}
])
})

test('key after long comment on empty value (eemeli/yaml#413)', () => {
const doc = YAML.parseDocument(source`
one:
# 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
2 changes: 1 addition & 1 deletion tests/doc/parse.js
Expand Up @@ -324,7 +324,7 @@ describe('empty(ish) nodes', () => {
test('empty node position', () => {
const doc = YAML.parseDocument('\r\na: # 123\r\n')
const empty = doc.contents.items[0].value
expect(empty.range).toEqual([5, 5, 5])
expect(empty.range).toEqual([5, 5, 12])
})

test('parse an empty string as null', () => {
Expand Down

0 comments on commit 4ac0f5d

Please sign in to comment.