Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

escallmatch

twada93.3kMIT1.5.0

ECMAScript CallExpression matcher made from function/method signature

ecmascript, ast

readme

escallmatch

ECMAScript CallExpression matcher made from function/method signature

Build Status NPM version Dependency Status License

EXAMPLE

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

Then match against path/to/some_test.js.

var escallmatch = require('escallmatch');
var esprima = require('esprima');
var estraverse = require('estraverse');
var fs = require('fs');

var matcher = escallmatch('assert.equal(actual, expected, [message])');

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)

escallmatch is a spin-off product of power-assert project.

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

API

var matcher = escallmatch(signatureStr, [options])

Create matcher object for a given function/method signature string.

var matcher = escallmatch('assert.equal(actual, expected, [message])');

Any arguments 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 Mozilla JavaScript AST spec.

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 {name: 'actual', kind: 'mandatory'}, whose name is an argument name in the signature and kind is 'mandatory' or 'optional'.

node and parentNode should be AST node objects defined in Mozilla JavaScript AST spec.

var calleeAst = matcher.calleeAst()

Returns clone of callee AST object based on signature passed to escallmatch function. Returned tree is one of AST node objects defined in Mozilla JavaScript AST spec (in most cases, Identifier or MemberExpression).

var argSigs = matcher.argumentSignatures()

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

INSTALL

via npm

Install

$ npm install --save escallmatch

via bower

Install

$ bower install --save escallmatch

Then load (escallmatch function is exported)

<script type="text/javascript" src="./path/to/bower_components/escallmatch/build/escallmatch.js"></script>

CHANGELOG

See CHANGELOG

AUTHOR

LICENSE

Licensed under the MIT license.

changelog

1.5.0 (2016-08-21)

1.4.2 (2015-06-05)

  • update espurify to 1.3.0 (0d3a925)
  • use licensify to prepend license header (e4a6981)

1.4.1 (2015-05-22)

1.4.0 (2015-04-27)

Features

1.3.2 (2015-04-24)

1.3.1 (2015-04-17)

1.3.0 (2015-04-15)

  • update esprima and estraverse (ea7006d2)

1.2.0 (2015-04-12)

Features

1.1.0 (2015-02-25)

Features

  • escallmatch:
    • use semantic versioning to resolve dependencies (3a7c0b40)
    • update deep-equal to 1.0.0 (7879a92d)
    • run CI on Node 0.12 and io.js (ce671bfc)
    • ship npm module with built bundle for browsers (e96bc59d)

1.0.1 (2014-11-27)

Bug Fixes

  • escallmatch: avoid using ES3 reserved words. (58eeeaaf, closes #1)

1.0.0 (2014-11-04)

The first stable release.

0.3.1 (2014-08-18)

Bug Fixes

  • escallmatch: dealing with optional parameters in the middle (05faf341)

0.3.0 (2014-08-08)

Bug Fixes

  • escallmatch: fix silly misspelling (f5cedac8)

Features

  • escallmatch: make callee comparison faster (aaf16312)

Breaking Changes

  • argumentSignitures method is renamed to argumentSignatures

To migrate, change your code from the following:

matcher.argumentSignitures()

To:

matcher.argumentSignatures()

(f5cedac8)

0.2.0 (2014-08-04)

Features

  • escallmatch:
    • argumentSignitures method that returns argument specs in signiture (2c2478a6)
    • calleeAst method that returns clone of callee in signiture (0df6baed)

0.1.1 (2014-08-01)

0.1.0 (2014-07-31)

Features

  • escallmatch:
    • ensure API definition is in the form of CallExpression (2aea209a, fb53e1e3)
    • ensure arguments are in the form of name or [name] (8f85a422, bf14aa5d, 5bdb3f6e)
    • ensure argument names are unique (df15c138)
    • matchArgument returns result object containing name and kind (71559f68)
    • optional parameter matching (a2ae9e1c, 86a1fb78)
    • without optional parameters, argument count should be an exact match (eb750b27)
    • matchArgument returns corresponding identifier name in example (10c9eb0e)
    • count arguments length to detect missing / optional parameters (0e24e84e)
    • calculate AST depth first (1024f7c3)
    • Matcher#test to match whole CallExpression (b9e3d2e0)
    • use new espurify module to compare two AST trees (e0cbe00d)
    • simple implementation using deep-equal (80de163d)