Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Dale Harvey committed Mar 7, 2011
0 parents commit 132538f
Show file tree
Hide file tree
Showing 5 changed files with 6,107 additions and 0 deletions.
23 changes: 23 additions & 0 deletions LICENSE
@@ -0,0 +1,23 @@
jshint-mode copyright (c) 2011 Dale Harvey (arandomurl.com)
lintnode copyright (c) 2010 Kevin Turner (keturn.net)
fullnodejs.js copyright (c) 2002 Douglas Crockford (www.JSLint.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

The Software shall be used for Good, not Evil.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
35 changes: 35 additions & 0 deletions README.markdown
@@ -0,0 +1,35 @@
jshint-mode
=======

A work in progress to integrate jshint into emacs via a node.js server, heavily inspited by Kevin Turners lintnode (https://github.com/keturn/lintnode) much thanks.

Dependencies
============

node.js (http://nodejs.org/)

Building
========

git clone git://github.com/daleharvey/jshint-mode.git

Usage
=====

To run from the command line:

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

To run within emacs (add this to your .emacs)

(add-to-list 'load-path "~/lib/jshint-mode")
(require 'flymake-jshint)
(add-hook 'javascript-mode-hook
(lambda () (flymake-mode t)))

You need to start the node server manually

$ node jshint-mode.js

and then run M-x flymake-mode to turn jshint on and off
62 changes: 62 additions & 0 deletions flymake-jshint.el
@@ -0,0 +1,62 @@
;; adapted from http://www.emacswiki.org/emacs/FlymakeJavaScript
;;
;; Installation:
;;
;; (add-to-list 'load-path "~/lib/jshint-mode")
;; (require 'flymake-jshint)
;; (add-hook 'javascript-mode-hook
;; (lambda () (flymake-mode t)))
;;
;; run M-x flymake-mode to turn flymake on and off
;;

(require 'flymake)

(defcustom jshint-mode-node-program "node"
"The program name to invoke node.js."
:type 'string
:group 'flymake-jshint)

(defcustom jshint-mode-location "~/.emacs.d/jshint-mode"
"The directory jshint-mode.js may be found in."
:type 'string
:group 'flymake-jshint)

(defcustom jshint-mode-port 3003
"The port the jshint-mode server runs on."
:type 'integer
:group 'flymake-jshint)

;; doesnt currently work
;; (defun jshint-mode-start ()
;; "Start the jshint-mode server.
;; Uses `jshint-mode-node-program' and `jshint-mode-location'."
;; (interactive)
;; (start-process "jshint-mode-server" "*jshint-mode*"
;; jshint-mode-node-program (concat jshint-mode-location "/jshint-mode.js")
;; "--port" (number-to-string jshint-mode-port)))

(defun flymake-jshint-init ()
(let* ((temp-file (flymake-init-create-temp-buffer-copy
'flymake-create-temp-inplace))
(local-file (file-relative-name
temp-file
(file-name-directory buffer-file-name)))
(jshint-url (format "http://127.0.0.1:%d/jshint" jshint-mode-port)))
(list "curl" (list "--form" (format "source=<%s" local-file)
"--form" (format "filename=%s" local-file)
jshint-url))))

(setq flymake-allowed-file-name-masks
(cons '(".+\\.js$"
flymake-jshint-init
flymake-simple-cleanup
flymake-get-real-file-name)
flymake-allowed-file-name-masks))

(setq flymake-err-line-patterns
(cons '("^Lint at line \\([[:digit:]]+\\) character \\([[:digit:]]+\\): \\(.+\\)$"
nil 1 2 3)
flymake-err-line-patterns))

(provide 'flymake-jshint)
66 changes: 66 additions & 0 deletions jshint-mode.js
@@ -0,0 +1,66 @@
/* 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'),
JSHINT = require('./jshint');

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(sourcedata, filename) {

// TODO: parse options from source file
var passed = JSHINT.JSHINT(sourcedata, {});

return passed ? "jslint: No problems found in " + filename + "\n"
: outputErrors(JSHINT.JSHINT.errors);
}

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

http.createServer(function(req, res) {

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

}).listen(port, host);

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

0 comments on commit 132538f

Please sign in to comment.