From 1d902e974654523f390c2741b116ee8995b3f66a Mon Sep 17 00:00:00 2001 From: Eemeli Aro Date: Sat, 8 Jun 2024 15:16:14 +0300 Subject: [PATCH] fix: Do not allow tab before block collection (fixes #549) --- src/compose/resolve-props.ts | 2 +- tests/doc/errors.ts | 23 ++++++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/compose/resolve-props.ts b/src/compose/resolve-props.ts index b78b33c6..9a2d1469 100644 --- a/src/compose/resolve-props.ts +++ b/src/compose/resolve-props.ts @@ -56,7 +56,7 @@ export function resolveProps( if ( !flow && atNewline && - indicator !== 'doc-start' && + (indicator !== 'doc-start' || next?.type !== 'flow-collection') && token.source[0] === '\t' ) { tab = token diff --git a/tests/doc/errors.ts b/tests/doc/errors.ts index e72439e8..89d7cd7b 100644 --- a/tests/doc/errors.ts +++ b/tests/doc/errors.ts @@ -1,13 +1,22 @@ import * as YAML from 'yaml' import { source } from '../_utils' -test('fail on map value indented with tab', () => { - const src = 'a:\n\t1\nb:\n\t2\n' - const doc = YAML.parseDocument(src) - expect(doc.errors).not.toHaveLength(0) - expect(() => String(doc)).toThrow( - 'Document with errors cannot be stringified' - ) +describe('tabs as indentation', () => { + test('fail on map value indented with tab', () => { + const src = 'a:\n\t1\nb:\n\t2\n' + const doc = YAML.parseDocument(src) + expect(doc.errors[0]).toMatchObject({ code: 'TAB_AS_INDENT' }) + }) + + test('block sequence with leading tab', () => { + const doc = YAML.parseDocument('\t- x') + expect(doc.errors).toMatchObject([{ code: 'TAB_AS_INDENT' }]) + }) + + test('block map with leading tab', () => { + const doc = YAML.parseDocument('\tx: y') + expect(doc.errors).toMatchObject([{ code: 'TAB_AS_INDENT' }]) + }) }) test('eemeli/yaml#6', () => {