Skip to content

Commit

Permalink
Transformation replaces passthrough functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
pguillory committed Feb 20, 2011
1 parent 49bd0b2 commit a0d9b0a
Show file tree
Hide file tree
Showing 21 changed files with 103 additions and 50 deletions.
73 changes: 73 additions & 0 deletions lib/transform.js
Expand Up @@ -33,6 +33,7 @@ exports.transformFile = function(filename_, options) {
n.children.push(snippets.helpers())
scan_for_functions_to_transform(n).forEach(transform_function)
replace_passthrough_callbacks(n)
replace_passthrough_functions(n)
var transformed = pp(n) + '\n'

if (options.saveParseTree) {
Expand Down Expand Up @@ -62,6 +63,78 @@ function scan_for_functions_to_transform(script) {
return functions
}

/*
foo()
function foo() {
bar()
}
// ...to...
bar()
*/
function replace_passthrough_functions(script) {
var replacement_names = {}
var replaced_functions = {}

uscan(script.children, function(n) {
if (is_passthrough_function(n)) {
replacement_names[n.name] = n.body.children[0].children[0].value
replaced_functions[n.name] = n
}
})

util.debug('replacement_names: ' + util.inspect(replacement_names))

uscan(script.children, function(n) {
if (n.type === CALL) {
var name = n.children[0].value
var new_name = replacement_names[name]
if (new_name) {
n.children[0].value = new_name
}
}
})

for (var name in replaced_functions) {
var replaced_function_definition = replaced_functions[name]
uscan(script.children, function(n) {
if (n.children) {
for (var i in n.children) {
if (n.children[i] === replaced_function_definition) {
delete n.children[i]
return
}
}
}
})
}
}

function is_passthrough_function(n) {
return n.type === FUNCTION
&& n.params
&& n.params.length === 0
&& n.body
&& n.body.type === SCRIPT
&& n.body.children
&& n.body.children.length === 1
&& n.body.children[0].type === CALL
&& n.body.children[0].children
&& n.body.children[0].children.length === 2
&& n.body.children[0].children[0].type === IDENTIFIER
&& n.body.children[0].children[0].value // <- replacement name
&& n.body.children[0].children[1].type === LIST
&& n.body.children[0].children[1].children
&& n.body.children[0].children[1].children.length === 0
}

/*
function(err, result) {
if (err) return callback(err)
return callback(null, result)
}
// ...to...
callback
*/
function replace_passthrough_callbacks(script) {
uscan(script.children, function(n) {
if (is_passthrough_for_callback(n)) {
Expand Down
6 changes: 2 additions & 4 deletions test/tests/3. While/1. Works/index.js
Expand Up @@ -21,16 +21,14 @@ function pow(base, exponent, __callback_2) {
__then_block_6();
}
else {
__else_block_7();
__rest_4();
}
;
function __then_block_6() {
n *= base;
setTimeout(__while_loop_3, 0);
};
function __else_block_7() {
__rest_4();
};
;
});
};
function __rest_4() {
Expand Down
2 changes: 1 addition & 1 deletion test/tests/3. While/1. Works/index.js.after.json

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions test/tests/3. While/2. Body/index.js
Expand Up @@ -25,7 +25,7 @@ function pow(base, exponent, __callback_2) {
__then_block_6();
}
else {
__else_block_7();
__rest_4();
}
;
function __then_block_6() {
Expand All @@ -37,9 +37,7 @@ function pow(base, exponent, __callback_2) {
setTimeout(__while_loop_3, 0);
});
};
function __else_block_7() {
__rest_4();
};
;
});
};
function __rest_4() {
Expand Down
2 changes: 1 addition & 1 deletion test/tests/3. While/2. Body/index.js.after.json

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions test/tests/3. While/3. Break/index.js
Expand Up @@ -21,7 +21,7 @@ function pow(base, exponent, __callback_2) {
__then_block_5();
}
else {
__else_block_6();
__rest_4();
}
;
function __then_block_5() {
Expand Down Expand Up @@ -50,9 +50,7 @@ function pow(base, exponent, __callback_2) {
};
});
};
function __else_block_6() {
__rest_4();
};
;
};
function __rest_4() {
return __callback_2(null, n);
Expand Down
2 changes: 1 addition & 1 deletion test/tests/3. While/3. Break/index.js.after.json

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions test/tests/3. While/4. Break in else/index.js
Expand Up @@ -21,7 +21,7 @@ function pow(base, exponent, __callback_2) {
__then_block_5();
}
else {
__else_block_6();
__rest_4();
}
;
function __then_block_5() {
Expand Down Expand Up @@ -53,9 +53,7 @@ function pow(base, exponent, __callback_2) {
};
});
};
function __else_block_6() {
__rest_4();
};
;
};
function __rest_4() {
return __callback_2(null, n);
Expand Down
2 changes: 1 addition & 1 deletion test/tests/3. While/4. Break in else/index.js.after.json

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions test/tests/4. For/1. Works/index.js
Expand Up @@ -25,7 +25,7 @@ function f(__callback_2) {
__then_block_6();
}
else {
__else_block_7();
__rest_4();
}
;
function __then_block_6() {
Expand All @@ -41,9 +41,7 @@ function f(__callback_2) {
});
});
};
function __else_block_7() {
__rest_4();
};
;
});
};
function __rest_4() {
Expand Down
2 changes: 1 addition & 1 deletion test/tests/4. For/1. Works/index.js.after.json

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions test/tests/4. For/2. Iterates properly/index.js
Expand Up @@ -23,7 +23,7 @@ function pow(base, exponent, __callback_2) {
__then_block_7();
}
else {
__else_block_8();
__rest_5();
}
;
function __then_block_7() {
Expand All @@ -41,9 +41,7 @@ function pow(base, exponent, __callback_2) {
});
});
};
function __else_block_8() {
__rest_5();
};
;
});
};
function __rest_5() {
Expand Down
2 changes: 1 addition & 1 deletion test/tests/4. For/2. Iterates properly/index.js.after.json

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions test/tests/5. Switch/2. SFC in case/index.js
Expand Up @@ -8,16 +8,14 @@ function f(__callback_2) {
__callback_2 = (__callback_2 || __throw_1);
switch ("foo") {
case "bar":
return __case_3();
return __rest_6();
case "foo":
return __case_4();
default:
return __case_5();
};
__rest_6();
function __case_3() {
__rest_6();
};
;
function __case_4() {
return g(__callback_2);
};
Expand Down
2 changes: 1 addition & 1 deletion test/tests/5. Switch/2. SFC in case/index.js.after.json

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions test/tests/7. For-in/1. Basic use/index.js
Expand Up @@ -19,7 +19,7 @@ function sum(values, __callback_2) {
__then_block_7();
}
else {
__else_block_8();
__rest_6();
}
;
function __then_block_7() {
Expand All @@ -33,9 +33,7 @@ function sum(values, __callback_2) {
setTimeout(__for_loop_5, 0);
});
};
function __else_block_8() {
__rest_6();
};
;
};
function __rest_6() {
return __callback_2(null, total);
Expand Down

0 comments on commit a0d9b0a

Please sign in to comment.