Skip to content

Commit

Permalink
* Working traversal and filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
joehewitt committed Jul 22, 2011
0 parents commit 0d788c4
Show file tree
Hide file tree
Showing 7 changed files with 427 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
13 changes: 13 additions & 0 deletions LICENSE
@@ -0,0 +1,13 @@
Copyright 2011 Joe Hewitt

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
6 changes: 6 additions & 0 deletions Makefile
@@ -0,0 +1,6 @@
default: test

test:
vows test/*-test.js

.PHONY: test
75 changes: 75 additions & 0 deletions README.md
@@ -0,0 +1,75 @@
transformjs
===========

Transforms JavaScript code safely.

TransformJS is based on the [Uglify](https://github.com/mishoo/UglifyJS) JavaScript parser. Once Uglify has parsed the JavaScript into an abstract syntax tree (AST), TransformJS allows you to traverse the AST and add, remove, replace, or modify nodes along the way.

TransformJS can be used for static analysis to remove dead code or for searching for patterns in the code. If you're using hand-rolled regular expressions for transforming JavaScript, TransformJS offers you a safer option by parsing the code according to the language grammar and outputting valid code.

Status
------------

Work-in-progress and intended only for myself for now.

Installation
------------

$ npm install transformjs (someday! not in registry yet)

Usage
------------

Here is an example that replaces all numbers with the value 2.

var transformjs = require('transformjs');
var ast = transformjs.transform('if (1) { a() } else { b() }', [
function(node, next) {
if (node.type == 'number') {
return {type: number, value: 2};
} else {
return next(node);
}
},
]);

var js = transformjs.generate(ast);


Traversal occurs from top to bottom. Filter functions are called in order for each node.
The filter can return the node unchanged, return a new node to take its place, or return null
to remove the node.

You are responsible for calling the next() function in order to call the next filter. If you
do not call next(), then you are responsible for traversing each of the sub-nodes of the node
by passing each sub-node to the next function.

function(node, next) {
if (node.type == 'binary') {
// Traverse the child nodes of the expression one by one
node.left = next(node.left);
node.right = next(node.right);
// Return the node without calling next, thereby ignoring subsequent filters
return node;
} else {
// Continue processing node and traversing its child nodes
return next();
}
}

License
-------

Copyright 2011 Joe Hewitt

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
215 changes: 215 additions & 0 deletions lib/transformjs.js
@@ -0,0 +1,215 @@

var dandy = require('dandy');
var uglify = require('uglify-js');

/**
* Traverses JavaScript AST, calling filters for each node.
*
* source - JavaScript source
* filters - array of functions like `function(node, next) {}`
*/
exports.transform = function(source, filters) {
// Parse into Uglify's AST of nested arrays
var ast = uglify.parser.parse(source);
// logDeep(ast[1]);

// Convert Uglify's AST into a nicer object hierarchy
var nodes = arraysToNodes(ast[1]);
// logDeep(nodes);

// Traverse hierarchy, calling filters for each node
nodes = visit(nodes);
// logDeep(nodes);

// Convert AST back to Uglify's format
ast[1] = nodesToArrays(nodes);
// logDeep(ast[1]);

return ast;

function visit(node) {
if (!node) {
return node;
} else if (node instanceof Array) {
var newNodes = [];
for (var i = 0; i < node.length; ++i) {
var newNode = visit(node[i]);
if (newNode) {
newNodes.push(newNode);
}
}
return newNodes;
} else {
var fns = filters ? filters.slice() : [];
fns.push(function(n) {
return walkNode(n, visit);
});

function next(n) {
if (n) {
return visit(n);
} else if (fns.length) {
return fns.shift()(node, next);
}
}

return next();
}
}
}

/**
* Generates JavaScript source from the AST returned by transform().
*/
exports.generate = function(ast, minify) {
var pro = uglify.uglify;
if (minify) {
ast = pro.ast_mangle(ast, {toplevel: true});
ast = pro.ast_squeeze(ast);
}
return pro.gen_code(ast, {beautify: false});
}

