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

Fixed detection of unused DI in directive and component #595

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion rules/di-unused.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ module.exports = {
'angular?animation': reportUnusedVariables,
'angular?config': reportUnusedVariables,
'angular?controller': reportUnusedVariables,
'angular?directive': reportUnusedVariables,
'angular?factory': reportUnusedVariables,
'angular?filter': reportUnusedVariables,
'angular?inject': reportUnusedVariables,
Expand All @@ -75,6 +74,10 @@ module.exports = {
reportUnusedVariables(null, providerFn);
reportUnusedVariables(null, $get);
},
'angular?directive': function(callee, directiveFn, controllerFn) {
reportUnusedVariables(callee, directiveFn);
reportUnusedVariables(callee, controllerFn);
},

'Program:exit': function() {
var globalScope = context.getScope();
Expand Down
62 changes: 61 additions & 1 deletion rules/utils/angular-rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,22 @@ function angularRule(ruleDefinition) {
if (node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression' || node.type === 'FunctionDeclaration') {
return node;
}

if (node.type === 'ObjectExpression') {
var controller;
node.properties.forEach(function(property) {
if (property.key.name === 'controller') {
if (property.value.type === 'FunctionExpression' || property.value.type === 'ArrowFunctionExpression') {
controller = property.value;
}
if (property.value.type === 'ArrayExpression') {
controller = property.value.elements[property.value.elements.length - 1];
}
}
});
return controller;
}

if (node.type !== 'Identifier') {
return;
}
Expand Down Expand Up @@ -250,14 +266,15 @@ function angularRule(ruleDefinition) {
case 'component':
case 'config':
case 'controller':
case 'directive':
case 'factory':
case 'filter':
case 'run':
case 'service':
return [node.callExpression.callee, node.fn];
case 'provider':
return assembleProviderArguments(node);
case 'directive':
return assembleDirectiveArguments(node);
}
}

Expand All @@ -270,6 +287,49 @@ function angularRule(ruleDefinition) {
return [node.callExpression, node.fn, findProviderGet(node.fn)];
}

function assembleDirectiveArguments(node) {
return [node.callExpression, node.fn, findDirectiveFunction(node.fn)];
}

function findDirectiveFunction(directiveFn) {
if (!directiveFn) {
return;
}

var controllerFn;
var returnFn;
if (directiveFn.body.type !== 'BlockStatement') {
returnFn = directiveFn.body.properties;
returnFn.forEach(function(arg) {
if (arg.key.name === 'controller' && (arg.value.type === 'FunctionExpression' || arg.value.type === 'ArrowFunctionExpression')) {
controllerFn = arg.value;
}
if (arg.value.type === 'ArrayExpression') {
controllerFn = arg.value.elements[arg.value.elements.length - 1];
}
});
return controllerFn;
}

directiveFn.body.body.forEach(function(statement) {
if (statement.type === 'ReturnStatement' && statement.argument.type === 'ObjectExpression') {
returnFn = statement.argument.properties;
returnFn.forEach(function(arg) {
if (arg.key.name === 'controller') {
if (arg.value.type === 'FunctionExpression' || arg.value.type === 'ArrowFunctionExpression') {
controllerFn = arg.value;
}
if (arg.value.type === 'ArrayExpression') {
controllerFn = arg.value.elements[arg.value.elements.length - 1];
}
}
});
}
});

return controllerFn;
}

/**
* Find the $get function of a provider based on the provider function body.
*/
Expand Down