0.0.11 • Published 10 years ago

astquery v0.0.11

Weekly downloads
402
License
MIT
Repository
github
Last release
10 years ago

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

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

10 years ago

0.0.10

10 years ago

0.0.9

10 years ago

0.0.8

10 years ago

0.0.7

10 years ago

0.0.6

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago