16

I have a default setup of js2-mode. This provides nice syntax highlighting and checking but I am now working on a project with a different set of styles (specifically this new project does not use semicolons).

This causes every single line of code to be underlined in js2-mode with the warning: missing ; after statement

Is there a way that I can disable the syntax checking in js2-mode?

Or, even better, is there a way to use a diferent syntax checker?

I have Flycheck mode installed and running for my .js files but I just do not have any syntax checkers enabled (i.e. all checkers are set to nil and not found). It would be great to know how to enable one of these and disable the built in js2 one.

I know the views on semicolon insertion vary but this project follows the "standard style" for which there is a syntax checker in Flycheck and I am just interested in if I can switch the checker.

0

2 Answers 2

20

The warning comes from JS2 Mode which implements its own parser for Javascript and warns about a couple of issues. As noted in the other answer the particular warning you observed is controlled by the user option js2-strict-missing-semi-warning which you can set to nil in your init file or via M-x customize so as to turn off the warning.

When using JS2 Mode together with Flycheck we generally recommend to turn off all warnings of JS2 Mode by setting the variables js2-mode-show-parse-errors and js2-mode-show-strict-warnings to nil as in almost all cases Flycheck's syntax checking provides superior error reporting.


To select the javascript-standard syntax checker for the current buffer use C-c ! s javascript-standard. You can permanently enable this syntax checker for your specific project by creating a .dir-locals.el file in the top level directory of the project with the following contents:

((js2-mode
  (flycheck-checker . javascript-standard)))

Emacs automatically loads these Directory Variables when visiting a file below the directory, and thus automatically picks the standard syntax checker for your project.

3
  • For some reason, even when js2-mode-show-parse-errors is set to nil, I still see the errors: C-h v js2-mode-show-parse-errors returns js2-mode-show-parse-errors is a variable defined in js2-mode.el'. Its value is nil. Original value was t but I still see the under lines. Do you know why this could be?
    – Startec
    Sep 18, 2016 at 1:54
  • Ah, there is a small type in your answer, I believe you meant js2-mode-show-strict-warnings nil. I added this as an edit.
    – Startec
    Sep 18, 2016 at 2:00
  • @Startec Oh sorry thanks for fixing my mistake.
    – user227
    Sep 18, 2016 at 6:07
2

You could customize the js2-strict-missing-semi-warning variable to nil so that it it doesn't warn you for this particular problem, or you could use js-mode instead of js2-mode.

Of the two options, I recommend changing your style guide; automatic semicolon insertion in JS is a trap. Still, I recognize that that might not always be possible.

2
  • 4
    -1: Please remove the last paragraph, it's off-topic. Discussion of Javascript styles is not for this site.
    – user227
    Sep 9, 2016 at 9:32
  • 3
    No thank you. I offer this advice as an engineer with 20 years of experience with Javascript. Certainly adapting emacs to fit the local conventions is doable (and it's what emacs is best at), but some things are more trouble than they're worth, and automatic semicolon insertion is at the top of that list. Putting that aside, tweaking js2-mode's behavior is your best option, as it's the simplest. I don't actually know anything about flymake though, so I can't help you there.
    – db48x
    Sep 9, 2016 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.