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

scopetracer

v1.0.1

Published

Modify JavaScript strings by applying mutation functions to each scope node in the AST

Downloads

33

Readme

scopetracer

Modify scoped segments of JavaScript. Provide a mutating function and a filter test, and it will apply the mutator to scope blocks found in JavaScript strings.

Example

var ScopeTracer = require("scopetracer")

var input = "// some javascript!\n\
function foo() {\n\
  // In a function\n\
  var abc = 'ABC'\n\
  setTimeout(function () {\n\
    console.log(abc)\n\
  }, 10)\n\
}\n\
foo()\n"

function mutate(content) {
  // We're putting things in single quotes, escape single quotes
  var name = this.fnName.replace(/'/g, "\\'")
  if (this.body.length === 0) {
    return []
  }
  return [{insertion: "console.log('entering " + name + "');", pos: this.body[0].range[0]}]
}

function nodeTest() {
  // Only functions, not outer enclosing scope.
  return this.path.length > 0
}

var tracer = ScopeTracer(mutate, nodeTest)

var output = tracer.transform(input)

console.log(output)

/*
  // some javascript!
  function foo() {
    // In a function
    console.log('entering foo');var abc = 'ABC'
    setTimeout(function () {
      console.log('entering foo>setTimeout() fn argument');console.log(abc)
    }, 10)
  }
  foo()
*/

eval(output)

/*
  entering foo
  entering foo>setTimeout() fn argument
  ABC
*/

API

var scopetracer = require("scopetracer")(mutate[, nodeTest])

Creates a tracer object that can be used to transform javascript strings.

mutate(content[, extra[, ...]])

The mutate function is called with the context of this being the scopenodes node defining this scope.

This is usually a function, though will also include the outer Program enclosing scope.

It is an Esprima AST node with a couple of additions added by scopenodes.

To mutate the content, return an array of objects defining the insertions or replacements to make.

Insertions are always safe, but it is up to you to make sure that replacements (using remove) do overlap other insertions or replacements.

[
  {
    insertion: "string to insert",
    pos: 200, // Position to insert, relative to the Esprima "range" e.g. this.range[0]
    remove: 0, // optional, how many characters to remove from content prior to inserting
    pri: 0, // optional: insertion priority in case of a `pos` tie. Lower pri inserts before higher pri.
  },
  {
    // ... return as many mutations as you like
  }
]

nodeTest(content[, extra[, ...]])

The test function is called with the same context as mutate.

It is simply expected to return true if mutate should be run or false if mutate is to be skipped for this node.

The default nodeTest if not defined is function nodeTest() { return true; } i.e. always call mutate.

scopetracer.transform(content[, extra[, ...]])

Extract scope nodes, then transform them according to mutate and nodeTest.

Any extra arguments will be added as arguments to mutate and nodeTest.

Similar Projects

falafel

LICENSE

MIT