Permalink
Browse files

added possibility to find nested object functions

  • Loading branch information...
1 parent 2b4f813 commit 01749781e9866d524324f390e5e0eac0e4762bfb @dresende committed Aug 25, 2011
Showing with 32 additions and 15 deletions.
  1. +32 −15 lib/ast-transformer.js
View
@@ -26,7 +26,7 @@ Transformer.prototype.generate = function () {
return js_compressor.gen_code(ast);
};
-Transformer.prototype.__crawlAST = function (ast) {
+Transformer.prototype.__crawlAST = function (ast, prefix) {
if (ast == undefined) return;
switch (ast[0]) {
@@ -39,6 +39,7 @@ Transformer.prototype.__crawlAST = function (ast) {
break;
case "function":
case "defun":
+ // @TODO: check function arguments
for (var i = 0; i < ast[3].length; i++) {
ast[3][i] = this.__crawlAST(ast[3][i]);
}
@@ -73,25 +74,29 @@ Transformer.prototype.__crawlAST = function (ast) {
ast[1] = this.__crawlAST(ast[1]);
break;
case "call":
+ var functionCall = getCallPath(ast[1]);
+
+ if (functionCall !== null && this.functionCalls.hasOwnProperty(functionCall)) {
+ var args = [];
+
+ for (var i = 0; i < ast[2].length; i++) {
+ // @TODO: check parameter type
+ args.push(convertASTToVar(ast[2][i]));
+ }
+
+ ast = convertVarToAST(this.functionCalls[functionCall].apply(null, args));
+ break;
+ }
+
switch (ast[1][0]) {
case "call":
ast[1] = this.__crawlAST(ast[1]);
break;
case "name":
- if (this.functionCalls.hasOwnProperty(ast[1][1])) {
- var args = [];
-
- for (var i = 0; i < ast[2].length; i++) {
- // @TODO: check parameter type
- args.push(convertASTToVar(ast[2][i]));
- }
-
- ast = convertVarToAST(this.functionCalls[ast[1][1]].apply(null, args));
- break;
- }
+ break;
case "dot":
for (var i = 0; i < ast[2].length; i++) {
- ast[2][i] = this.__crawlAST(ast[2][i]);
+ ast[2][i] = this.__crawlAST(ast[2][i], ast);
}
break;
default:
@@ -119,12 +124,24 @@ Transformer.prototype.__crawlAST = function (ast) {
}
break;
default:
- console.log("---------- unknown --------------");
- console.log(ast);
+ for (var i = 1; i < ast.length; i++) {
+ ast[i] = this.__crawlAST(ast[i]);
+ }
+ // console.log("---------- unknown --------------");
+ // console.log(ast);
}
return ast;
};
+function getCallPath(ast) {
+ switch (ast[0]) {
+ case "name":
+ return ast[1];
+ case "dot":
+ return getCallPath(ast[1]) + "." + ast[2];
+ }
+ return null;
+}
function convertVarToAST(v) {
switch (typeof v) {
case "string":

0 comments on commit 0174978

Please sign in to comment.