Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

ast-test

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

ast-test

Test ast node on some condition

latest
Source
npmnpm
Version
1.1.1
Version published
Maintainers
1
Created
Source

ast-test Build Status unstable

Test source tree on passing some condition.

Use

$ npm install --save ast-test

var parse = require('esprima').parse;
var test = require('ast-test');

//catch all `foo` assignments
var rule = {
	AssignmentExpression: function (node) {
		if (node.operator !== '=') return false;
		return node.left.name === 'foo';
	}
};

test(parse('foo = 1;').body[0], rule) //true
test(parse('var foo = 1;').body[0], rule) //false

API

test(Node, testRules) → Boolean

Test whether node passes test rules passed. testRules is an object containing node types as keys and testing functions as values. Matched nodes are tested with according testing functions, and if matched none — test returns false. You can declare node supertypes to match, in that case they will be checked beforehead.

var rule = {
	Expression: function (node) {
		return true;
	},
	AssignmentExpression: function (node) {
		if (node.operator !== '=') return false;
		return node.left.name === 'foo';
	}
};

test(parse('foo = 1;').body[0].expression, rule); //true
test(parse('bar = 1;').body[0].expression, rule); //false
test(parse('var foo = 1;').body[0], rule); //false

NPM

Keywords

ast

FAQs

Package last updated on 19 Feb 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