visit.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. 'use strict';
  2. var identity = require('./nodes/identity.js');
  3. const BREAK = Symbol('break visit');
  4. const SKIP = Symbol('skip children');
  5. const REMOVE = Symbol('remove node');
  6. /**
  7. * Apply a visitor to an AST node or document.
  8. *
  9. * Walks through the tree (depth-first) starting from `node`, calling a
  10. * `visitor` function with three arguments:
  11. * - `key`: For sequence values and map `Pair`, the node's index in the
  12. * collection. Within a `Pair`, `'key'` or `'value'`, correspondingly.
  13. * `null` for the root node.
  14. * - `node`: The current node.
  15. * - `path`: The ancestry of the current node.
  16. *
  17. * The return value of the visitor may be used to control the traversal:
  18. * - `undefined` (default): Do nothing and continue
  19. * - `visit.SKIP`: Do not visit the children of this node, continue with next
  20. * sibling
  21. * - `visit.BREAK`: Terminate traversal completely
  22. * - `visit.REMOVE`: Remove the current node, then continue with the next one
  23. * - `Node`: Replace the current node, then continue by visiting it
  24. * - `number`: While iterating the items of a sequence or map, set the index
  25. * of the next step. This is useful especially if the index of the current
  26. * node has changed.
  27. *
  28. * If `visitor` is a single function, it will be called with all values
  29. * encountered in the tree, including e.g. `null` values. Alternatively,
  30. * separate visitor functions may be defined for each `Map`, `Pair`, `Seq`,
  31. * `Alias` and `Scalar` node. To define the same visitor function for more than
  32. * one node type, use the `Collection` (map and seq), `Value` (map, seq & scalar)
  33. * and `Node` (alias, map, seq & scalar) targets. Of all these, only the most
  34. * specific defined one will be used for each node.
  35. */
  36. function visit(node, visitor) {
  37. const visitor_ = initVisitor(visitor);
  38. if (identity.isDocument(node)) {
  39. const cd = visit_(null, node.contents, visitor_, Object.freeze([node]));
  40. if (cd === REMOVE)
  41. node.contents = null;
  42. }
  43. else
  44. visit_(null, node, visitor_, Object.freeze([]));
  45. }
  46. // Without the `as symbol` casts, TS declares these in the `visit`
  47. // namespace using `var`, but then complains about that because
  48. // `unique symbol` must be `const`.
  49. /** Terminate visit traversal completely */
  50. visit.BREAK = BREAK;
  51. /** Do not visit the children of the current node */
  52. visit.SKIP = SKIP;
  53. /** Remove the current node */
  54. visit.REMOVE = REMOVE;
  55. function visit_(key, node, visitor, path) {
  56. const ctrl = callVisitor(key, node, visitor, path);
  57. if (identity.isNode(ctrl) || identity.isPair(ctrl)) {
  58. replaceNode(key, path, ctrl);
  59. return visit_(key, ctrl, visitor, path);
  60. }
  61. if (typeof ctrl !== 'symbol') {
  62. if (identity.isCollection(node)) {
  63. path = Object.freeze(path.concat(node));
  64. for (let i = 0; i < node.items.length; ++i) {
  65. const ci = visit_(i, node.items[i], visitor, path);
  66. if (typeof ci === 'number')
  67. i = ci - 1;
  68. else if (ci === BREAK)
  69. return BREAK;
  70. else if (ci === REMOVE) {
  71. node.items.splice(i, 1);
  72. i -= 1;
  73. }
  74. }
  75. }
  76. else if (identity.isPair(node)) {
  77. path = Object.freeze(path.concat(node));
  78. const ck = visit_('key', node.key, visitor, path);
  79. if (ck === BREAK)
  80. return BREAK;
  81. else if (ck === REMOVE)
  82. node.key = null;
  83. const cv = visit_('value', node.value, visitor, path);
  84. if (cv === BREAK)
  85. return BREAK;
  86. else if (cv === REMOVE)
  87. node.value = null;
  88. }
  89. }
  90. return ctrl;
  91. }
  92. /**
  93. * Apply an async visitor to an AST node or document.
  94. *
  95. * Walks through the tree (depth-first) starting from `node`, calling a
  96. * `visitor` function with three arguments:
  97. * - `key`: For sequence values and map `Pair`, the node's index in the
  98. * collection. Within a `Pair`, `'key'` or `'value'`, correspondingly.
  99. * `null` for the root node.
  100. * - `node`: The current node.
  101. * - `path`: The ancestry of the current node.
  102. *
  103. * The return value of the visitor may be used to control the traversal:
  104. * - `Promise`: Must resolve to one of the following values
  105. * - `undefined` (default): Do nothing and continue
  106. * - `visit.SKIP`: Do not visit the children of this node, continue with next
  107. * sibling
  108. * - `visit.BREAK`: Terminate traversal completely
  109. * - `visit.REMOVE`: Remove the current node, then continue with the next one
  110. * - `Node`: Replace the current node, then continue by visiting it
  111. * - `number`: While iterating the items of a sequence or map, set the index
  112. * of the next step. This is useful especially if the index of the current
  113. * node has changed.
  114. *
  115. * If `visitor` is a single function, it will be called with all values
  116. * encountered in the tree, including e.g. `null` values. Alternatively,
  117. * separate visitor functions may be defined for each `Map`, `Pair`, `Seq`,
  118. * `Alias` and `Scalar` node. To define the same visitor function for more than
  119. * one node type, use the `Collection` (map and seq), `Value` (map, seq & scalar)
  120. * and `Node` (alias, map, seq & scalar) targets. Of all these, only the most
  121. * specific defined one will be used for each node.
  122. */
  123. async function visitAsync(node, visitor) {
  124. const visitor_ = initVisitor(visitor);
  125. if (identity.isDocument(node)) {
  126. const cd = await visitAsync_(null, node.contents, visitor_, Object.freeze([node]));
  127. if (cd === REMOVE)
  128. node.contents = null;
  129. }
  130. else
  131. await visitAsync_(null, node, visitor_, Object.freeze([]));
  132. }
  133. // Without the `as symbol` casts, TS declares these in the `visit`
  134. // namespace using `var`, but then complains about that because
  135. // `unique symbol` must be `const`.
  136. /** Terminate visit traversal completely */
  137. visitAsync.BREAK = BREAK;
  138. /** Do not visit the children of the current node */
  139. visitAsync.SKIP = SKIP;
  140. /** Remove the current node */
  141. visitAsync.REMOVE = REMOVE;
  142. async function visitAsync_(key, node, visitor, path) {
  143. const ctrl = await callVisitor(key, node, visitor, path);
  144. if (identity.isNode(ctrl) || identity.isPair(ctrl)) {
  145. replaceNode(key, path, ctrl);
  146. return visitAsync_(key, ctrl, visitor, path);
  147. }
  148. if (typeof ctrl !== 'symbol') {
  149. if (identity.isCollection(node)) {
  150. path = Object.freeze(path.concat(node));
  151. for (let i = 0; i < node.items.length; ++i) {
  152. const ci = await visitAsync_(i, node.items[i], visitor, path);
  153. if (typeof ci === 'number')
  154. i = ci - 1;
  155. else if (ci === BREAK)
  156. return BREAK;
  157. else if (ci === REMOVE) {
  158. node.items.splice(i, 1);
  159. i -= 1;
  160. }
  161. }
  162. }
  163. else if (identity.isPair(node)) {
  164. path = Object.freeze(path.concat(node));
  165. const ck = await visitAsync_('key', node.key, visitor, path);
  166. if (ck === BREAK)
  167. return BREAK;
  168. else if (ck === REMOVE)
  169. node.key = null;
  170. const cv = await visitAsync_('value', node.value, visitor, path);
  171. if (cv === BREAK)
  172. return BREAK;
  173. else if (cv === REMOVE)
  174. node.value = null;
  175. }
  176. }
  177. return ctrl;
  178. }
  179. function initVisitor(visitor) {
  180. if (typeof visitor === 'object' &&
  181. (visitor.Collection || visitor.Node || visitor.Value)) {
  182. return Object.assign({
  183. Alias: visitor.Node,
  184. Map: visitor.Node,
  185. Scalar: visitor.Node,
  186. Seq: visitor.Node
  187. }, visitor.Value && {
  188. Map: visitor.Value,
  189. Scalar: visitor.Value,
  190. Seq: visitor.Value
  191. }, visitor.Collection && {
  192. Map: visitor.Collection,
  193. Seq: visitor.Collection
  194. }, visitor);
  195. }
  196. return visitor;
  197. }
  198. function callVisitor(key, node, visitor, path) {
  199. if (typeof visitor === 'function')
  200. return visitor(key, node, path);
  201. if (identity.isMap(node))
  202. return visitor.Map?.(key, node, path);
  203. if (identity.isSeq(node))
  204. return visitor.Seq?.(key, node, path);
  205. if (identity.isPair(node))
  206. return visitor.Pair?.(key, node, path);
  207. if (identity.isScalar(node))
  208. return visitor.Scalar?.(key, node, path);
  209. if (identity.isAlias(node))
  210. return visitor.Alias?.(key, node, path);
  211. return undefined;
  212. }
  213. function replaceNode(key, path, node) {
  214. const parent = path[path.length - 1];
  215. if (identity.isCollection(parent)) {
  216. parent.items[key] = node;
  217. }
  218. else if (identity.isPair(parent)) {
  219. if (key === 'key')
  220. parent.key = node;
  221. else
  222. parent.value = node;
  223. }
  224. else if (identity.isDocument(parent)) {
  225. parent.contents = node;
  226. }
  227. else {
  228. const pt = identity.isAlias(parent) ? 'alias' : 'scalar';
  229. throw new Error(`Cannot replace node with ${pt} parent`);
  230. }
  231. }
  232. exports.visit = visit;
  233. exports.visitAsync = visitAsync;