Skip to content

Commit

Permalink
Parse block map value comments immediately after : as pair comments
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Dec 27, 2018
1 parent 9b49ddb commit 6d3cb36
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
15 changes: 5 additions & 10 deletions __tests__/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,9 @@ k2:
expect(doc.contents).toMatchObject({
items: [
{
commentBefore: 'c0',
key: { value: 'k1' },
comment: 'c1',
key: { commentBefore: 'c0', value: 'k1' },
value: {
commentBefore: 'c1',
items: [{ value: 'v1' }, { commentBefore: 'c2', value: 'v2' }],
comment: 'c3'
}
Expand All @@ -157,8 +156,7 @@ k2:
comment: 'c5'
})
expect(String(doc)).toBe(`#c0
k1:
#c1
k1: #c1
- v1
#c2
- v2
Expand Down Expand Up @@ -486,16 +484,13 @@ describe('eemeli/yaml#18', () => {
foo: #123
bar: 1\n`
const doc = YAML.parseDocument(src)
expect(String(doc)).toBe(`test1:
foo:
#123
bar: 1\n`)
expect(String(doc)).toBe(src)
})

test('minimal', () => {
const src = `foo: #123\n bar: baz\n`
const doc = YAML.parseDocument(src)
expect(String(doc)).toBe(`foo:\n #123\n bar: baz\n`)
expect(String(doc)).toBe(src)
})
})

Expand Down
3 changes: 2 additions & 1 deletion __tests__/corner-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,11 @@ describe('eemeli/yaml#l19', () => {
const doc = YAML.parseDocument(src)
expect(String(doc)).toBe('a: null # 123\n')
})

test('seq', () => {
const src = '- a: # 123'
const doc = YAML.parseDocument(src)
expect(String(doc)).toBe('- a: null # 123\n')
expect(String(doc)).toBe('- a: # 123\n null\n')
})
})

Expand Down
35 changes: 32 additions & 3 deletions src/schema/parseMap.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Type } from '../cst/Node'
import { Char, Type } from '../cst/Node'
import PlainValue from '../cst/PlainValue'
import { YAMLSemanticError, YAMLSyntaxError } from '../errors'
import Map from './Map'
Expand Down Expand Up @@ -58,6 +58,33 @@ export default function parseMap(doc, cst) {
return map
}

const valueHasPairComment = ({ context: { lineStart, node, src }, props }) => {
if (props.length === 0) return false
const { start } = props[0]
if (node && start > node.valueRange.start) return false
if (src[start] !== Char.COMMENT) return false
for (let i = lineStart; i < start; ++i) if (src[i] === '\n') return false
return true
}

function resolvePairComment(item, pair) {
if (!valueHasPairComment(item)) return
const comment = item.getPropValue(0, Char.COMMENT, true)
let found = false
const cb = pair.value.commentBefore
if (cb && cb.startsWith(comment)) {
pair.value.commentBefore = cb.substr(comment.length + 1)
found = true
} else {
const cc = pair.value.comment
if (!item.node && cc && cc.startsWith(comment)) {
pair.value.comment = cc.substr(comment.length + 1)
found = true
}
}
if (found) pair.comment = comment
}

function resolveBlockMapItems(doc, cst) {
const comments = []
const items = []
Expand Down Expand Up @@ -102,15 +129,17 @@ function resolveBlockMapItems(doc, cst) {
valueNode = new PlainValue(Type.PLAIN, [])
valueNode.context = { parent: item, src: item.context.src }
const pos = item.range.start + 1
const origPos = item.range.origStart + 1
valueNode.range = { start: pos, end: pos }
valueNode.valueRange = { start: pos, end: pos }
if (typeof item.range.origStart === 'number') {
const origPos = item.range.origStart + 1
valueNode.range.origStart = valueNode.range.origEnd = origPos
valueNode.valueRange.origStart = valueNode.valueRange.origEnd = origPos
}
}
items.push(new Pair(key, doc.resolveNode(valueNode)))
const pair = new Pair(key, doc.resolveNode(valueNode))
resolvePairComment(item, pair)
items.push(pair)
checkKeyLength(doc.errors, cst, i, key, keyStart)
key = undefined
keyStart = null
Expand Down

0 comments on commit 6d3cb36

Please sign in to comment.