forked from fgnass/jshint.tmbundle
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/fgnass/jshint.tmbundle
- Loading branch information
Showing
4 changed files
with
99 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,98 @@ | ||
var sys = require("sys"), | ||
fs = require("fs"), | ||
env = process.env || process.ENV, | ||
JSHINT = require('./jshint.js').JSHINT, | ||
entities = { | ||
'&': '&', | ||
'"': '"', | ||
'<': '<', | ||
'>': '>' | ||
}; | ||
var sys = require('sys'); | ||
var fs = require('fs'); | ||
var http = require('http'); | ||
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;}); | ||
} | ||
|
||
module.exports = function (options) { | ||
var file = env.TM_FILEPATH, | ||
input = fs.readFileSync(file, 'utf8'), | ||
body = ''; | ||
/** | ||
* Downloads the latest JSHint version from jshint.com and invokes the callback when done. | ||
*/ | ||
function download(ready) { | ||
var req = http.get({host: 'jshint.com', port: 80, path: '/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) { | ||
|
||
//remove shebang | ||
input = input.replace(/^\#\!.*/, ''); | ||
var file = env.TM_FILEPATH; | ||
var input = fs.readFileSync(file, 'utf8'); | ||
|
||
if (!JSHINT(input, options)) { | ||
JSHINT.errors.forEach(function(e) { | ||
if (e) { | ||
body += ('<a href="txmt://open?url=file://' + escape(file) + '&line=' + e.line + '&column=' + e.character + '">' + 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>'; | ||
//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>'; | ||
} | ||
}); | ||
} | ||
}); | ||
fs.readFile(__dirname + '/output.html', 'utf8', function(e, html) { | ||
sys.puts(html.replace('{body}', body)); | ||
process.exit(205); //show_html | ||
}); | ||
} | ||
} | ||
if (body.length > 0) { | ||
fs.readFile(__dirname + '/output.html', 'utf8', function(e, html) { | ||
sys.puts(html.replace('{body}', body)); | ||
process.exit(205); //show_html | ||
}); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters