Utility library to manipulate ASTs for ESLint projects
Install
$ npm install --save eslint-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 aCallExpression
node
's callee is anIdentifier
namedrequire
node
has exactly 1Literal
argument whose value is astring
Example:
;// => true;// => false;// => false
Usage example:
{ 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:
{ 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:
{ 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;
astUtils.getPropertyName(node)
Get the name of a MemberExpression
's property. Returns:
- a
string
if the property is accessed through dot notation. - a
string
if the property is accessed through brackets and is a string. - a
number
if the property is accessed through brackets and is a number. undefined
ifnode
is not aMemberExpression
undefined
if the property name is a hard to compute expression.
Example:
foobar// => 'bar'foo'bar'// => 'bar'foobar// => undefinedfoo0// => 0 # Numberfoonull// => nullfooundefined// => undefined
Usage example:
{ return { if astUtils context; } ;}
astUtils.computeStaticExpression(node)
Get the value of an expression that can be statically computed, i.e. without variables references or expressions too complex.
Returns:
undefined
if the value could not be statically computed.- An object with a
value
property containing the computed value.
Example:
foo// => undefined42// => {value: 42}'foo'// => {value: 'foo'}undefined// => {value: undefined}null// => {value: null}1 + 2 - 4 + -1// => {value: -2}true ? 1 : 2// => {value: 1}`foo `// => {value: 'foo bar'}
Usage example:
{ return { const expression = astUtils; if expression context; } ;}
astUtils.isPromise(node)
Checks whether node
is a Promise.
Returns true
if and only if node
is one of the following:
- a call of an expression's
then
orcatch
properties - a call to a property of
Promise
(exceptcancel
,promisify
,promisifyAll
andis
) - a call to
new Promise
If node
uses unknown properties of a value that would be considered a Promise, node
itself would not be considered as a Promise.
Example:
foo;// => truefoo;// => truefoo;// => truefoo; // isFulfilled(fn) may not return a Promise// => false Promise;// => truePromise;// => truePromise;// => truePromiseallpromises;// => truePromise; // Bluebird method// => true fn;// => truevalue;// => false
Usage example:
{ { if astUtils context; } return CallExpression: reportIfPromise NewExpression: reportIfPromise ;}
astUtils.isFunctionExpression(node)
Checks whether node
is a function expression or an arrow function expression (not a function declaration).
If node
uses unknown properties of a value that would be considered a Promise, node
itself would not be considered as a Promise.
Example:
{}// => true {}// => true {} // function declaration// => false
Usage example:
{ return { if nodecalleetype === 'Identifier' && nodecalleename === 'test' && !astUtils && !astUtils context; } ;}
License
MIT © Jeroen Engels