]> git.r.bdr.sh - rbdr/dotfiles/blame - vim/ftplugin/javascript/jslint/runjslint.js
Merge branch 'master' of github.com:benbeltran/dotfiles
[rbdr/dotfiles] / vim / ftplugin / javascript / jslint / runjslint.js
CommitLineData
64bb1bce
BB
1/*jslint laxbreak: true */
2
3var fs, vm, sandbox, jslintCore = 'jslint-core.js';
4
5if (typeof require !== 'undefined') {
6 print = require('util').puts;
7 fs = require('fs');
8 vm = require('vm');
9 sandbox = {};
10 res = vm.runInNewContext(fs.readFileSync(jslintCore), sandbox, jslintCore);
11 JSLINT = sandbox.JSLINT;
12} else {
13 load('jslint-core.js');
14}
15
16// Import extra libraries if running in Rhino.
17if (typeof importPackage != 'undefined') {
18 importPackage(java.io);
19 importPackage(java.lang);
20}
21
22var readSTDIN = (function() {
23 // readSTDIN() definition for nodejs
24 if (typeof process != 'undefined' && process.openStdin) {
25 return function readSTDIN(callback) {
26 var stdin = process.openStdin()
27 , body = [];
28
29 stdin.on('data', function(chunk) {
30 body.push(chunk);
31 });
32
33 stdin.on('end', function(chunk) {
34 callback(body.join('\n'));
35 });
36 };
37
38 // readSTDIN() definition for Rhino
39 } else if (typeof BufferedReader != 'undefined') {
40 return function readSTDIN(callback) {
41 // setup the input buffer and output buffer
42 var stdin = new BufferedReader(new InputStreamReader(System['in'])),
43 lines = [];
44
45 // read stdin buffer until EOF (or skip)
46 while (stdin.ready()){
47 lines.push(stdin.readLine());
48 }
49
50 callback(lines.join('\n'));
51 };
52
53 // readSTDIN() definition for Spidermonkey
54 } else if (typeof readline != 'undefined') {
55 return function readSTDIN(callback) {
56 var line
57 , input = []
58 , emptyCount = 0
59 , i;
60
61 line = readline();
62 while (emptyCount < 25) {
63 input.push(line);
64 if (line) {
65 emptyCount = 0;
66 } else {
67 emptyCount += 1;
68 }
69 line = readline();
70 }
71
72 input.splice(-emptyCount);
73 callback(input.join('\n'));
74 };
75 }
76})();
77
78readSTDIN(function(body) {
79 var ok = JSLINT(body)
80 , i
81 , error
82 , errorType
83 , nextError
84 , errorCount
85 , WARN = 'WARNING'
86 , ERROR = 'ERROR';
87
88 if (!ok) {
89 errorCount = JSLINT.errors.length;
90 for (i = 0; i < errorCount; i += 1) {
91 error = JSLINT.errors[i];
92 errorType = WARN;
93 nextError = i < errorCount ? JSLINT.errors[i+1] : null;
94 if (error && error.reason && error.reason.match(/^Stopping/) === null) {
95 // If jslint stops next, this was an actual error
96 if (nextError && nextError.reason && nextError.reason.match(/^Stopping/) !== null) {
97 errorType = ERROR;
98 }
99 print([error.line, error.character, errorType, error.reason].join(":"));
100 }
101 }
102 }
103});