3 var convert = require('./convert')
9 // Assert if `test` passes for `node`.
10 // When a `parent` node is known the `index` of node should also be given.
11 // eslint-disable-next-line max-params
12 function is(node, test, index, parent, context) {
13 var hasParent = parent !== null && parent !== undefined
14 var hasIndex = index !== null && index !== undefined
15 var check = convert(test)
19 (typeof index !== 'number' || index < 0 || index === Infinity)
21 throw new Error('Expected positive finite index or child node')
24 if (hasParent && (!is(parent) || !parent.children)) {
25 throw new Error('Expected parent node')
28 if (!node || !node.type || typeof node.type !== 'string') {
32 if (hasParent !== hasIndex) {
33 throw new Error('Expected both parent and index')
36 return Boolean(check.call(context, node, index, parent))