123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- (function () {
- 'use strict';
- var estraverse = require('estraverse');
- function isNode(node) {
- if (node == null) {
- return false;
- }
- return typeof node === 'object' && typeof node.type === 'string';
- }
- function isProperty(nodeType, key) {
- return (nodeType === estraverse.Syntax.ObjectExpression || nodeType === estraverse.Syntax.ObjectPattern) && key === 'properties';
- }
- function Visitor(visitor, options) {
- options = options || {};
- this.__visitor = visitor || this;
- this.__childVisitorKeys = options.childVisitorKeys
- ? Object.assign({}, estraverse.VisitorKeys, options.childVisitorKeys)
- : estraverse.VisitorKeys;
- if (options.fallback === 'iteration') {
- this.__fallback = Object.keys;
- } else if (typeof options.fallback === 'function') {
- this.__fallback = options.fallback;
- }
- }
-
- Visitor.prototype.visitChildren = function (node) {
- var type, children, i, iz, j, jz, child;
- if (node == null) {
- return;
- }
- type = node.type || estraverse.Syntax.Property;
- children = this.__childVisitorKeys[type];
- if (!children) {
- if (this.__fallback) {
- children = this.__fallback(node);
- } else {
- throw new Error('Unknown node type ' + type + '.');
- }
- }
- for (i = 0, iz = children.length; i < iz; ++i) {
- child = node[children[i]];
- if (child) {
- if (Array.isArray(child)) {
- for (j = 0, jz = child.length; j < jz; ++j) {
- if (child[j]) {
- if (isNode(child[j]) || isProperty(type, children[i])) {
- this.visit(child[j]);
- }
- }
- }
- } else if (isNode(child)) {
- this.visit(child);
- }
- }
- }
- };
-
- Visitor.prototype.visit = function (node) {
- var type;
- if (node == null) {
- return;
- }
- type = node.type || estraverse.Syntax.Property;
- if (this.__visitor[type]) {
- this.__visitor[type].call(this, node);
- return;
- }
- this.visitChildren(node);
- };
- exports.version = require('./package.json').version;
- exports.Visitor = Visitor;
- exports.visit = function (node, visitor, options) {
- var v = new Visitor(visitor, options);
- v.visit(node);
- };
- }());
|