npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ast-utils

v1.0.0

Published

Utility library to manipulate ASTs

Downloads

3,407

Readme

ast-utils Build Status

Utility library to manipulate ASTs

Install

$ npm install --save ast-utils

Usage

const astUtils = require('ast-utils');

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 an Identifier named require
  • node has exactly 1 Literal argument whose value is a string

Example:

require('lodash');
// => true
require(foo);
// => false
foo('lodash');
// => false

Usage example (in the context of an ESLint rule):

function create(context) {
	return {
		CallExpression(node) {
			if (astUtils.isStaticRequire(node)) {
				context.report({
					node: node,
					message: 'Use import syntax rather than `require`'
				});
			}
		}
	};
}

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:

require('lodash');
// => 'lodash'
require('./foo');
// => './foo'

Usage example (in the context of an ESLint rule):

function create(context) {
	return {
		CallExpression(node) {
			if (astUtils.isStaticRequire(node) && astUtils.getRequireSource(node) === 'underscore') {
				context.report({
					node: node,
					message: 'Use `lodash` instead of `underscore`'
				});
			}
		}
	};
}

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 named name inside of node
  • That Identifier is a variable (i.e. not a static property name for instance)
  • That Identifier does not reference a different variable named name introduced in a sub-scope of node.

Example:

foo(a);
// containsIdentifier('a', node) // => true
// containsIdentifier('b', node) // => true

function foo(fn) {
	return function(a) {
		return fn(a);
	};
}
// containsIdentifier('a', node) // => false

Usage example (in the context of an ESLint rule):

function create(context) {
	return {
		FunctionDeclaration(node) {
			node.params.forEach(param => {
				if (param.type === 'Identifier' && !astUtils.containsIdentifier(param.name, node.body)) {
					context.report({
						node: node,
						message: `${name} is never used`
					});
				}
			});
		}
	};
}

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].some(node => astUtils.containsIdentifier('a', node));
// equivalent to
astUtils.someContainIdentifier('a', [node1, node2, node3]);

License

MIT © Jeroen Engels