/
jshint-tm.js
executable file
·98 lines (91 loc) · 2.82 KB
/
jshint-tm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
var sys = require('sys');
var fs = require('fs');
var http = require('https');
var env = process.env || process.ENV;
var jshintPath = __dirname + '/jshint.js';
var entities = {
'&': '&',
'"': '"',
'<': '<',
'>': '>'
};
function html(s) {
return (s || '').replace(/[&"<>]/g, function(c) {return entities[c] || c;});
}
/**
* Downloads the latest JSHint version from jshint.com and invokes the callback when done.
*/
function download(ready) {
var req = https.get({host: 'raw.github.com', port: 80, path: '/jshint/jshint/master/jshint.js'}, function(res) {
if (res.statusCode == 200) {
res.setEncoding('utf8');
var data = '';
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
fs.writeFile(jshintPath, data, ready);
});
}
else {
ready('Download of jshint.js failed. HTTP status code: ' + res.statusCode);
}
}).on('error', function(err) {
ready('Download of jshint.js failed: ' + html(err.message));
});
}
/**
* Updates the local copy of jshint.js (if it is older than one day) and
* invokes the given callback, passing the JSHINT object.
*/
function autoupdate(callback) {
var fileExists;
function done(err) {
callback(err, (!err || fileExists) && require(jshintPath).JSHINT);
}
fs.stat(jshintPath, function(err, stats) {
fileExists = !err;
if (err || (Date.now() - Date.parse(stats.mtime)) / 1000 / 60 / 60 / 24 >= 1) {
return download(done);
}
done();
});
}
module.exports = function(options) {
autoupdate(function(err, jshint) {
var body = '';
if (err) {
body += '<div class="error">' + err + '</div>';
}
if (jshint) {
var file = env.TM_FILEPATH;
var input = fs.readFileSync(file, 'utf8');
//remove shebang
input = input.replace(/^\#\!.*/, '');
if (!jshint(input, options)) {
jshint.errors.forEach(function(e) {
if (e) {
var link = 'txmt://open?url=file://' + escape(file) + '&line=' + e.line + '&column=' + e.character;
body += ('<a class="txmt" href="' + link + '">' + e.reason);
if (e.evidence && !isNaN(e.character)) {
body += '<tt>';
body += html(e.evidence.substring(0, e.character-1));
body += '<em>';
body += (e.character <= e.evidence.length) ? html(e.evidence.substring(e.character-1, e.character)) : ' ';
body += '</em>';
body += html(e.evidence.substring(e.character));
body += '</tt>';
}
body += '</a>';
}
});
}
}
if (body.length > 0) {
fs.readFile(__dirname + '/output.html', 'utf8', function(e, html) {
sys.puts(html.replace('{body}', body));
process.exit(205); //show_html
});
}
});
};