JSPM

ast-test

1.1.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 74
  • Score
    100M100P100Q61250F
  • License Unlicensed

Test ast node on some condition

Package Exports

  • ast-test

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ast-test) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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