From 69f35171cbc101f84051bd2862ec8494391c028f Mon Sep 17 00:00:00 2001 From: Eemeli Aro Date: Sun, 26 May 2024 14:48:39 +0300 Subject: [PATCH] fix: Support # within %TAG prefixes with trailing #comments --- src/parse/lexer.ts | 11 ++++++++--- tests/directives.ts | 24 +++++++++++++++++++++++- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/parse/lexer.ts b/src/parse/lexer.ts index c398452e..6fbd360c 100644 --- a/src/parse/lexer.ts +++ b/src/parse/lexer.ts @@ -274,10 +274,15 @@ export class Lexer { } if (line[0] === '%') { let dirEnd = line.length - const cs = line.indexOf('#') - if (cs !== -1) { + let cs = line.indexOf('#') + while (cs !== -1) { const ch = line[cs - 1] - if (ch === ' ' || ch === '\t') dirEnd = cs - 1 + if (ch === ' ' || ch === '\t') { + dirEnd = cs - 1 + break + } else { + cs = line.indexOf('#', cs + 1) + } } while (true) { const ch = line[dirEnd - 1] diff --git a/tests/directives.ts b/tests/directives.ts index 540ef946..1d6d2a1e 100644 --- a/tests/directives.ts +++ b/tests/directives.ts @@ -2,7 +2,7 @@ import { parseDocument, Scalar } from 'yaml' import { source } from './_utils' describe('%TAG', () => { - test('parse', () => { + test('parse local tags', () => { const doc = parseDocument(source` %TAG ! !foo: %TAG !bar! !bar: @@ -24,6 +24,28 @@ describe('%TAG', () => { }) }) + test('parse global tags', () => { + const doc = parseDocument(source` + %TAG ! foo: + %TAG !bar! bar:bar#bar? #comment + --- + - !bar v1 + - !bar!foo v2 + `) + expect(doc.errors).toHaveLength(0) + expect(doc.directives.tags).toMatchObject({ + '!!': 'tag:yaml.org,2002:', + '!': 'foo:', + '!bar!': 'bar:bar#bar?' + }) + expect(doc.contents).toMatchObject({ + items: [ + { value: 'v1', tag: 'foo:bar' }, + { value: 'v2', tag: 'bar:bar#bar?foo' } + ] + }) + }) + test('create & stringify', () => { const doc = parseDocument('[ v1, v2 ]\n') ;(doc.get(0, true) as Scalar).tag = '!foo:foo'