ast-utils
Utility library to manipulate ASTs
Install
$ npm install --save ast-utils
Usage
const astUtils = ;
API
astUtils.isStaticRequire(node)
Checks whether node
is a call to CommonJS's require
function.
Returns true
if and only if:
- node is a
CallExpression
node
's callee is anIdentifier
namedrequire
node
has exactly 1Literal
argument whose value is astring
Example:
;// => true;// => false;// => false
Usage example (in the context of an ESLint rule):
{ return { if astUtils context; } ;}
astUtils.getRequireSource(node)
Gets the source of a require()
call. If node
is not a require
call (in the definition of isStaticRequire
), it will return undefined
.
Example:
;// => 'lodash';// => './foo'
Usage example (in the context of an ESLint rule):
{ return { if astUtils && astUtils === 'underscore' context; } ;}
astUtils.containsIdentifier(name, node)
Checks if there is a reference to a variable named name
inside of node
.
Returns true if and only if:
- There is an
Identifier
namedname
inside ofnode
- That
Identifier
is a variable (i.e. not a static property name for instance) - That
Identifier
does not reference a different variable namedname
introduced in a sub-scope ofnode
.
Example:
;// containsIdentifier('a', node) // => true// containsIdentifier('b', node) // => true { return { return ; };}// containsIdentifier('a', node) // => false
Usage example (in the context of an ESLint rule):
{ return { nodeparams; } ;}
astUtils.someContainIdentifier(name, nodes)
Checks if there is a reference to a variable named name
inside any node of the nodes
array. Will return false
if nodes
is not an array.
This is a shorthand version of containsIdentifier
that works for arrays. The following are equivalent:
node1 node2 node3;// equivalent toastUtils;
License
MIT © Jeroen Engels