summaryrefslogtreecommitdiffstats
path: root/emacs.d/lisp/jshint-mode/jshint-mode.js
blob: f3005f719fe8c0dec45d23243266a027bddea4fd (plain) (blame)
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
/* HTTP interface to JSHint.

   curl --form source="<path/to/my.js" --form=filename="my.js" http://127.0.0.1:3003/jshint

  TODO:
    parse incoming source files for embedded jshint options
    support file uploads?
    speed up
*/

var http = require('http'),
    formidable = require('formidable'),
    JSLINT = require('./jslint'),
    JSHINT = require('./jshint');

var hinters = {
  jshint: JSHINT.JSHINT,
  jslint: JSLINT.JSLINT
};

function getOpt(key) {
  var index = process.ARGV.indexOf(key);
  return index !== -1 ? process.ARGV[index + 1] : false;
}

function outputErrors(errors) {

  var e, i, output = [];

  function out(s) {
    output.push(s + '\n');
  }

  for (i = 0; i < errors.length; i += 1) {
    e = errors[i];
    if (e) {
      out('Lint at line ' + e.line + ' character ' + e.character + ': ' + e.reason);
      out((e.evidence || '').replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1"));
      out('');
    }
  }
  return output.join('');
}

function lintify(mode, sourcedata, filename) {
  var passed = hinters[mode](sourcedata, {});
  return passed ? "js: No problems found in " + filename + "\n"
    : outputErrors(hinters[mode].errors);
}

var port = getOpt("--port") || 3003,
    host = getOpt("--host") || "127.0.0.1";

http.createServer(function(req, res) {

  if (req.url === '/check' && req.method.toUpperCase() === 'POST') {
    var form = new formidable.IncomingForm();
    form.parse(req, function(err, fields, files) {
      var mode = (fields.mode && fields.mode == "jslint") ? "jslint" : "jshint";
      console.log('Applying \'' + mode + '\' to: ' + (fields.filename || 'anonymous'));
      var results = lintify(mode, fields.source, fields.filename);
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.end(results);
    });
    return;
  }

  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end("hello from jshint-mode");

}).listen(port, host);

console.log('Started JSHint server at http:// ' + host + ':' + port);