// *************************************************************************************************

var typeMap = {
num: ['value', LITERAL],
string: ['value', LITERAL],
name: ['name', LITERAL],
'if': ['condition', NODE, 'ifBlock', NODE, 'elseBlock', NODE],
'call': ['left', NODE, 'args', ARRAY],
'var': ['decls', DECLS],
'binary': ['op', LITERAL, 'left', NODE, 'right', NODE],
'assign': ['um', LITERAL, 'left', NODE, 'right', NODE],
'decl': ['left', LITERAL, 'right', NODE],
block: ['statements', ARRAY],
stat: ['expr', NODE],
};

function LITERAL(val, from) {
return val;
}

function NODE(val, from) {
if (from) {
return arrayToNode(val);
} else {
return nodeToArray(val);
}
}

function ARRAY(val, from) {
if (from) {
return arraysToNodes(val);
} else {
return nodesToArrays(val);
}
}

function DECLS(val, from) {
if (from) {
return arraysToNodes(val, true);
} else {
return nodesToArrays(val, true);
}
}

// *************************************************************************************************

function arraysToNodes(statements, isDecl) {
var nodes = [];
for (var i = 0; i < statements.length; ++i) {
var statement = statements[i];
var newNode = arrayToNode(isDecl ? statement[1] : statement);
if (isDecl) {
newNode = {type: 'decl', left: statement[0], right: newNode};
}
if (newNode) {
nodes.push(newNode);
}
}
return nodes;
}


function arrayToNode(arr) {
var map = typeMap[arr[0]];
if (map) {
var node = {type: arr[0]};
for (var i = 0; i < map.length; i += 2) {
var name = map[i];
var fn = map[i+1];
var obj = arr[(i/2)+1];
if (obj !== undefined) {
node[name] = fn(obj, true);
}
}
return node;
} else {
return {type: arr[0], original: arr};
}
}

function nodesToArrays(nodes, isDecl) {
var arrays = [];
for (var i = 0; i < nodes.length; ++i) {
var node = nodes[i];
if (isDecl) {
var arr = nodeToArray(node.right);
arrays.push([node.left, arr]);
} else {
var arr = nodeToArray(node);
arrays.push(arr);
}
}
return arrays;
}

function nodeToArray(node) {
var map = typeMap[node.type];
if (map) {
var arr = [node.type];
for (var i = 0; i < map.length; i += 2) {
var name = map[i];
var fn = map[i+1];
var obj = node[name];
var objArr = fn(obj);
arr.push(objArr);
}
return arr;
} else {
return node.original;
}
}

function walkNodes(nodes, visit) {
var newNodes = [];
for (var i = 0; i < nodes.length; ++i) {
var newNode = visit(nodes[i]);
if (newNode) {
newNodes.push(newNode);
}
}
return newNodes;
}

function walkNode(node, visit) {
var map = typeMap[node.type];
if (map) {
for (var i = 0; i < map.length; i += 2) {
var name = map[i];
var fn = map[i+1];
var obj = node[name];
if (fn == NODE) {
node[name] = visit(obj);
} else if (fn == ARRAY || fn == DECLS) {
node[name] = visit(obj);
}
}
}
return node;
}

function logDeep(obj) {
console.log(require('util').inspect(obj, null, 200));
}
20 changes: 20 additions & 0 deletions package.json
@@ -0,0 +1,20 @@
{
"name": "transformjs",
"description": "Transforms JavaScript code.",
"version": "0.0.1",
"homepage": "http://github.com/joehewitt/transformjs",
"repository": {
"type": "git",
"url" : "http://github.com/joehewitt/transformjs.git"
},
"keywords": [],
"author": "Joe Hewitt <joe@joehewitt.com>",
"contributors": [],
"dependencies": {
"dandy": "https://github.com/joehewitt/dandy/tarball/master",
"uglify-js": ""
},
"engines": { "node": ">=0.4.0" },
"main": "./lib/transformjs",
"directories": {}
}

0 comments on commit 0d788c4

Please sign in to comment.