You are here: Home Tech Flymake Mode for Emacs/JavaScript V8 edition

Flymake Mode for Emacs/JavaScript V8 edition

by Reed O'Brien last modified Dec 07, 2011 02:45 PM
V8 powered jslint or jshint with Flymake in Aquamacs Emacs

Now using jshint-V8

  • The following should still work though

Overview

Following are instructions for setting up JSLint to hightlight syntax errors live in Emacs via flymake-mode.

Last year I wrote up the setup with rhino. Unfortunately I have found that to be very slow and almost un-usable with js files of any size. So this time we will do it with V8.

The steps to get it going are roughly as follows:

  1. Install and configure V8.
  2. Clone jslint-v8 and compile it for use.
  3. Setup Emacs to use your program on javascript files with .
  4. Profit! Or at least save some time not waiting for rhino powered jslint

NB: I did this on OSX (10.7.0) but if you have the prerequisites installed it should work fine on BSD and Linux as well.

Assumption You have python setup already with virtualenv

Setup V8

There are acouple steps to set up V8

Create a virtualenv:

$ cd /usr/local/dev
$ virtualenv v8-env --no-site-packages --distribute
$ source v8-env/bin/activate

Install scons:

$ easy_install scons

Get and build V8:

$ svn checkout http://v8.googlecode.com/svn/trunk/ v8-read-only
$ cd v8-read-only
$ scons snapshot=on library=shared arch=x64 d8

Warning

with OSX 10.7 Lion scons fails with ImportError: No module named SCons.Script so you need to export SCONS_LIB_DIR=/usr/local/src/v8-env/lib/python2.7/site-packages/scons-2.1.0-py2.7.egg/scons-2.1.0 to make it work

That was easy no? snapshot=on reportedly make it load faster. arch=x64 targets 64 architecture and d8 tells it to build the developer shell in release mode. Let's test it:

$ echo 'print("Hello, world!");' > test.js
$ ./d8 test.js
Hello World!!

If it prints Hello world!! it worked. So let's put it someplace permanent:

$ sudo cp d8 /usr/local/bin/
$ sudo cp libv8* /usr/local/lib/
$ cp include/* /usr/local/include/

Compile jslint-v8

Clone the project:

$ git clone https://github.com/geekq/jslint-v8

Get the latest jslint (You prolly dont' have to do this as YMMV):

$ cd jslint-v8
$ curl -O https://raw.github.com/douglascrockford/JSLint/1ebebf3313cfeede287120a79fd652df0d70af35/jslint.js

Compile it:

$ g++ -o jslint jslint.cpp -I/usr/local/include/ -lv8 -L/usr/local/lib -lpthread

Test it:

$ echo 'print("Hello, world!");' > test.js
$ ./jslint test.js
Lint at line 1 character 1: Missing "use strict" statement.
print('Hello World!!')

Lint at line 1 character 23: Missing semicolon.
print('Hello World!!')

Move it into place:

$ sudo cp jslint /usr/local/bin/

Setup Emacs

Create a flymake-jslint.el file

Create flymake-jslint.el in your emacs site direcory or somewher eon your load path with the folllowing contents:

(require 'flymake)

(defun flymake-jslint-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))))
    (list "/usr/local/bin/jslint" (list local-file))))

(setq flymake-allowed-file-name-masks
      (cons '(".+\\.js$"
              flymake-jslint-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-jslint)

Import it in your .emacs

Add the following to .emacs:

(require 'flymake-jslint)
(add-hook 'javascript-mode-hook
          (lambda () (flymake-mode 1)))

Conclusion

Now start emacs (or load .emacs) and open a javascript file... You should have pyflakes running jslint against your code.

Thanks to Alexander Kabakov for pertinent typographical corrections

Share this: