6

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.)

azangru
  • 2,644
  • 5
  • 34
  • 55

1 Answers1

1

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.

sjamaan
  • 2,282
  • 10
  • 19
  • Sorry for the late reply. I tried your suggestion, but my emacs still seems to keep using jshint despite the code above. I can override this while in a js file by pressing Meta-X and then typing `flycheck-select-checker` and selecting `javascript-eslint`, but when I edited your code to `((js-mode . ((flycheck-select-checker . (javascript-eslint)))))` and placed it in `.dir-locals.el`, this didn't work. Lacking knowledge of elisp, I am completely out of ideas :-( – azangru Aug 04 '15 at 02:47
  • flycheck-select-checker is a function, whereas the other two things are variables. .dir-locals.el will only accept an alist of variable to value mappings, which explains why that doesn't work. I do have `(flycheck-add-mode 'javascript-eslint 'javascript-mode)` in my .emacs; perhaps you don't? That would explain why disabling jshint would not cause eslint to be picked. – sjamaan Aug 05 '15 at 06:33