Skip to content

Commit 6d3cb36

Browse files
committed
Parse block map value comments immediately after : as pair comments
1 parent 9b49ddb commit 6d3cb36

3 files changed

Lines changed: 39 additions & 14 deletions

File tree

__tests__/comments.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,9 @@ k2:
141141
expect(doc.contents).toMatchObject({
142142
items: [
143143
{
144-
commentBefore: 'c0',
145-
key: { value: 'k1' },
144+
comment: 'c1',
145+
key: { commentBefore: 'c0', value: 'k1' },
146146
value: {
147-
commentBefore: 'c1',
148147
items: [{ value: 'v1' }, { commentBefore: 'c2', value: 'v2' }],
149148
comment: 'c3'
150149
}
@@ -157,8 +156,7 @@ k2:
157156
comment: 'c5'
158157
})
159158
expect(String(doc)).toBe(`#c0
160-
k1:
161-
#c1
159+
k1: #c1
162160
- v1
163161
#c2
164162
- v2
@@ -486,16 +484,13 @@ describe('eemeli/yaml#18', () => {
486484
foo: #123
487485
bar: 1\n`
488486
const doc = YAML.parseDocument(src)
489-
expect(String(doc)).toBe(`test1:
490-
foo:
491-
#123
492-
bar: 1\n`)
487+
expect(String(doc)).toBe(src)
493488
})
494489

495490
test('minimal', () => {
496491
const src = `foo: #123\n bar: baz\n`
497492
const doc = YAML.parseDocument(src)
498-
expect(String(doc)).toBe(`foo:\n #123\n bar: baz\n`)
493+
expect(String(doc)).toBe(src)
499494
})
500495
})
501496

__tests__/corner-cases.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,11 @@ describe('eemeli/yaml#l19', () => {
191191
const doc = YAML.parseDocument(src)
192192
expect(String(doc)).toBe('a: null # 123\n')
193193
})
194+
194195
test('seq', () => {
195196
const src = '- a: # 123'
196197
const doc = YAML.parseDocument(src)
197-
expect(String(doc)).toBe('- a: null # 123\n')
198+
expect(String(doc)).toBe('- a: # 123\n null\n')
198199
})
199200
})
200201

src/schema/parseMap.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Type } from '../cst/Node'
1+
import { Char, Type } from '../cst/Node'
22
import PlainValue from '../cst/PlainValue'
33
import { YAMLSemanticError, YAMLSyntaxError } from '../errors'
44
import Map from './Map'
@@ -58,6 +58,33 @@ export default function parseMap(doc, cst) {
5858
return map
5959
}
6060

61+
const valueHasPairComment = ({ context: { lineStart, node, src }, props }) => {
62+
if (props.length === 0) return false
63+
const { start } = props[0]
64+
if (node && start > node.valueRange.start) return false
65+
if (src[start] !== Char.COMMENT) return false
66+
for (let i = lineStart; i < start; ++i) if (src[i] === '\n') return false
67+
return true
68+
}
69+
70+
function resolvePairComment(item, pair) {
71+
if (!valueHasPairComment(item)) return
72+
const comment = item.getPropValue(0, Char.COMMENT, true)
73+
let found = false
74+
const cb = pair.value.commentBefore
75+
if (cb && cb.startsWith(comment)) {
76+
pair.value.commentBefore = cb.substr(comment.length + 1)
77+
found = true
78+
} else {
79+
const cc = pair.value.comment
80+
if (!item.node && cc && cc.startsWith(comment)) {
81+
pair.value.comment = cc.substr(comment.length + 1)
82+
found = true
83+
}
84+
}
85+
if (found) pair.comment = comment
86+
}
87+
6188
function resolveBlockMapItems(doc, cst) {
6289
const comments = []
6390
const items = []
@@ -102,15 +129,17 @@ function resolveBlockMapItems(doc, cst) {
102129
valueNode = new PlainValue(Type.PLAIN, [])
103130
valueNode.context = { parent: item, src: item.context.src }
104131
const pos = item.range.start + 1
105-
const origPos = item.range.origStart + 1
106132
valueNode.range = { start: pos, end: pos }
107133
valueNode.valueRange = { start: pos, end: pos }
108134
if (typeof item.range.origStart === 'number') {
135+
const origPos = item.range.origStart + 1
109136
valueNode.range.origStart = valueNode.range.origEnd = origPos
110137
valueNode.valueRange.origStart = valueNode.valueRange.origEnd = origPos
111138
}
112139
}
113-
items.push(new Pair(key, doc.resolveNode(valueNode)))
140+
const pair = new Pair(key, doc.resolveNode(valueNode))
141+
resolvePairComment(item, pair)
142+
items.push(pair)
114143
checkKeyLength(doc.errors, cst, i, key, keyStart)
115144
key = undefined
116145
keyStart = null

0 commit comments

Comments
 (0)