$ cnpm install esniff
Low footprint, fast source code parser, which allows you to find all code fragment occurrences with respect to all syntax rules that cannot be handled with plain regular expression search.
It aims at use cases where we need to quickly find usage of given function, property etc. in syntactically valid code.
$ npm install esniff
To port it to Browser or any other (non CJS) environment, use your favorite CJS bundler. No favorite yet? Try: Browserify, Webmake or Webpack
Using main module you can configure sophisticated parser on your own. However, first, see preprared API utilities that may already address use cases you have.
code
Code to parsetriggerChar
Character which is expected to trigger custom handling via callback
callback
To detect and eventually handle case we're afterExample: Find all require(..)
calls:
var esniff = require('esniff');
var result = esniff('var x = require(\'foo/bar\')', 'r', function (index, previous, nest) {
if (previous === '.') return next(); // Ignore x.require calls
if (code.indexOf('require', index) !== index) return esniff.next(); // Not really `require` call
next('require'.length); // Move after `require` and skip any following whitespace
index = esniff.index; // Update index
if (code[i] !== '(') return resume(); // Not `require(`
return collectNest(); // Collect all code between parenthesis
});
console.log(result); [{ point: 17, column: 17, line: 1, raw: '\'foo/bar\'' }]
Returns function which allows us to find all accessed property names on given object name
var findProperties = require('esniff/accessed-properties');
var findContextProperties = findProperties('this');
var result = findContextProperties('var foo = "0"; this.bar = foo; this.someMethod(); otherFunction()');
console.log(result); // [ { name: 'bar', start: 20, end: 23 }, { name: 'someMethod', start: 36, end: 46 } ]
Returns function which allows us to find all occurrences of given function (or method) being invoked
Through options we can restrict cases which we're after:
asProperty
(default: false
), on true will allow x.name()
when we search for name
callsasPlain
(default: true
), on true it allows plain calls e.g. name()
when we search for name
. Should be set to false
if we're strictly about method calls.Setting both asProperty
and asPlain
to false, will always produce empty result
var findRequires = require('esniff/function')('require');
findRequires('var x = require(\'foo/bar\')');
// [{ point: 17, column: 17, line: 1, raw: '\'foo/bar\'' }]
Resolves expressions separated with commas, with additional limit
you can specify after which number of arguments resolver should stop
var resolveArgs = require('esniff/resolve-arguments');
var result = resolveArgs('"raz", "dwa", [\'raz\', \'dwa\'], "trzy"', 3));
console.log(result); // ['"raz"', ' "dwa"', ' [\'raz\', \'dwa\']']
x = { foo: 'bar' } / 14
, esniff in that case will assume that /
starts regular expression). Still there's not known use case where such code may make any sense, and many popular JS source code parsers share very same vulnerability.$ npm test
Copyright 2013 - present © cnpmjs.org