Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
call-matcher
Advanced tools
ECMAScript CallExpression matcher made from function/method signature
ECMAScript CallExpression matcher made from function/method signature
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.
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
.
an object
for configuration options. If not passed, default options will be used.
type | default value |
---|---|
object | (return value of estraverse.VisitorKeys ) |
VisitorKeys for AST traversal. See estraverse.VisitorKeys and babel.types.VISITOR_KEYS.
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'],
...
Tests whether node
matches the signature or not.
true
if matched.false
if not matched.node
should be an AST node object defined in The ESTree Spec (formerly known as Mozilla SpiderMonkey Parser API).
Returns match result object representing whether node
(and its parentNode
) matches some argument of the signature or not.
null
if not matched.{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).
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
).
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
$ npm install --save call-matcher
See CHANGELOG
Licensed under the MIT license.
2.0.0 (2019-01-21)
We drop support of ancient (= before ES6) environments. Please use polyfills by your own.
We stopped providing prebuilt bundle for browsers. Please build your own by your bundler.
FAQs
ECMAScript CallExpression matcher made from function/method signature
The npm package call-matcher receives a total of 0 weekly downloads. As such, call-matcher popularity was classified as not popular.
We found that call-matcher demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.