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


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

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

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

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

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

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

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

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;

Dumps all your /* @annotations */


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

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