Skip to content

Commit

Permalink
Allow visiting non-parsed documents (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Jan 31, 2021
1 parent bfd8855 commit a294ec2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
1 change: 0 additions & 1 deletion src/ast/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ export { YAMLMap, findPair } from './YAMLMap.js'
export { YAMLSeq } from './YAMLSeq.js'

export { toJS } from './toJS.js'
export { visit } from './visit.js'
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { Document } from './doc/Document.js'
import { YAMLSemanticError } from './errors.js'
import { warn } from './log.js'

export { visit } from './ast/index.js'
export { defaultOptions, scalarOptions } from './options.js'
export { visit } from './visit.js'
export { Document, parseCST }

export function parseAllDocuments(src, options) {
Expand Down
18 changes: 9 additions & 9 deletions src/ast/visit.js → src/visit.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Type } from '../constants.js'
import { Alias } from './Alias.js'
import { Node } from './Node.js'
import { Pair } from './Pair.js'
import { Scalar } from './Scalar.js'
import { YAMLMap } from './YAMLMap.js'
import { YAMLSeq } from './YAMLSeq.js'
import { Alias } from './ast/Alias.js'
import { Node } from './ast/Node.js'
import { Pair } from './ast/Pair.js'
import { Scalar } from './ast/Scalar.js'
import { YAMLMap } from './ast/YAMLMap.js'
import { YAMLSeq } from './ast/YAMLSeq.js'
import { Document } from './doc/Document.js'

const BREAK = Symbol('break visit')
const SKIP = Symbol('skip children')
Expand Down Expand Up @@ -32,7 +32,7 @@ function _visit(key, node, visitor, path) {
} else if (parent instanceof Pair) {
if (key === 'key') parent.key = ctrl
else parent.value = ctrl
} else if (parent && parent.type === Type.DOCUMENT) {
} else if (parent instanceof Document) {
parent.contents = ctrl
} else {
const pt = parent && parent.type
Expand Down Expand Up @@ -68,7 +68,7 @@ function _visit(key, node, visitor, path) {
}

export function visit(node, visitor) {
if (node && node.type === Type.DOCUMENT) {
if (node instanceof Document) {
const cd = _visit(null, node.contents, visitor, Object.freeze([node]))
if (cd === REMOVE) node.contents = null
} else _visit(null, node, visitor, Object.freeze([]))
Expand Down
14 changes: 13 additions & 1 deletion tests/visit.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as YAML from '../index.js'
const { parseDocument, visit } = YAML
const { Document, parseDocument, visit } = YAML

test('Map', () => {
const doc = parseDocument('{ one: 1, two }')
Expand Down Expand Up @@ -66,6 +66,18 @@ test('Visit called with non-Document root', () => {
])
})

test('Visiting a constructed document', () => {
const doc = new Document([1, 'two'])
const fn = jest.fn()
visit(doc, { Map: fn, Pair: fn, Seq: fn, Alias: fn, Scalar: fn })
expect(fn.mock.calls).toMatchObject([
[null, { items: [{}, {}] }, [{}]],
[0, { value: 1 }, [{}, {}]],
[1, { value: 'two' }, [{}, {}]]
])
expect(String(doc)).toBe('- 1\n- two\n')
})

test('Only Scalar defined', () => {
const doc = parseDocument('foo:\n - "one"\n - \'two\'\n')
const Scalar = jest.fn()
Expand Down

0 comments on commit a294ec2

Please sign in to comment.