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

eslint-traverser

v1.5.2

Published

A utility that helps traverse code the way ESLint does

Downloads

504

Readme

Build Status Coverage Status NPM version

eslint-traverser

A utility that helps traverse code the way ESLint does. This allows you to test any utilities you write for ESLint rules.

Installation

npm install eslint-traverser

Usage

Common Usage

The module exposes a function that gets code and an optional config object, and gets a traverser, with a get function. The get function calls the callback for every node of the type, with node and context parameters, which are the same as the ones in ESLint itself.

const traverse = require('eslint-traverser')

traverse('var y = f(x)')
    .get('CallExpression', (node, context) => {
      console.log(node.callee.name) //logs `f`
      sourceCode = context.getSourceCode()
      //...
    })

Using configs

You can define a configuration to run your traversal (globals, settings, etc) in an additional parameter in the call to traverser. You may not use the rules key in this configuration.

const traverse = require('eslint-traverser')

traverse('import foo from "bar"', {parserOptions: {sourceType: module}})
    .get('Program:exit', (node, context) => {
      console.log('Modules!')
    })

As a shortcut, you can pass only the parserOptions object, and if your config only contains parserOptions keys, it will work normally.

const traverse = require('eslint-traverser')

traverse('import foo from "bar"', {sourceType: module})
    .get('Program:exit', (node, context) => {
      console.log('Modules!')
    })

Using a selector

You can also filter results using a selector, which can be a function or any of the iteratees supplied by Lodash

const traverse = require('eslint-traverser')

traverse('f(); g(x)')
    .get('CallExpression', node => node.arguments.length, (node, context) => {
      console.log(node.callee.name) //logs `g`
      //...
    })
    
traverse('f(); g(x)')
    .get('CallExpression', 'arguments.length', node => {
      console.log(node.callee.name) //logs `g`
      //...
    })
    
traverse('f(); g(x)')
    .get('CallExpression', ['arguments.length', 0], node => {
      console.log(node.callee.name) //logs `f`
      //...
    })
    
traverse('f(); g(x)')
    .get('CallExpression', {callee: {name: 'g'}}, node => {
      console.log(node.callee.name) //logs `g`
      //...
    })    

Using the first method

The first method is the same as the get method, except it only calls the callback on the first result, while get calls it on all the results

const traverse = require('eslint-traverser')
traverse('f(); g()')
    .get('CallExpression', node => {
      console.log(node.callee.name) 
    })
// f
// g

traverse('f(); g()')
    .first('CallExpression', node => {
      console.log(node.callee.name)
    })
// f

Using the visitAll method

The visitAll method gets two arguments: first, an optional visitors object in the same structure as an ESLint rule visitor object. The second is a callback, that gets called at the end of the code traversal with two arguments: the Program node and the context.

const traverse = require('eslint-traverser')
traverse('var y = f(x)')
    .visitAll((node, context) => {
      console.log(node.type) //Program
      
    })

traverse('var y = f(x)')
    .visitAll({
      CallExpression(node) { console.log('Call expression!')}
    }, () => { console.log('finished!')})
// Call expression!
// finished!

Using the runRuleCode method

The runRuleCode method gets one argument: the rule. This runs an ESLint rule (a function that gets a context and returns a visitors object) on the specified code.

const traverse = require('eslint-traverser')
traverse('var y = f(x)')
    .runRuleCode(context => ({
      CallExpression(node) { console.log('Call expression!')}
    }))
// Call expression!