Skip to content

Commit 4ac0f5d

Browse files
committed
fix: Set correct node-end position for empty values with comments (#413)
1 parent e97948d commit 4ac0f5d

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

src/compose/compose-node.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ interface Props {
2222
comment: string
2323
anchor: SourceToken | null
2424
tag: SourceToken | null
25+
end: number
2526
}
2627

2728
const CN = { composeNode, composeEmptyNode }
@@ -93,7 +94,7 @@ export function composeEmptyNode(
9394
offset: number,
9495
before: Token[] | undefined,
9596
pos: number | null,
96-
{ spaceBefore, comment, anchor, tag }: Props,
97+
{ spaceBefore, comment, anchor, tag, end }: Props,
9798
onError: ComposeErrorHandler
9899
) {
99100
const token: FlowScalar = {
@@ -109,7 +110,10 @@ export function composeEmptyNode(
109110
onError(anchor, 'BAD_ALIAS', 'Anchor cannot be an empty string')
110111
}
111112
if (spaceBefore) node.spaceBefore = true
112-
if (comment) node.comment = comment
113+
if (comment) {
114+
node.comment = comment
115+
node.range[2] = end
116+
}
113117
return node
114118
}
115119

tests/doc/errors.js

+18
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,24 @@ describe('block collections', () => {
122122
}
123123
])
124124
})
125+
126+
test('key after long comment on empty value (eemeli/yaml#413)', () => {
127+
const doc = YAML.parseDocument(source`
128+
one:
129+
# large block of text, large block of text, large block of text, large block of text, large block of text,
130+
# large block of text, large block of text, large block of text, large block of text, large block of text,
131+
# large block of text, large block of text, large block of text, large block of text, large block of text,
132+
# large block of text, large block of text, large block of text, large block of text, large block of text,
133+
# large block of text, large block of text, large block of text, large block of text, large block of text,
134+
# large block of text, large block of text, large block of text, large block of text, large block of text,
135+
# large block of text, large block of text, large block of text, large block of text, large block of text,
136+
# large block of text, large block of text, large block of text, large block of text, large block of text,
137+
# large block of text, large block of text, large block of text, large block of text, large block of text,
138+
# large block of text, large block of text, large block of text, large block of text, large block of text,
139+
two: b
140+
`)
141+
expect(doc.errors).toMatchObject([])
142+
})
125143
})
126144

127145
describe('flow collections', () => {

tests/doc/parse.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ describe('empty(ish) nodes', () => {
324324
test('empty node position', () => {
325325
const doc = YAML.parseDocument('\r\na: # 123\r\n')
326326
const empty = doc.contents.items[0].value
327-
expect(empty.range).toEqual([5, 5, 5])
327+
expect(empty.range).toEqual([5, 5, 12])
328328
})
329329

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

0 commit comments

Comments
 (0)