New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

astquery

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

astquery

Use css-like selectors for walking over AST-tree

latest
Source
npmnpm
Version
0.0.11
Version published
Weekly downloads
6.4K
-1.12%
Maintainers
1
Weekly downloads
 
Created
Source

ASTQuery

Use css-like selectors for walking over AST-tree

Example

var ast = esprima.parse('function test(){ var a, b = 0; a = 1; a += 2; b++; b--; };test();');
var astQuery = new ASTQuery(ast, 'es5');
astQuery.on({
	print: function(node, message) {
		console.log('node type is ' + node.type + ' | ' + message)
	}

	, ':: FunctionDeclaration': function(node) {
		this.print(node, '1| ');
	}

	, ':: VariableDeclarator': function(node) {
		this.print(node, '2| ' + node.id.name);
	}

	, ':: AssignmentExpression[operator="="]': function(node) {
		this.print(node, '3| ' + node.left.name + "|" + node.operator);
	}

// TODO::
//	, ':: UpdateExpression[operator="++"]': function(node) {
//		this.print(node, '4| ' + node.argument.name + "|" + node.operator);
//	}

// TODO::
//	, ':: :other': function(node) {
//		console.log('  untouched node type is ' + node.type);
//	}
}, {prefix: '::'});

astQuery.apply();

// console output is:
// > node type is FunctionDeclaration | 1|
// > node type is VariableDeclarator | 2| a
// > node type is VariableDeclarator | 2| b
// > node type is AssignmentExpression | 3| a|=

TODO

  • ':other' pseudo-class
  • complex selector's support ('tag>tag')
  • classes support with API to adding class description
  • es6 'query' string template tag (astQuery.queryVariableDeclarator:${function(node){ return node.id.name == 'a' }})
  • 'removeListener'

Keywords

ast-alter

FAQs

Package last updated on 04 Apr 2014

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