Update Jun 21 2014: add another better solution that uses web-mode
1. Syntax highlighting
If you are working with Javascript, especially ReactJS, you definitely have known about JSX, the XML syntax inside of Javascript. This section will demonstrate how to setup Emacs for working with JSX files using web-mode, an autonomous emacs major-mode for editing web templates. For web-mode to work properly with JSX files, add this to your .emacs
This is how web-mode indents and highlights my jsx files (taken from my Emacs).
2. JSX Syntax Checking
Next, we need a tool for syntax checking. For Javascript, we have jslint
(or
jshint
), which I have previously written another post about how to set it up
in Emacs here
Emacs - Setup JSHint for on-the-fly (potential) errors checking.
For JSX, there is a similar tool called jsxhint
. Of course, you need to
install it before you can use
You can test whether the tool works correctly by trying to check the syntax of a JSX file
Now, to integrate it into Emacs, you need to install flycheck
(a modern
version of flymake
) through Emacs Packages Manager. Now, I will show 2 ways of
setting it corresponding to the 2 solutions that I mentioned in part 1 (web-mode
and jsx-mode).
Note: you need to make sure that jsxhint
is located inside your Emacs’
path (or install exec-path-from-shell
to import your shell’s PATH
automatically).
Add this to your .emacs to activate the checker for .jsx files
JSX Auto complete, Snippets and Suggestion
Finally, the greatest thing when using Emacs for JS development is the Suggestion feature with TernJS. The steps on how to set up TernJS for both JS and JSX mode is presented in this post Emacs - Javascript Completion and Refactoring.