Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

merge-estraverse-visitors

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

merge-estraverse-visitors

merge multiple estraverse visitors into one

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

merge-estraverse-visitors

Merge multiple visitors for estraverse into one

Build Status NPM version Code Style License

API

const 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.

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

AUTHOR

SUPPORT POLICY

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).

LICENSE

Licensed under the MIT license.

Keywords

FAQs

Package last updated on 18 May 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc