Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 22f2c6f

Browse files
committed
fix: In flow collections, allow []{} immediately after : with plain key (fixes #550)
1 parent 1d902e9 commit 22f2c6f

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

src/parse/lexer.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,13 @@ function isEmpty(ch: string) {
9191
}
9292
}
9393

94-
const hexDigits = '0123456789ABCDEFabcdef'.split('')
95-
const tagChars =
96-
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-#;/?:@&=+$_.!~*'()".split(
97-
''
98-
)
99-
const invalidFlowScalarChars = ',[]{}'.split('')
100-
const invalidAnchorChars = ' ,[]{}\n\r\t'.split('')
101-
const isNotAnchorChar = (ch: string) => !ch || invalidAnchorChars.includes(ch)
94+
const hexDigits = new Set('0123456789ABCDEFabcdef')
95+
const tagChars = new Set(
96+
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-#;/?:@&=+$_.!~*'()"
97+
)
98+
const flowIndicatorChars = new Set(',[]{}')
99+
const invalidAnchorChars = new Set(' ,[]{}\n\r\t')
100+
const isNotAnchorChar = (ch: string) => !ch || invalidAnchorChars.has(ch)
102101

103102
/**
104103
* Splits an input string into lexical tokens, i.e. smaller strings that are
@@ -573,7 +572,7 @@ export class Lexer {
573572
while ((ch = this.buffer[++i])) {
574573
if (ch === ':') {
575574
const next = this.buffer[i + 1]
576-
if (isEmpty(next) || (inFlow && next === ',')) break
575+
if (isEmpty(next) || (inFlow && flowIndicatorChars.has(next))) break
577576
end = i
578577
} else if (isEmpty(ch)) {
579578
let next = this.buffer[i + 1]
@@ -584,15 +583,14 @@ export class Lexer {
584583
next = this.buffer[i + 1]
585584
} else end = i
586585
}
587-
if (next === '#' || (inFlow && invalidFlowScalarChars.includes(next)))
588-
break
586+
if (next === '#' || (inFlow && flowIndicatorChars.has(next))) break
589587
if (ch === '\n') {
590588
const cs = this.continueScalar(i + 1)
591589
if (cs === -1) break
592590
i = Math.max(i, cs - 2) // to advance, but still account for ' #'
593591
}
594592
} else {
595-
if (inFlow && invalidFlowScalarChars.includes(ch)) break
593+
if (inFlow && flowIndicatorChars.has(ch)) break
596594
end = i
597595
}
598596
}
@@ -640,7 +638,7 @@ export class Lexer {
640638
case ':': {
641639
const inFlow = this.flowLevel > 0
642640
const ch1 = this.charAt(1)
643-
if (isEmpty(ch1) || (inFlow && invalidFlowScalarChars.includes(ch1))) {
641+
if (isEmpty(ch1) || (inFlow && flowIndicatorChars.has(ch1))) {
644642
if (!inFlow) this.indentNext = this.indentValue + 1
645643
else if (this.flowKey) this.flowKey = false
646644
return (
@@ -664,11 +662,11 @@ export class Lexer {
664662
let i = this.pos + 1
665663
let ch = this.buffer[i]
666664
while (ch) {
667-
if (tagChars.includes(ch)) ch = this.buffer[++i]
665+
if (tagChars.has(ch)) ch = this.buffer[++i]
668666
else if (
669667
ch === '%' &&
670-
hexDigits.includes(this.buffer[i + 1]) &&
671-
hexDigits.includes(this.buffer[i + 2])
668+
hexDigits.has(this.buffer[i + 1]) &&
669+
hexDigits.has(this.buffer[i + 2])
672670
) {
673671
ch = this.buffer[(i += 3)]
674672
} else break

tests/doc/parse.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,20 @@ describe('flow collection keys', () => {
259259
]
260260
})
261261
})
262+
263+
test('empty scalar as last flow collection value (#550)', () => {
264+
const doc = YAML.parseDocument<YAML.YAMLMap, false>('{c:}')
265+
expect(doc.contents.items).toMatchObject([
266+
{ key: { value: 'c' }, value: { value: null } }
267+
])
268+
})
269+
270+
test('plain key with no space before flow collection value (#550)', () => {
271+
const doc = YAML.parseDocument<YAML.YAMLMap, false>('{c:[]}')
272+
expect(doc.contents.items).toMatchObject([
273+
{ key: { value: 'c' }, value: { items: [] } }
274+
])
275+
})
262276
})
263277

264278
test('eemeli/yaml#38', () => {

0 commit comments

Comments
 (0)