Skip to content

Commit

Permalink
fix: Improve missing-argument error on node .toJS() method (#458)
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Apr 4, 2023
1 parent 84b3811 commit aa0a1d4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/nodes/Node.ts
Expand Up @@ -4,7 +4,7 @@ import type { ToJSOptions } from '../options.js'
import { Token } from '../parse/cst.js'
import type { StringifyContext } from '../stringify/stringify.js'
import type { Alias } from './Alias.js'
import { NODE_TYPE } from './identity.js'
import { isDocument, NODE_TYPE } from './identity.js'
import type { Scalar } from './Scalar.js'
import { toJS, ToJSContext } from './toJS.js'
import type { YAMLMap } from './YAMLMap.js'
Expand Down Expand Up @@ -97,6 +97,7 @@ export abstract class NodeBase {
doc: Document<Node, boolean>,
{ mapAsMap, maxAliasCount, onAnchor, reviver }: ToJSOptions = {}
): any {
if (!isDocument(doc)) throw new TypeError('A document argument is required')
const ctx: ToJSContext = {
anchors: new Map(),
doc,
Expand Down
5 changes: 5 additions & 0 deletions tests/node-to-js.ts
Expand Up @@ -63,6 +63,11 @@ describe('alias', () => {
})

describe('options', () => {
test('doc is required', () => {
const doc = parseDocument('key: 42')
expect(() => doc.contents?.toJS({} as any)).toThrow(TypeError)
})

test('mapAsMap', () => {
const doc = parseDocument('key: 42')
expect(doc.contents?.toJS(doc, { mapAsMap: true })).toMatchObject(
Expand Down

0 comments on commit aa0a1d4

Please sign in to comment.