Skip to content

Commit

Permalink
fix: Support # within %TAG prefixes with trailing #comments
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed May 26, 2024
1 parent ba89ff2 commit 69f3517
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/parse/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
24 changes: 23 additions & 1 deletion tests/directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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'
Expand Down

0 comments on commit 69f3517

Please sign in to comment.