-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
75 lines (69 loc) · 1.86 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'use strict';
const typeOf = require('kind-of');
/**
* Visit the given `node` with the specified function, the calls
* [mapVisit](#mapVisit) if `node.nodes` exists.
*
* ```js
* const ast = new Node({type: 'root'});
* visit(ast, function(node) {
* node.val = '*';
* });
* console.log(ast.val);
* //=> '*'
* ```
* @param {Object} `node` (required) Instance of [snapdragon-node][], or if a plain object is passed it will be converted to an instance of [snapdragon-node][].
* @param {Function} `fn` (required)
* @param {Object} `parent` (optional) Instance of [snapdragon-node][] to use as the `node.parent`
* @return {Object} Returns the given node.
* @api public
*/
function visit(node, fn) {
if (typeof fn !== 'function') {
throw new TypeError('expected fn to be a visitor function');
}
if (typeOf(node) !== 'object') {
throw new Error('expected node to be an object');
}
fn(node);
return node.nodes ? mapVisit(node, fn) : node;
}
/**
* Calls [visit](#visit) on each node in `node.nodes`.
*
* ```js
* var node = new Node({type: 'star', val: ''});
* var ast = new Node({type: 'root', nodes: []});
* ast.push(node);
*
* visit.map(ast, function(node) {
* if (node.type === 'star') {
* node.val = '*';
* }
* });
*
* console.log(node.val);
* //=> '*'
* ```
* @param {Object} `node`
* @param {Function} `fn`
* @return {Object} Returns the given node.
* @api public
*/
function mapVisit(node, fn) {
if (typeof fn !== 'function') {
throw new TypeError('expected fn to be a visitor function');
}
if (typeOf(node) !== 'object') {
throw new Error('expected node to be an object');
}
if (!Array.isArray(node.nodes)) {
throw new TypeError('expected node.nodes to be an array');
}
for (const child of node.nodes) {
visit(child, fn);
}
return node;
}
visit.map = visit.mapVisit = mapVisit;
module.exports = visit;