reid/node-jslint — Issue #18: Some extra useful information
Author: Satyam
Created: 2011-10-12 21:20:56+00:00
State: open

Checking the parse tree of JSLint, there are a few extra good pieces of information you can get.

To be able to exploit that, linter.js must return the parsing tree, which can be easily done by adding:
```javascript
result.tree = JSLINT.tree;
```
close to where other bits of information are returned.

Later in reporter.js, it is easy to list globals, which are rarely a good thing:

```javascript
if (lint.globals) {
console.log('\nGlobals:\n' + lint.globals.join(' , ') + '\n');
}
```
And also both unused or undefined variables:

```javascript
var uu = function (what, label) {
if (what) {
var i, d, snippets = [];
for (var i = 0;i < what.length; i++) {
d = what[i];
snippets[i] = '[' + (d.line + 1) + ',1] en: ' + d['function'] + ' - ' + d.name;
}
console.log('\n' + label + ' variable:\n' + snippets.join('\n') + '\n');
}
};
uu(lint.undefined, 'Undefined');
uu(lint.unused, 'Unused');
```

The most complex addition is to list repeated literals, good candidates to turn into constants that the YUI Compressor might significantly reduce:

```javascript
var repeated = function (tree) {
var strs = {},
re = /^(first|second|third|else|block|\d+)$/,

traverse = function (what) {
var k,
v = what.id;
if (v === '(string)' || v === '(number)') {
v = what.value;
if (v in strs) {
strs[v] += 1;
} else {
strs[v] = 1;
}
}

for (k in what) {
if (what.hasOwnProperty(k)) {
if (re.test(k)) {
v = what[k];
if (v) {
traverse(v);
}
}
}
}
};

traverse(tree);
console.log('Repeated Constants:\n');
var now = 2,
next,
k,
v,
strs1 = {};

for (k in strs) {
if (strs.hasOwnProperty(k)) {
v = strs[k];
if (v !== 1) {
strs1[k] = v;
}
}
}
strs = strs1;

while (now < Number.MAX_VALUE) {
console.log(now + ':');
next = Number.MAX_VALUE;
for (k in strs) {
if (strs.hasOwnProperty(k)) {
v = strs[k];
if (v === now) {
console.log(' ' + k);
} else {
if (v > now) {
next = Math.min(next, v);
}
}
}
}
now = next;
}
};
repeated(lint.tree);
```

Crockford made significant changes to JSLint recently, the tree is different now, I had to go back to an earlier version but my current one actually works with the latest JSLint. I am not sure if I picked the correct one for the version you are using. The code listing the repeated constants works but the globals, undefined and unused didn't show any in a few samples I tried, there might not be any (which is good) or I got the wrong version of my code for your version of JSLint.