Merge multiple visitors for estraverse into one
const mergedVisitor = mergeVisitors(arrayOfVisitors)
npm install merge-estraverse-visitors
For given code,
function tenTimes (cb) {
for (var i = 0; i < 10; i += 1) {
cb();
}
}
Merge multiple estraverse visitors into one then run against target AST.
const visitor1 = {
enter: function (currentNode, parentNode) {
switch(currentNode.type) {
case 'ForStatement':
console.log('v1: going to skip ' + currentNode.type);
this.skip();
break;
case 'CallExpression':
case 'FunctionDeclaration':
console.log('v1: entering ' + currentNode.type);
break;
}
return undefined;
},
leave: function (currentNode, parentNode) {
switch(currentNode.type) {
case 'ForStatement':
case 'CallExpression':
case 'FunctionDeclaration':
console.log('v1: leaving ' + currentNode.type);
break;
}
}
};
const visitor2 = {
enter: function (currentNode, parentNode) {
switch(currentNode.type) {
case 'ForStatement':
case 'CallExpression':
case 'FunctionDeclaration':
console.log('v2: entering ' + currentNode.type);
break;
}
},
leave: function (currentNode, parentNode) {
switch(currentNode.type) {
case 'ForStatement':
case 'CallExpression':
case 'FunctionDeclaration':
console.log('v2: leaving ' + currentNode.type);
break;
}
}
};
const mergeVisitors = require('merge-estraverse-visitors');
const estraverse = require('estraverse');
const { parse } = require('acorn');
const ast = parse(code);
const mergedVisitor = mergeVisitors([ visitor1, visitor2 ])
estraverse.traverse(ast, mergedVisitor);
Results in:
v1: entering FunctionDeclaration
v2: entering FunctionDeclaration
v1: going to skip ForStatement
v2: entering ForStatement
v2: entering CallExpression
v2: leaving CallExpression
v2: leaving ForStatement
v1: leaving ForStatement
v2: leaving FunctionDeclaration
v1: leaving FunctionDeclaration
Supports Node under maintenance. In other words, we stop supporting old Node versions when their maintenance ends. Any other environments are not supported officially (means that we do not test against them on CI service).
Licensed under the MIT license.