Skip to content

Commit 2fa7f54

Browse files
committed
Allow same-column end for flow collections (Fixes #113, fixes #114, ref #108)
1 parent 2906c26 commit 2fa7f54

File tree

2 files changed

+83
-5
lines changed

2 files changed

+83
-5
lines changed

src/cst/FlowCollection.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,16 @@ export default class FlowCollection extends Node {
4747
this.items.push(blankLine)
4848
}
4949
offset = Node.endOfIndent(src, lineStart)
50-
if (offset - lineStart <= indent)
51-
this.error = new YAMLSemanticError(
52-
this,
53-
'Insufficient indentation in flow collection'
54-
)
50+
if (offset <= lineStart + indent) {
51+
char = src[offset]
52+
if (
53+
offset < lineStart + indent ||
54+
(char !== ']' && char !== '}')
55+
) {
56+
const msg = 'Insufficient indentation in flow collection'
57+
this.error = new YAMLSemanticError(this, msg)
58+
}
59+
}
5560
}
5661
break
5762
case ',':

tests/cst/corner-cases.js

+73
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { source } from 'common-tags'
12
import parse from '../../src/cst/parse'
23

34
describe('folded block with chomp: keep', () => {
@@ -230,3 +231,75 @@ test('blank line after less-indented comment (eemeli/yaml#91)', () => {
230231
{ type: 'MAP' }
231232
])
232233
})
234+
235+
describe('flow collection as same-line mapping key value', () => {
236+
test('eemeli/yaml#113', () => {
237+
const src = source`
238+
---
239+
foo:
240+
bar:
241+
enum: [
242+
"abc",
243+
"cde"
244+
]
245+
`
246+
const doc = parse(src)
247+
const barValue = doc[0].contents[0].items[1].node.items[1].node
248+
expect(barValue.items[1].node).toMatchObject({
249+
error: null,
250+
items: [
251+
{ char: '[' },
252+
{ type: 'QUOTE_DOUBLE' },
253+
{ char: ',' },
254+
{ type: 'QUOTE_DOUBLE' },
255+
{ char: ']' }
256+
],
257+
type: 'FLOW_SEQ'
258+
})
259+
})
260+
261+
test('eemeli/yaml#114', () => {
262+
const src = source`
263+
foo: {
264+
bar: boom
265+
}
266+
`
267+
const doc = parse(src)
268+
const flowCollection = doc[0].contents[0].items[1].node
269+
expect(flowCollection).toMatchObject({
270+
error: null,
271+
items: [
272+
{ char: '{' },
273+
{ type: 'PLAIN' },
274+
{ char: ':' },
275+
{ type: 'PLAIN' },
276+
{ char: '}' }
277+
],
278+
type: 'FLOW_MAP'
279+
})
280+
})
281+
282+
test('Fails on insufficient indent', () => {
283+
const src = source`
284+
foo: {
285+
bar: boom
286+
}
287+
`
288+
const doc = parse(' ' + src)
289+
const flowCollection = doc[0].contents[0].items[1].node
290+
expect(flowCollection).toMatchObject({
291+
error: {
292+
message: 'Insufficient indentation in flow collection',
293+
name: 'YAMLSemanticError'
294+
},
295+
items: [
296+
{ char: '{' },
297+
{ type: 'PLAIN' },
298+
{ char: ':' },
299+
{ type: 'PLAIN' },
300+
{ char: '}' }
301+
],
302+
type: 'FLOW_MAP'
303+
})
304+
})
305+
})

0 commit comments

Comments
 (0)