New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

static-method

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

static-method

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

latest
Source
npmnpm
Version
0.1.1
Version published
Maintainers
1
Created
Source

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

Keywords

ast

FAQs

Package last updated on 26 Jun 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts