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

ast-finder

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ast-finder

AST finder utility.

  • 0.2.0
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source
require('ast-finder');

function myFunction(module){
    var foo = true;
 
    module.exports = foo;  
};

var myFunctionString = myFunction.toString(); // function body to string
 
var parsed = AstFinder.parse(myFunctionString); //parse to ast
 
var batch = AstFinder.find(parsed,[{
    conditions : {
        'body.operator' : '=',
        'body.left.expression.name' : 'module',
        'body.left.property': 'exports'
    },
    type : AstFinder.AST_TYPES.SIMPLE_STATEMENT,
    resultType : AstFinder.SELECTOR.MULTIPLE
}]); // search through ast for exports =
 
batch.get(0).each(function(node){
    var $scope = node.$prev; // parent function scope

    //replace module.exports with a return
    $scope.body[1] = AstFinder.factory('AST_Return',{
        value : node.body.right
    });
}); //loop through results of the first search requirements and transform if needed
 
console.log(AstFinder.toString(parsed)); //convert ast back to code string

Keywords

FAQs

Package last updated on 18 May 2015

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