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

compact-js-ast

v4.2.3

Published

Outputs a compact string representation of the espree AST of a JS input program

Downloads

13

Readme

Synopsis

Outputs a compact string representation of the espree AST of an input program. It provides an executable compast that can be used to output a summarized version of the AST of a given JavaScript program. The output can be in YML or JSON format. The program can be given as a file or as a string in the command line.

$ compast -h                    
Usage: compast [options] [filename]

Converts a JS program into a JSON or YML AST format

Arguments:
  filename                    file with the original code

Options:
  -V, --version               output the version number
  -o, --output <filename>     file name of the json output program
  -p, --program <JS program>  JS program is given in the command line
  -jw --whites <string>       string '  ' Specifies the number of whites for formatting the object (default: "  ")
  -e --hide <fieldnames...>   List of AST fields to omit (default: [])
  -f --hideFile <fileName>    File with a line per AST fields to omit
  -j --json                   output in JSON format (default is YML
  -n --no-parse               do not parse the code, assume the input is already an AST in json format
  -b --babel                  parse the code with babel (default is espree)
  -a --all                    output all fields
  -l --location               omit only location fields
  -h, --help                  display help for command

Install

npm i -g compact-js-ast

Examples

Here is an example of the YML output of the AST for the program a.b(4):

$ compast -p 'a.b(4)'           
type: "Program"
body:
  - type: "ExpressionStatement"
    expression:
      type: "CallExpression"
      callee:
        type: "MemberExpression"
        object:
          type: "Identifier"
          name: "a"
        property:
          type: "Identifier"
          name: "b"
      arguments:
        - type: "Literal"
          value: 4
$ compast -jp 'a.b(4)'
{
  "type": "Program",
  "body": [
    {
      "type": "ExpressionStatement",
      "expression": {
        "type": "CallExpression",
        "callee": {
          "type": "MemberExpression",
          "object": {
            "type": "Identifier",
            "name": "a"
          },
          "property": {
            "type": "Identifier",
            "name": "b"
          }
        },
        "arguments": [
          {
            "type": "Literal",
            "value": 4
          }
        ]
      }
    }
  ]
}

Same, but removing namess and values:

$ compast -e name value -p 'a.b(4)' 
type: "Program"
body:
  - type: "ExpressionStatement"
    expression:
      type: "CallExpression"
      callee:
        type: "MemberExpression"
        object:
          type: "Identifier"
        property:
          type: "Identifier"
      arguments:
        - type: "Literal"

Using babel:

➜ nicolo-howto-talk git:(main) compast -blp 'a?.[0]' | yq '.program.body[0]'

{
  "type": "ExpressionStatement",
  "expression": {
    "type": "OptionalMemberExpression",
    "object": {
      "type": "Identifier",
      "name": "a"
    },
    "computed": true,
    "property": {
      "type": "NumericLiteral",
      "extra": {
        "rawValue": 0,
        "raw": "0"
      },
      "value": 0
    },
    "optional": true
  }
}

See also

  • astexplorer.net is a web-based tool for exploring the AST of a JavaScript program. It provides a visual representation of the AST and allows to explore the different nodes and their properties.
  • jq is a lightweight and flexible command-line JSON processor. It can be used to filter and transform the output of compast to obtain a more compact representation of the AST.
  • yq is a YAML version of jq
  • jless is a command-line JSON viewer that can be used to visualize the output of compast in a more readable format.