Skip to content

Commit

Permalink
fix: Improve error when parsing a non-string value (fixes #459)
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Jun 2, 2024
1 parent edc623d commit a1dc96f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
12 changes: 8 additions & 4 deletions docs/04_documents.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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 = {})`
Expand Down
1 change: 1 addition & 0 deletions src/parse/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
9 changes: 9 additions & 0 deletions tests/doc/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit a1dc96f

Please sign in to comment.