Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support module names that are in variables #15

Merged
merged 1 commit into from
Jul 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,25 @@ function parseOptions(opts) {
function findDependencies(source, opts) {
opts = parseOptions(opts);

var potentialModuleNameVariable = {};
var rootDeps = [];
var modules = {};

estraverse.traverse(esprima.parse(source, {sourceType: "module"}), {
leave: function(node, parent) {
if (canBeModuleNameVariable(node)) {
potentialModuleNameVariable[node.id.name] = node.init.value;
}

if (!isAngularModuleStatement(node)) {
if (isNgModuleDeclaration(node)) {
modules['ng'] = [];
}
return;
}

var moduleName = parent.arguments[0].value;
var moduleNameArg = parent.arguments[0];
var moduleName = moduleNameArg.value || potentialModuleNameVariable[moduleNameArg.name];
if (parent.arguments[1]) {
// if already declared, will reset dependencies, like how angular behaves (latest declaration wins)
modules[moduleName] = _.map(parent.arguments[1].elements, 'value');
Expand Down Expand Up @@ -64,6 +70,10 @@ function isNgModuleDeclaration(node) {
return node.type === 'CallExpression' && node.callee.name === 'angularModule' && node.arguments.length > 0 && node.arguments[0].value === 'ng';
}

function canBeModuleNameVariable(node) {
return node.type === 'VariableDeclarator' && typeof node.init.value === 'string';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess node.init could be undefined or null

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. I'll investigate.

}

module.exports = findDependencies;
module.exports.isAngularModuleStatement = isAngularModuleStatement;
module.exports.isNgModuleDeclaration = isNgModuleDeclaration;
12 changes: 12 additions & 0 deletions test/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,16 @@ describe('lookup', function () {
};
lookup(source).should.eql(deps);
});

it('should capture simple module declaration using a variable instead of a literal module name', function () {
var source = 'var moduleName = "test"; angular.module(moduleName, []);';
var deps = {
dependencies: ['ng'],
modules: {
'test': []
}
};
lookup(source).should.eql(deps);
});

});