merge-estraverse-visitors

2018-09-20 admin

merge-estraverse-visitors是什么

什么是merge-estraverse-visitors,merge multiple estraverse visitors into one

merge-estraverse-visitors使用教程帮助文档

merge-estraverse-visitors

Merge multiple visitors for estraverse into one

Build Status NPM version License

API

var mergedVisitor = mergeVisitors(arrayOfVisitors)

INSTALL

npm install merge-estraverse-visitors

USAGE

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.

var 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;
        }
    }
};

var 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;
        }
    }
};

var mergeVisitors = require('merge-estraverse-visitors');
var estraverse = require('estraverse');
var acorn = require('acorn');
var ast = acorn.parse(code);
estraverse.traverse(ast, mergeVisitors([ visitor1, visitor2 ]));

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

AUTHOR

LICENSE

Licensed under the MIT license.

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-40750.html

文章标题:merge-estraverse-visitors

回到顶部