Skip to content

Instantly share code, notes, and snippets.

@peterreisz
Last active July 4, 2016 07:32
Show Gist options
  • Save peterreisz/1e4f8c41d68d23060b0ec4650916c17e to your computer and use it in GitHub Desktop.
Save peterreisz/1e4f8c41d68d23060b0ec4650916c17e to your computer and use it in GitHub Desktop.
Detect angularjs 1.x unused injected dependencies
function u(i) {
var s = 'function fn'+ i.substring(9),
i = s.indexOf('('),
j = s.indexOf(')');
return eslint.verify(s, {
rules: {
'no-unused-vars': [
'error', {
vars: 'all',
args: 'all'
}
]
}
}).filter(function(item) {
return i < item.column && item.column <= j && item.line == 1;
}).map(function(item) {
return (s.substring(i + 1, item.column).match(/,/g) || []).length
});
}
function fn() {
var p = [];
var listed = {};
(function ud(m) {
var m = angular.module(m),
q = m._invokeQueue;
m._invokeQueue.forEach(function(i) {
var d = i[2][1], n = i[2][0];
if ('constant' != i[1] && 'value' != i[1] && d && d[d.length - 1] && !listed[m.name + n]) {
listed[m.name + n] = true
var e = u(d[d.length - 1].toString());
if (e.length > 0) {
p.push({
module: m.name,
name: n,
unused: e.map(function(w) {
return i[2][1][w];
}).join(', ')
});
}
}
});
m.requires.forEach(ud);
})(document.querySelector('[ng-app]').attributes.getNamedItem('ng-app').value)
console.table(p);
}
if (window.eslint) {
fn();
} else {
var s=document.createElement('script');
s.setAttribute('src','http://eslint.org/js/app/eslint.js');
s.addEventListener('load', fn);
document.body.appendChild(s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment