Skip to content

Commit

Permalink
Fix various bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Dec 15, 2020
1 parent 18cc015 commit 94e0e79
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/compose/resolve-block-map.ts
@@ -1,4 +1,5 @@
import { Pair, YAMLMap } from '../ast/index.js'
import { Type } from '../constants.js'
import type { Document } from '../doc/Document.js'
import type { BlockMap } from '../parse/parser.js'
import { composeNode } from './compose-node.js'
Expand All @@ -12,6 +13,7 @@ export function resolveBlockMap(
) {
const start = offset
const map = new YAMLMap(doc.schema)
map.type = Type.MAP
if (anchor) doc.anchors.setAnchor(map, anchor)

for (const { start, key, sep, value } of items) {
Expand Down
6 changes: 1 addition & 5 deletions src/compose/resolve-block-scalar.ts
Expand Up @@ -24,11 +24,7 @@ export function resolveBlockScalar(
// shortcut for empty contents
if (!scalar.source || chompStart === 0) {
const value =
header.chomp === '+'
? lines.map(line => line[0]).join('\n') + '\n'
: header.chomp === '-'
? ''
: '\n'
header.chomp === '+' ? lines.map(line => line[0]).join('\n') + '\n' : ''
let length = header.length
if (scalar.source) length += scalar.source.length
return { value, type, comment: header.comment, length }
Expand Down
2 changes: 2 additions & 0 deletions src/compose/resolve-block-seq.ts
@@ -1,4 +1,5 @@
import { YAMLSeq } from '../ast/index.js'
import { Type } from '../constants.js'
import type { Document } from '../doc/Document.js'
import type { BlockSequence } from '../parse/parser.js'
import { composeNode } from './compose-node.js'
Expand All @@ -12,6 +13,7 @@ export function resolveBlockSeq(
) {
const start = offset
const seq = new YAMLSeq(doc.schema)
seq.type = Type.SEQ
if (anchor) doc.anchors.setAnchor(seq, anchor)
for (const { start, value } of items) {
const props = resolveProps(
Expand Down
10 changes: 8 additions & 2 deletions src/compose/resolve-flow-collection.ts
@@ -1,4 +1,5 @@
import { Node, Pair, YAMLMap, YAMLSeq } from '../ast/index.js'
import { Type } from '../constants.js'
import type { Document } from '../doc/Document.js'
import type { FlowCollection, SourceToken } from '../parse/parser.js'
import { composeNode } from './compose-node.js'
Expand All @@ -12,6 +13,7 @@ export function resolveFlowCollection(
let offset = fc.offset
const isMap = fc.start.source === '{'
const coll = isMap ? new YAMLMap(doc.schema) : new YAMLSeq(doc.schema)
coll.type = isMap ? Type.FLOW_MAP : Type.FLOW_SEQ
if (_anchor) doc.anchors.setAnchor(coll, _anchor)

let key: Node.Parsed | null = null
Expand All @@ -24,6 +26,7 @@ export function resolveFlowCollection(
let anchor = ''
let tagName = ''

let atExplicitKey = false
let atValueEnd = false

function resetProps() {
Expand All @@ -33,6 +36,7 @@ export function resolveFlowCollection(
newlines = ''
anchor = ''
tagName = ''
atExplicitKey = false
atValueEnd = false
}

Expand All @@ -43,7 +47,7 @@ export function resolveFlowCollection(
const props = { spaceBefore, comment, anchor, tagName }
value = composeNode(doc, offset, props, onError)
}
if (isMap) {
if (isMap || atExplicitKey) {
const pair = key ? new Pair(key, value) : new Pair(value)
coll.items.push(pair)
} else {
Expand Down Expand Up @@ -97,8 +101,10 @@ export function resolveFlowCollection(
case 'explicit-key-ind':
if (anchor || tagName)
onError(offset, 'Anchors and tags must be after the ? indicator')
atExplicitKey = true
break
case 'map-value-ind': {
atExplicitKey = false
if (key) {
if (value) {
onError(offset, 'Missing {} around pair used as mapping key')
Expand Down Expand Up @@ -139,7 +145,7 @@ export function resolveFlowCollection(
}
if (isSourceToken) offset += (token as SourceToken).source.length
}
if (key || value) addItem()
if (key || value || atExplicitKey) addItem()
coll.range = [fc.offset, offset]
return coll as YAMLMap.Parsed | YAMLSeq.Parsed
}
11 changes: 9 additions & 2 deletions src/compose/resolve-props.ts
@@ -1,6 +1,13 @@
import { SourceToken } from '../parse/parser'
import { StreamDirectives } from './stream-directives'

function isSpaceBefore(sep: string) {
if (!sep) return false
const first = sep.indexOf('\n')
if (first === -1) return false
return sep.includes('\n', first + 1)
}

export function resolveProps(
directives: StreamDirectives,
start: SourceToken[],
Expand All @@ -27,7 +34,7 @@ export function resolveProps(
case 'comment': {
const cb = token.source.substring(1)
if (!hasComment) {
if (sep) spaceBefore = true
if (isSpaceBefore(sep)) spaceBefore = true
comment = cb
} else comment += sep + cb
hasComment = true
Expand Down Expand Up @@ -57,6 +64,6 @@ export function resolveProps(
}
if (token.source) length += token.source.length
}
if (!comment && sep) spaceBefore = true
if (!comment && isSpaceBefore(sep)) spaceBefore = true
return { found, spaceBefore, comment, anchor, tagName, length }
}
12 changes: 12 additions & 0 deletions src/parse/lexer.ts
Expand Up @@ -101,6 +101,7 @@ export class Lexer {

atEnd = false
buffer = ''
flowKey = false
flowLevel = 0
indent = 0
indentMore = ''
Expand Down Expand Up @@ -280,6 +281,7 @@ export class Lexer {
case '{':
case '[':
this.pushCount(1)
this.flowKey = false
this.flowLevel = 1
return 'flow'
case '}':
Expand Down Expand Up @@ -317,17 +319,27 @@ export class Lexer {
case '{':
case '[':
this.pushCount(1)
this.flowKey = false
this.flowLevel += 1
return 'flow'
case '}':
case ']':
this.pushCount(1)
this.flowKey = true
this.flowLevel -= 1
return this.flowLevel ? 'flow' : 'doc'
case '"':
case "'":
this.flowKey = true
return this.parseQuotedScalar()
case ':':
if (this.flowKey) {
this.pushCount(1) + this.pushSpaces()
return 'flow'
}
// fallthrough
default:
this.flowKey = false
return this.parsePlainScalar()
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/stringify/addComment.js
Expand Up @@ -7,7 +7,9 @@ export function addCommentBefore(str, indent, comment) {
export function addComment(str, indent, comment) {
return !comment
? str
: comment.indexOf('\n') === -1
? `${str} #${comment}`
: `${str}\n` + comment.replace(/^/gm, `${indent || ''}#`)
: comment.includes('\n')
? `${str}\n` + comment.replace(/^/gm, `${indent || ''}#`)
: str.endsWith(' ')
? `${str}#${comment}`
: `${str} #${comment}`
}

0 comments on commit 94e0e79

Please sign in to comment.