JavaScript Shaper

Shaper is an extensible framework for JavaScript syntax tree shaping, created by Olov Lassus. Follow me on Twitter.

Shaper manipulates the syntax tree representation of your JS source code, and then turns it back into source code again. It goes very far to keep all your source code formatting, indentation, whitespace and comments intact.

Discuss in the JSShaper Google group. Source code available (MIT license) at GitHub. Check out the README for more info. Clone it and use it or hack it and create your own plugins! All of Shaper is written in JavaScript so you'll start adding to it in no time. Shaper is ES5 strict mode and restrict mode clean, and will stay that way.

node run-shaper.js filename [plugin1 .. pluginN]
        where the plugins (or builtins) forms a syntax tree shaping pipeline

Example: node run-shaper.js myfile.js plugins/annotater.js plugins/restricter.js --source


PLUGINS

It's really easy to create new Shaper plugins. Here are the existing ones (click to view JavaScript source)

restricter
Adds restrict mode checking to your program by rewriting operators into function-calls

asserter
C-style assertions with great error output
Assert(f(x) === 3)

logger
Formatted logging with great detail (file, function, line)
Log("Formatting via {0}", "Fmt") or just Log()

watcher
Trap (watch) object modifications for debugging purposes
Watch(o, "age", function() {
  if (o.age < 0) {
    throw new Error("age out of range");
  }
});

bitwiser
Requires annotation for bitwise operators | and & to avoid mistakenly using them instead of || and &&
var y = (/*@bitwise*/ x | 3);

stringconverter
Converts "" + expr and expr + "" into String(expr)

annotater
Internal plugin used by other plugins. Lets you match annotations and execute code with no effort Annotater(/\/\*+\s*@loose\s*\*+\//,
  function(node, match) {
    node.loose = true;
  }
);

annotation-printer
Dumps all your /* @annotations */


BUILTINS

--tree
Dumps the syntax tree in an easy to read format, used when developing plugins

--source
Prints the shaped source code. This is often the final plugin/builtin used in a pipeline