npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

call-matcher

v2.0.0

Published

ECMAScript CallExpression matcher made from function/method signature

Downloads

669,680

Readme

call-matcher

ECMAScript CallExpression matcher made from function/method signature

Build Status NPM version Code Style License

EXAMPLE

Creating CallExpression matcher for method signature 'assert.equal(actual, expected, [message])'.

Then match against path/to/some_test.js.

var CallMatcher = require('call-matcher');
var esprima = require('esprima');
var estraverse = require('estraverse');
var fs = require('fs');

var ast = esprima.parse('assert.equal(actual, expected, [message])');
var expression = ast.body[0].expression;
var matcher = new CallMatcher(expression);

estraverse.traverse(esprima.parse(fs.readFileSync('path/to/some_test.js')), {
    enter: function (currentNode, parentNode) {
        if (matcher.test(currentNode)) {
            // currentNode is a CallExpression that matches to the signature
        }
        var argMatched = matcher.matchArgument(currentNode, parentNode);
        if (argMatched) {
            if (argMatched.kind === 'mandatory') {
                // mandatory arg (in this case, `actual` or `expected`)
            } else if (argMatched.kind === 'optional') {
                // optional arg (in this case, `message`)
            }
        }
    }
});

where content of path/to/some_test.js is:

var assert = require('assert');
var anotherAssert = assert;
var equal = assert.equal.bind(assert);
var foo = '2';
var bar = 2;

assert.equal(foo, bar);  // matches
assert.equal(bar, foo);  // matches
assert.equal(foo, bar, 'foo shoule be equal to bar');  // matches (with optional arg)

assert.equal();  // does not match (less args)
assert.equal(foo);  // does not match (less args)
assert.equal(foo, bar, 'hoge', 'fuga');  // does not match (too much args)

assert.notEqual(foo, bar);  // does not match (callee method name differs)
anotherAssert.equal(foo, bar);  // does not match (callee object name differs)
equal(foo, bar);  // does not match (callee does not match)

call-matcher is a spin-off product of power-assert project.

Pull-requests, issue reports and patches are always welcomed.

API

var matcher = new CallMatcher(signatureAst, [options])

Create matcher object for a given expression.

var ast = esprima.parse('assert.equal(actual, expected, [message])');
var expression = ast.body[0].expression;
var matcher = new CallMatcher(expression);

Any signature string enclosed in bracket (for example, [message]) means optional parameters. Without bracket means mandatory parameters.

Returns matcher object having four methods, test, matchArgument, calleeAst, and argumentSignatures.

options

an object for configuration options. If not passed, default options will be used.

options.visitorKeys

| type | default value | |:---------|:--------------| | object | (return value of estraverse.VisitorKeys) |

VisitorKeys for AST traversal. See estraverse.VisitorKeys and babel.types.VISITOR_KEYS.

options.astWhiteList

| type | default value | |:---------|:--------------| | object | N/A |

Type and property whitelist on creating AST clone. astWhiteList is an object containing NodeType as keys and properties as values.

{
    ArrayExpression: ['type', 'elements'],
    ArrayPattern: ['type', 'elements'],
    ArrowFunctionExpression: ['type', 'id', 'params', 'body', 'generator', 'expression'],
    AssignmentExpression: ['type', 'operator', 'left', 'right'],
    ...

var isMatched = matcher.test(node)

Tests whether node matches the signature or not.

  • Returns true if matched.
  • Returns false if not matched.

node should be an AST node object defined in The ESTree Spec (formerly known as Mozilla SpiderMonkey Parser API).

var argMatched = matcher.matchArgument(node, parentNode)

Returns match result object representing whether node (and its parentNode) matches some argument of the signature or not.

  • Returns null if not matched.
  • If matched, returns object like {index: 0, name: 'actual', kind: 'mandatory'}, whose index is an index of matched argument, name is an argument name in the signature and kind is 'mandatory' or 'optional'.

node and parentNode should be AST node objects defined in The ESTree Spec (formerly known as Mozilla SpiderMonkey Parser API).

var calleeAst = matcher.calleeAst()

Returns clone of callee AST object based on signature passed to CallMatcher function. Returned tree is one of AST node objects defined in The ESTree Spec (formerly known as Mozilla SpiderMonkey Parser API) (in most cases, Identifier or MemberExpression).

var argSigs = matcher.argumentSignatures()

Returns array of argument signature objects based on signature passed to CallMatcher function. Returns array of objects like [{index: 0, name: 'actual', kind: 'mandatory'}], whose index is an index of matched argument, name is an argument name in the signature and kind is 'mandatory' or 'optional'.

INSTALL

via npm

Install

$ npm install --save call-matcher

CHANGELOG

See CHANGELOG

AUTHOR

CONTRIBUTORS

LICENSE

Licensed under the MIT license.