Skip to content

Commit

Permalink
Merge pull request #2 from dfellis/improve-tree-traversal-and-tests
Browse files Browse the repository at this point in the history
Improve the AST traversal and the test coverage.
  • Loading branch information
David Ellis committed Jul 15, 2014
2 parents 65be731 + a6f5c9f commit 6d3a5fa
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
17 changes: 17 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ function findAutoSubtree(ast, asyncIdentifier) {
if (result) break;
}
return result;
} else if (ast.type === 'FunctionExpression') {
for (var l = 0; l < ast.body.body.length; l++) {
result = findAutoSubtree(ast.body.body[l], asyncIdentifier);
if (result) break;
}
return result;
} else if (ast.type === 'ExpressionStatement') {
return findAutoSubtree(ast.expression, asyncIdentifier);
} else if (ast.type === 'CallExpression') {
Expand All @@ -78,6 +84,17 @@ function findAutoSubtree(ast, asyncIdentifier) {
return ast.arguments[0];
}
return undefined;
} else if (ast.type === 'AssignmentExpression') {
return findAutoSubtree(ast.right, asyncIdentifier);
} else if (ast.type === 'BinaryExpression') {
result = findAutoSubtree(ast.left, asyncIdentifier);
if (result) return result;
result = findAutoSubtree(ast.right, asyncIdentifier);
return result;
} else if (ast.type === 'ReturnStatement') {
return findAutoSubtree(ast.argument, asyncIdentifier);
} else if (ast.type === 'Literal') {
return undefined;
} else {
// TODO: Handle the rest of the tree
return undefined;
Expand Down
5 changes: 5 additions & 0 deletions tests/examples/dynamicauto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var async = require('async');

module.exports = function autoWrapper(steps, callback) {
async.auto(steps, callback);
};
18 changes: 18 additions & 0 deletions tests/examples/multirequire.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var foo = require('bar');
var async = require('async');

function blah() {
return foo.blah() * 2;
}

module.exports = function baz(abc, callback) {
async.auto({
a: foo.a,
b: foo.b,
c: ['a', 'b', foo.c],
d: ['c', foo.d],
e: ['b', foo.e],
f: ['d', 'e', foo.f],
g: ['f', blah]
}, callback);
};
40 changes: 40 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,43 @@ exports.buildGraphVizDotString = function(test) {
test.equal(typeof dotString, 'string');
test.done();
};

exports.fullStack = function(test) {
test.expect(1);
var result = fs.readFileSync('./tests/examples/basic.dot', 'utf8');
test.equal(
result,
async2dot.buildGraphVizDotString(
async2dot.findAutoSubtree(
async2dot.getAst(
async2dot.loadFile(
'./tests/examples/basic.js'
)))));
test.done();
};

exports.checkPathThatIgnoresNonAsyncRequires = function(test) {
test.expect(1);
test.equal(
'ObjectExpression',
async2dot.findAutoSubtree(
async2dot.getAst(
async2dot.loadFile(
'./tests/examples/multirequire.js'
))).type
);
test.done();
};

exports.throwsWhenAutoIsDynamic = function(test) {
test.expect(1);
test.throws(function() {
async2dot.buildGraphVizDotString(
async2dot.findAutoSubtree(
async2dot.getAst(
async2dot.loadFile(
'./tests/examples/dynamicauto.js'
))));
});
test.done();
};

0 comments on commit 6d3a5fa

Please sign in to comment.