From b1d2b287e80caeb262c4dc81459f52b982a5e741 Mon Sep 17 00:00:00 2001 From: Eemeli Aro Date: Sat, 13 Mar 2021 12:32:10 +0200 Subject: [PATCH] Allow for empty lines after node props (Fixes #242) --- src/cst/ParseContext.js | 8 ++++++-- tests/doc/parse.js | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/cst/ParseContext.js b/src/cst/ParseContext.js index 7b716a8d..5840caa0 100644 --- a/src/cst/ParseContext.js +++ b/src/cst/ParseContext.js @@ -125,8 +125,12 @@ export class ParseContext { ch === '\n' ) { if (ch === '\n') { - const lineStart = offset + 1 - const inEnd = Node.endOfIndent(src, lineStart) + let inEnd = offset + let lineStart + do { + lineStart = inEnd + 1 + inEnd = Node.endOfIndent(src, lineStart) + } while (src[inEnd] === '\n') const indentDiff = inEnd - (lineStart + this.indent) const noIndicatorAsIndent = parent.type === Type.SEQ_ITEM && parent.context.atLineStart diff --git a/tests/doc/parse.js b/tests/doc/parse.js index 2686785c..0715f121 100644 --- a/tests/doc/parse.js +++ b/tests/doc/parse.js @@ -466,6 +466,47 @@ describe('maps with no values', () => { }) }) +describe('collection item with anchor followed by empty line (#242)', () => { + test('reported 1', () => { + const src = ` +key1: &default + + subkey1: value1 + +key2: + <<: *default\n` + expect(YAML.parse(src, { merge: true })).toMatchObject({ + key1: { subkey1: 'value1' }, + key2: { subkey1: 'value1' } + }) + }) + + test('reported 2', () => { + const src = ` +key1: &default + + # This key ... + subkey1: value1 + +key2: + <<: *default\n` + expect(YAML.parse(src, { merge: true })).toMatchObject({ + key1: { subkey1: 'value1' }, + key2: { subkey1: 'value1' } + }) + }) + + test('minimal with anchor', () => { + const src = '- &a\n\n foo' + expect(YAML.parse(src)).toMatchObject(['foo']) + }) + + test('minimal with tag', () => { + const src = '- !!str\n\n foo' + expect(YAML.parse(src)).toMatchObject(['foo']) + }) +}) + describe('Excessive entity expansion attacks', () => { const root = path.resolve(__dirname, '../artifacts/pr104') const src1 = fs.readFileSync(path.resolve(root, 'case1.yml'), 'utf8')