Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: eemeli/yaml
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.4.2
Choose a base ref
...
head repository: eemeli/yaml
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v2.4.3
Choose a head ref
  • 7 commits
  • 12 files changed
  • 1 contributor

Commits on May 17, 2024

  1. fix: Check for non-node complex keys when stringifying with simpleKeys (

    eemeli committed May 17, 2024
    Copy the full SHA
    ba89ff2 View commit details

Commits on May 26, 2024

  1. Copy the full SHA
    69f3517 View commit details

Commits on Jun 2, 2024

  1. fix: Do not parse -.NaN or +.nan as NaN (#546)

    eemeli committed Jun 2, 2024
    Copy the full SHA
    edc623d View commit details
  2. fix: Improve error when parsing a non-string value (fixes #459)

    eemeli committed Jun 2, 2024
    Copy the full SHA
    a1dc96f View commit details
  3. chore: Refresh lockfile

    eemeli committed Jun 2, 2024
    Copy the full SHA
    b92b602 View commit details
  4. style: Apply updated Prettier style

    eemeli committed Jun 2, 2024
    Copy the full SHA
    fad10b2 View commit details
  5. 2.4.3

    eemeli committed Jun 2, 2024
    Copy the full SHA
    208d98f View commit details
Showing with 881 additions and 925 deletions.
  1. +8 −4 docs/04_documents.md
  2. +795 −904 package-lock.json
  3. +1 −1 package.json
  4. +9 −3 src/parse/lexer.ts
  5. +3 −3 src/schema/Schema.ts
  6. +1 −1 src/schema/core/float.ts
  7. +1 −1 src/schema/yaml-1.1/float.ts
  8. +1 −1 src/stringify/stringifyPair.ts
  9. +23 −1 tests/directives.ts
  10. +9 −0 tests/doc/parse.ts
  11. +18 −0 tests/doc/stringify.ts
  12. +12 −6 tests/doc/types.ts
12 changes: 8 additions & 4 deletions docs/04_documents.md
Original file line number Diff line number Diff line change
@@ -40,6 +40,14 @@ doc.contents
// range: [ 0, 180, 180 ] }
```

These functions should never throw,
provided that `str` is a string and the `options` are valid.
Errors and warnings are included in the documents' `errors` and `warnings` arrays.
In particular, if `errors` is not empty
it's likely that the document's parsed `contents` are not entirely correct.

The `contents` of a parsed document will always consist of `Scalar`, `Map`, `Seq` or `null` values.

#### `parseDocument(str, options = {}): Document`

Parses a single `Document` from the input `str`; used internally by `parse`.
@@ -56,10 +64,6 @@ See [Options](#options) for more information on the second parameter.

<br/>

These functions should never throw; errors and warnings are included in the documents' `errors` and `warnings` arrays. In particular, if `errors` is not empty it's likely that the document's parsed `contents` are not entirely correct.

The `contents` of a parsed document will always consist of `Scalar`, `Map`, `Seq` or `null` values.

## Creating Documents

#### `new Document(value, replacer?, options = {})`
1,699 changes: 795 additions & 904 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yaml",
"version": "2.4.2",
"version": "2.4.3",
"license": "ISC",
"author": "Eemeli Aro <eemeli@gmail.com>",
"repository": "github:eemeli/yaml",
12 changes: 9 additions & 3 deletions src/parse/lexer.ts
Original file line number Diff line number Diff line change
@@ -174,6 +174,7 @@ export class Lexer {
*/
*lex(source: string, incomplete = false) {
if (source) {
if (typeof source !== 'string') throw TypeError('source is not a string')
this.buffer = this.buffer ? this.buffer + source : source
this.lineEndPos = null
}
@@ -274,10 +275,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]
6 changes: 3 additions & 3 deletions src/schema/Schema.ts
Original file line number Diff line number Diff line change
@@ -17,11 +17,11 @@ export class Schema {
name: string
sortMapEntries: ((a: Pair, b: Pair) => number) | null
tags: Array<CollectionTag | ScalarTag>
toStringOptions: Readonly<ToStringOptions> | null;
toStringOptions: Readonly<ToStringOptions> | null

// Used by createNode() and composeScalar()
declare readonly [MAP]: CollectionTag;
declare readonly [SCALAR]: ScalarTag;
declare readonly [MAP]: CollectionTag
declare readonly [SCALAR]: ScalarTag
declare readonly [SEQ]: CollectionTag

constructor({
2 changes: 1 addition & 1 deletion src/schema/core/float.ts
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ export const floatNaN: ScalarTag = {
identify: value => typeof value === 'number',
default: true,
tag: 'tag:yaml.org,2002:float',
test: /^(?:[-+]?\.(?:inf|Inf|INF|nan|NaN|NAN))$/,
test: /^(?:[-+]?\.(?:inf|Inf|INF)|\.nan|\.NaN|\.NAN)$/,
resolve: str =>
str.slice(-3).toLowerCase() === 'nan'
? NaN
2 changes: 1 addition & 1 deletion src/schema/yaml-1.1/float.ts
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ export const floatNaN: ScalarTag = {
identify: value => typeof value === 'number',
default: true,
tag: 'tag:yaml.org,2002:float',
test: /^[-+]?\.(?:inf|Inf|INF|nan|NaN|NAN)$/,
test: /^(?:[-+]?\.(?:inf|Inf|INF)|\.nan|\.NaN|\.NAN)$/,
resolve: (str: string) =>
str.slice(-3).toLowerCase() === 'nan'
? NaN
2 changes: 1 addition & 1 deletion src/stringify/stringifyPair.ts
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ export function stringifyPair(
if (keyComment) {
throw new Error('With simple keys, key nodes cannot have comments')
}
if (isCollection(key)) {
if (isCollection(key) || (!isNode(key) && typeof key === 'object')) {
const msg = 'With simple keys, collection cannot be used as a key value'
throw new Error(msg)
}
24 changes: 23 additions & 1 deletion tests/directives.ts
Original file line number Diff line number Diff line change
@@ -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'
9 changes: 9 additions & 0 deletions tests/doc/parse.ts
Original file line number Diff line number Diff line change
@@ -154,6 +154,15 @@ aliases:
})
})

test('buffer as source (eemeli/yaml#459)', () => {
const buffer = readFileSync(
resolve(__dirname, '../artifacts/prettier-circleci-config.yml')
)
expect(() => YAML.parseDocument(buffer as any)).toThrow(
'source is not a string'
)
})

describe('eemeli/yaml#19', () => {
test('map', () => {
const src = 'a:\n # 123'
18 changes: 18 additions & 0 deletions tests/doc/stringify.ts
Original file line number Diff line number Diff line change
@@ -723,6 +723,24 @@ describe('simple keys', () => {
)
})

test('key with JS object value', () => {
const doc = YAML.parseDocument<any>('[foo]: bar')
doc.contents.items[0].key = { foo: 42 }
expect(doc.toString()).toBe('? foo: 42\n: bar\n')
expect(() => doc.toString({ simpleKeys: true })).toThrow(
/With simple keys, collection cannot be used as a key value/
)
})

test('key with JS null value', () => {
const doc = YAML.parseDocument<any>('[foo]: bar')
doc.contents.items[0].key = null
expect(doc.toString()).toBe('? null\n: bar\n')
expect(() => doc.toString({ simpleKeys: true })).toThrow(
/With simple keys, collection cannot be used as a key value/
)
})

test('key value lingth > 1024', () => {
const str = `
? ${new Array(1026).join('a')}
18 changes: 12 additions & 6 deletions tests/doc/types.ts
Original file line number Diff line number Diff line change
@@ -373,19 +373,22 @@ option: TruE\n`)
const src = `canonical: 6.8523015e+5
fixed: 685230.15
negative infinity: -.inf
not a number: .NaN`
not a number: .NaN
not a NaN: -.NaN`

const doc = parseDocument(src)
expect(doc.toJS()).toMatchObject({
canonical: 685230.15,
fixed: 685230.15,
'negative infinity': Number.NEGATIVE_INFINITY,
'not a number': NaN
'not a number': NaN,
'not a NaN': '-.NaN'
})
expect(String(doc)).toBe(`canonical: 6.8523015e+5
fixed: 685230.15
negative infinity: -.inf
not a number: .nan\n`)
not a number: .nan
not a NaN: -.NaN\n`)
})

test('!!int', () => {
@@ -546,7 +549,8 @@ exponential: 685.230_15e+03
fixed: 685_230.15
sexagesimal: 190:20:30.15
negative infinity: -.inf
not a number: .NaN`
not a number: .NaN
not a NaN: -.NaN`

const doc = parseDocument(src)
expect(doc.toJS()).toMatchObject({
@@ -555,7 +559,8 @@ not a number: .NaN`
fixed: 685230.15,
sexagesimal: 685230.15,
'negative infinity': Number.NEGATIVE_INFINITY,
'not a number': NaN
'not a number': NaN,
'not a NaN': '-.NaN'
})
expect(String(doc)).toBe(`%YAML 1.1
---
@@ -564,7 +569,8 @@ exponential: 6.8523015e+5
fixed: 685230.15
sexagesimal: 190:20:30.15
negative infinity: -.inf
not a number: .nan\n`)
not a number: .nan
not a NaN: -.NaN\n`)
})

test('!!int', () => {