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 🙏

© 2025 – Pkg Stats / Ryan Hefner

static-method

v0.1.1

Published

Easily replace specific function calls without messing around with the syntax tree

Downloads

15

Readme

static-method

Replace function calls using esprima.

static-method lets you easily replace specific function calls without messing around with the syntax tree. It exists as a nice middle ground between running a regex over your entire codebase and defining a complicated AST transform.

Build Status

var staticMethod = require('static-method');

var sm = staticMethod({
  parseInt: function(src, node) {
    if (node.arguments.length === 1) {
      return 'parseInt(' + node.arguments[0].raw + ', 10)';
    }
  }
});

process.stdin.pipe(sm).pipe(process.stdout);

Examples:

Install

npm install --save static-method

Methods

var sm = staticMethod(methods)

Returns a transform stream that transforms javascript source code according to each property in the methods configuration object.

methods object

methods is a configuration object. The keys are the function names you'd like to replace. The value is a function that defines the transform you'd like to perform on that function call.

transform function

Each transform function receives two arguments. The first is the source of the function call. The second is a falafel AST node that you can modify directly, use to extract arguments, etc.

The easiest way to replace the call is to return a string from the transform function that contains the replacement code.

Ex: This replaces all calls to foo() with the string "bar":

var sm = staticMethod({
  foo: function() {
    return '"bar"';
  }
});

Examples

Replace all calls to foo() with calls to bar().

var staticMethod = require('static-method');

var sm = staticMethod({
  foo: function(src, node) {
    return src.replace(/^foo/, 'bar');
  }
});

process.stdin.pipe(sm).pipe(process.stdout);

input:

$ cat source.js

foo();
foo(1, 2);
foo(a, function(err, data) {
  if (err) throw(err);
  console.log(data);
});

output:

$ node replace.js < source.js

bar();
bar(1, 2);
bar(a, function(err, data) {
  if (err) throw(err);
  console.log(data);
});

Comment out all calls to eval and add an alert

var staticMethod = require('static-method');

var sm = staticMethod({
  eval: function(src, node) {
    return 'alert("Think about what you\'ve done"); /*' + src + '*/';
  }
});

process.stdin.pipe(sm).pipe(process.stdout);

input:

$ cat source.js

console.log(eval("2 + 2"));

output:

$ node replace.js < source.js

console.log(alert("Think hard about what you're doing") /*eval("2 + 2")*/);

Add the radix to parseInt

It's best practice to always include the optional radix parameter in parseInt calls. Let's enforce this.

var staticMethod = require('static-method');

var sm = staticMethod({
  parseInt: function(src, node) {
    if (node.arguments.length === 1) {
      return 'parseInt(' + node.arguments[0].raw + ', 10)';
    }
  }
});

process.stdin.pipe(sm).pipe(process.stdout);

input:

$ cat source.js

parseInt();
parseInt('5');
parseInt('5', 10);

output:

$ node replace.js < source.js

parseInt();
parseInt('5', 10);
parseInt('5', 10);

Alternatives

Inspired by static-module by substack.

If your needs are more sophisticated then you should look at jstransform, falafel, or go straight for esprima.

License

MIT