Skip to content

Commit

Permalink
fix: Always reset indentation in lexer on ... (fixes #558)
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Jun 29, 2024
1 parent aea700d commit f685e1c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
7 changes: 2 additions & 5 deletions src/parse/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,14 +310,11 @@ export class Lexer {
if (ch === '-' || ch === '.') {
if (!this.atEnd && !this.hasChars(4)) return this.setNext('line-start')
const s = this.peek(3)
if (s === '---' && isEmpty(this.charAt(3))) {
if ((s === '---' || s === '...') && isEmpty(this.charAt(3))) {
yield* this.pushCount(3)
this.indentValue = 0
this.indentNext = 0
return 'doc'
} else if (s === '...' && isEmpty(this.charAt(3))) {
yield* this.pushCount(3)
return 'stream'
return s === '---' ? 'doc' : 'stream'
}
}
this.indentValue = yield* this.pushSpaces(false)
Expand Down
18 changes: 13 additions & 5 deletions tests/lexer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Lexer } from 'yaml'
import { CST, Lexer } from 'yaml'

const { DOCUMENT: DOC, SCALAR } = CST

test('unexpected unindent in quoted string with CRLF', () => {
const src = '- "\r\nx"'
Expand All @@ -8,23 +10,29 @@ test('unexpected unindent in quoted string with CRLF', () => {
res.push(lex)
if (++n === 10) break
}
expect(res).toEqual(['\u0002', '-', ' ', '"', '\r\n', '\u001f', 'x"'])
expect(res).toEqual([DOC, '-', ' ', '"', '\r\n', SCALAR, 'x"'])
})

test('plain scalar + CRLF + comment', () => {
const src = 'foo\r\n# bar'
const res = Array.from(new Lexer().lex(src))
expect(res).toEqual(['\u0002', '\u001f', 'foo', '\r\n', '# bar'])
expect(res).toEqual([DOC, SCALAR, 'foo', '\r\n', '# bar'])
})

test('Scalar starting with : after flow-ish key in preceding node in flow collection', () => {
const src = '[[], :@]'
const res = Array.from(new Lexer().lex(src))
expect(res).toEqual(['\u0002', '[', '[', ']', ',', ' ', '\u001f', ':@', ']'])
expect(res).toEqual([DOC, '[', '[', ']', ',', ' ', SCALAR, ':@', ']'])
})

test('unindented comment in flow collection', () => {
const src = '- {\n#c\n }'
const res = Array.from(new Lexer().lex(src))
expect(res).toEqual(['\u0002', '-', ' ', '{', '\n', '#c', '\n', ' ', '}'])
expect(res).toEqual([DOC, '-', ' ', '{', '\n', '#c', '\n', ' ', '}'])
})

test('indented map + ... + unindented flow collection', () => {
const src = ' :\n...\n{\n}'
const res = Array.from(new Lexer().lex(src))
expect(res).toEqual([DOC, ' ', ':', '\n', '...', '\n', DOC, '{', '\n', '}'])
})

0 comments on commit f685e1c

Please sign in to comment.