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-scout

v1.0.5

Published

Search for information in an abstract sysntax tree

Downloads

16

Readme

ast-scout

Search abstract syntax tree (Babylon AST) and report mathing paths.

Installation

npm i ast-scout --save

Usage

This is a tool to search AST for objects on basis of a string of code. AstScout can be useful for collecting info from your codebase.

From the scout's search definition ast-scout creates internally (with help of Babel) a visitor object tree where each visitor is, if required from the provided match definitions, decorated with additional logic to report back specific AST paths.

example

const findPaths = require('ast-scout').findPaths;
const traverse = require('@babel/traverse').default;
const generate = require('@babel/generator').default;
const parser = require('@babel/parser');

const code = `
  import { bla } from 'common/utils';

  function foo() {
    bla('Hello world!');
  }

  function foo2() {
    bla('Hello earth!');
  }

  function foo3() {
    bla(KEY_VALUE);
  }
`;

const babelConfig = {
  sourceType: 'unambiguous',
  plugins: ['jsx', 'decorators-legacy', 'classProperties', 'objectRestSpread'],
}

const ast = parser.parse(code, babelConfig);

const scout = {
  search: 'bla()',
  match: [{
    search: 'bla',
    marked: true,
  }]
};

traverse(ast, {
  Program: function programVisitor(path) {
    const { searchPaths, matchPaths } = findPaths(path, scout, babelConfig);

    console.log('searchPaths', searchPaths.length);
    console.log('matchPaths', matchPaths.length);
    matchPaths.forEach( path => {
      console.log(`match type: ${path.parentPath.node.arguments[0].type}`);
      console.log(`      code: ${generate(path.parentPath.node.arguments[0]).code}`);
    });
  }
});

result

searchPaths 3
matchPaths 3
match type: StringLiteral
      code: 'Hello world!'
match type: StringLiteral
      code: 'Hello earth!'
match type: Identifier
      code: KEY_VALUE

API

findPaths

findPaths(path, scout, babelConfig);

Returns an object with Babel path results for searchPaths and matchPaths.

Path

object - Babel path object.

Scout

string|object - Defines search, matching and path reporting requirements. See below for more scout examples.

BabelConfig

object - Internally babel parser is used for creating a nested visitors structrure. Passing this config ensures the AST created from the search parameter is in line with the AST created from the code you want to analyse.

Scout Examples

Simple search

In the returned searchPaths all results found for CallExpression paths with the exact match of getMyString(welcomeMessageKey) are listed.

const scout = 'getMyString(welcomeMessageKey)'

Search with regExpr defined in match

In the returned searchPaths all results found for ImportDeclaration paths with the exact match of import { getMyString } from \'./common/utils\ are listed.

In the returned matchPaths all child paths are listed for Identifier getMyString and in addition for Identifier getAllMyStrings.

To instruct ast-scout to include the matching child node in the matchPaths results use: marked: true.

const scout = {
  search: 'import { getMyString } from \'./common/utils\';',
  match: [{
    search: 'getMyString',
    regExpr: /^getMyString$|^getAllMyStrings$/,
    marked: true
  }, {
    search: './common/utils',
    regExpr: /common\/utils$/
  }],
}

Search with context and startType

Searching for jsx attribute declarations without providing the containing jsx container in the search term would lead us to results of object definition types instead. Therefore we can define a search context (the jsx attribute definition within div tags) and instruct the ast-scout process to start the visitor object tree at the JSXAttribute node.

In the returned searchPaths all paths found for the exact match of key={previousSearchTerm} are listed.

In the returned matchPaths all child paths are listed for Identifier previousSearchTerm.

const scout = {
  search: {
    context: '<div key={previousSearchTerm} />',
    startType: 'JSXAttribute'
  },
  match: [{
    search: 'previousSearchTerm',
    marked: true
  }],
}