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

strong-trace-scopenodes

v1.0.2

Published

Extract AST nodes from a js string that define scope

Downloads

4

Readme

strong-trace-scopenodes

Find the AST nodes from a string of JavaScript that define scope. E.g. The outer Program scope and functions.

Example

var scopenodes = require("strong-trace-scopenodes")
var fs = require("fs")

var filename = process.argv[2] || "./test/lib/nested.js"
var content = fs.readFileSync(filename).toString()
var out = scopenodes(content)
console.log(out)

/*
  [ { type: 'Program',
    body: [ [Object], [Object] ],
    range: [ 0, 116 ],
    loc: { start: [Object], end: [Object] },
    path: [],
    parent: null },
  { type: 'BlockStatement',
    body: [ [Object] ],
    range: [ 21, 69 ],
    loc: { start: [Object], end: [Object] },
    path: [ [Object] ],
    parent:
     { type: 'FunctionDeclaration',
       id: [Object],
       params: [Object],
  ...

*/

API

scopenodes(jsString)

Will return a list of nodes from an AST (Esprima) that define scoped blocks of the provided JavaScript string.

It will add some properties to each node:

  • fnName: An attempted guess at the function name. It should always be right if the function is named.
  • path: A list of outer scopes in order of scopechain for this node
  • parent: For a FunctionDeclaration or FunctionExpression node, the declaration or expression.
  • isStrict: a boolean value as to whether THIS SCOPE SPECIFICALLY has declared strict mode. Does not account for inherited strict mode.

Naming generally works like the following, and may change as we go:

  1. If the function is named, use that name.
  2. If it is an assigment, use what it is assigned to
  3. If it is a function call argument or new argument, call it "fnName() fn argument" where fnName is the function called.
  4. If the call is complex (contains a parenthesis) attempt to remove all but the last bit. (e.g. foo.map(...).sort(...).forEach(...) will become ".forEach() fn argument")
  5. If the function looks like listener for an on event (e.g. foo.on('exit', ...)) call it foo.on 'exit' listener
  6. In any of the above cases if that name has already been assigned, start indexing the names like so: foo, foo{2}, foo{3}, etc.

Naming nests with scope, so if you have code such as this:

function foo() {
  function bar() {
    // ...
  }
}

The two functions would be named foo and foo>bar denoting that this bar is the one defind in the scope of foo.