Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use flycheck with eslint on emacs when editing files from a particular project

Normally, when editing JavaScript on emacs, I use flycheck with jshint to check for syntax errors. So I have jshint installed globally, and the following command in the .emacs file to use flycheck in js-mode:

(require 'flycheck)
(add-hook 'js-mode-hook
          (lambda () (flycheck-mode t)))

But I also contribute to a project where they use eslint to define syntax rules. I have a .dir-locals.el file in that project folder, which at the moment only defines the required tabulation:

((nil . ((tab-width . 4)
         (js-indent-level . 4)))

 (js-mode . ((tab-width . 4)
             (js-indent-level . 4)))

 (html-mode . ((tab-width . 4)
               (sgml-basic-offset . 4))))

Is it possible, perhaps with the help of .dir-locals.el file, to tell emacs to use flycheck with eslint in this particular project folder while keep using flycheck with jshint in the rest of the projects? I've heard the following lines should do the trick for switching from jshint to eslint, but am not entirely sure where to add them:

(setq flycheck-disabled-checkers '(javascript-jshint))
(setq flycheck-checkers '(javascript-eslint))

(As you can guess, I am not at all good with setting up emacs, so I will be very grateful for specific instructions.)

like image 293
azangru Avatar asked Mar 15 '15 21:03

azangru


1 Answers

I am in the same situation, except I want eslint to be the default and use jshint in some older "legacy" projects.

dir-locals simply contains a nested alist mapping modes to variable/value pairs that need to be set in that mode. So your setq can trivially be translated to such an alist:

((js-mode . ((flycheck-disabled-checkers . (javascript-jshint))
             (flycheck-checkers . (javascript-eslint)))))

It is actually better to only set flycheck-disabled-checkers because the checkers already includes both javascript-jshint and javascript-eslint, (it is just disabled in your config), and flycheck-checkers is marked as a "risky" variable (because this will cause arbitrary programs to be run on the input) so it will cause a question to be asked whenever you open a JS file in the dir.

like image 100
sjamaan Avatar answered Nov 15 '22 09:11

sjamaan