Skip to content

Commit

Permalink
Stop aliasing pair comment to pair value comment
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Dec 27, 2018
1 parent 7d8ded9 commit 9b49ddb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 28 deletions.
11 changes: 5 additions & 6 deletions __tests__/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ k2:
key: { value: 'k1' },
value: {
commentBefore: 'c1',
items: [{ value: 'v1' }, { commentBefore: 'c2', value: 'v2' }]
},
comment: 'c3'
items: [{ value: 'v1' }, { commentBefore: 'c2', value: 'v2' }],
comment: 'c3'
}
},
{
key: { value: 'k2' },
Expand Down Expand Up @@ -272,16 +272,15 @@ describe('stringify comments', () => {
const doc = YAML.parseDocument(src)
doc.contents.items[0].key.commentBefore = 'c0'
doc.contents.items[0].key.comment = 'c1'
doc.contents.items[0].comment = 'c2'
const seq = doc.contents.items[0].value
seq.commentBefore = 'c2'
seq.items[0].commentBefore = 'c3'
seq.items[1].commentBefore = 'c4'
seq.comment = 'c5'
expect(String(doc)).toBe(
`#c0
? map #c1
:
#c2
: #c2
#c3
- value 1
#c4
Expand Down
40 changes: 18 additions & 22 deletions src/schema/Pair.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@ export default class Pair extends Node {
this.key.commentBefore = cb
}

get comment() {
return this.value && this.value.comment
}

set comment(comment) {
if (this.value == null) this.value = new Scalar(null)
this.value.comment = comment
}

get stringKey() {
const key = toJSON(this.key)
if (key === null) return ''
Expand Down Expand Up @@ -61,12 +52,16 @@ export default class Pair extends Node {
implicitKey: !explicitKey,
indent: indent + ' '
})
let keyStr = doc.schema.stringify(key, ctx, () => {
let str = doc.schema.stringify(key, ctx, () => {
keyComment = null
})
if (keyComment) keyStr = addComment(keyStr, ctx.indent, keyComment)
ctx.implicitKey = false
const valueStr = doc.schema.stringify(value, ctx, onComment)
str = addComment(str, ctx.indent, keyComment)
str = explicitKey ? `? ${str}\n${indent}:` : `${str}:`
if (this.comment) {
// expected (but not strictly required) to be a single-line comment
str = addComment(str, indent, this.comment)
if (onComment) onComment()
}
let vcb = ''
if (value) {
if (value.spaceBefore) vcb = '\n'
Expand All @@ -75,14 +70,15 @@ export default class Pair extends Node {
vcb += `\n${cs}`
}
}
if (explicitKey) {
const ws = vcb ? `${vcb}\n${ctx.indent}` : ' '
return `? ${keyStr}\n${indent}:${ws}${valueStr}`
} else if (value instanceof Collection) {
return `${keyStr}:${vcb}\n${ctx.indent}${valueStr}`
} else {
const ws = vcb ? `${vcb}\n${ctx.indent}` : ' '
return `${keyStr}:${ws}${valueStr}`
}
ctx.implicitKey = false
let valueComment = value instanceof Node && value.comment
let valueStr = doc.schema.stringify(value, ctx, () => {
valueComment = null
})
const ws =
vcb || this.comment || (!explicitKey && value instanceof Collection)
? `${vcb}\n${ctx.indent}`
: ' '
return addComment(str + ws + valueStr, ctx.indent, valueComment)
}
}

0 comments on commit 9b49ddb

Please sign in to comment.