95

I'm using Sublime Text as a text editor.

There's a jsFormat for formatting javascript files but I can't find one for JSX.

How you guys deal with formatting JSX?

Kaloyan Kosev
  • 12,483
  • 8
  • 59
  • 90
fin
  • 1,299
  • 1
  • 10
  • 20
  • Yes, look for the jsx textmate bundle which can be used with pretty much any editor. – Brigand Dec 19 '14 at 02:01
  • it seems like a syntax hightlighter only, i'm looking for the formatter which does ident and sort of things – fin Dec 19 '14 at 02:59
  • 2
    I don't know of one. The react google group would be a better place to ask. – Brigand Dec 19 '14 at 05:16
  • can't find a good solution either. Currently it's possible to parse the source with esprima-fb, and use escodegen-jsx to generated formatted output. Formatting options are limited, and you lose white lines... plus escodegen-jsx is a fork without issue tracking.. not sure if it will be actively maintained. I wonder how fb deals with their jsx code base. – user1600124 Jan 08 '15 at 03:17
  • the beta verion of esformatter seem to have some ability of formatting JSX cause it's using esprima-fb, but still not production ready yet – fin Jan 10 '15 at 15:09

7 Answers7

60

Update 4

Check prettier, not that configurable as esformatter, but currently used to format some big projects (like React itself)

Update 3

Check sublime jsfmt. If you add esformatter-jsx to the config and install the package inside the forlder for sublime-jsfmt. You will be able to format JSX files directly from Sublime. Here is a guide for that

Update 2

from the command line you can also use esbeautifier. It is a wrapper around esformatter that accept a list of globs to format

# install the dependencies globally
npm i -g esbeautifier

# beautify the files under src/ and specs/ both .js and .jsx
esbeautifier src/**/*.js* specs/**/*.js*

Update

So I ended up doing a plugin for esformatter to enable the formatting of JSX files:

https://www.npmjs.com/package/esformatter-jsx

Here is a live demo on requirebin

It should be somehow feasible to call esformatter from Sublime passing the current file as the argument. In any case to use it from the command line you can follow these instructions:

From the command line it can be used like this:

# install the dependencies globally
npm i -g esformatter esformatter-jsx

# call it (this will print to stdout)
esformatter --plugins=esformatter-jsx ./path/to/your/file

# to actually modify the file
esformatter --plugins=esformatter-jsx ./path/to/your/file > ./path/to/your/file

# to specify a config file (where you can also specify the plugins)
# check esformatter for more info about the configuration options
esformatter -c ./path/to/.esformatter ./path/to/your/file > ./path/to/your/file

==== old answer below ===

So if what you're looking is just to make your jsx files to be formatted while allowing the jsx syntax (basically beautify all the javascript syntax and ignore jsx tags, meaning leave them as is), this is what I'm doing using esformatter

// needed for grunt.file.expand
var grunt = require('grunt');

// use it with care, I haven't check if there
// isn't any side effect from using proxyquire to 
// inject esprima-fb into the esformatter
// but this type of dependency replacement
// seems to be very fragile... if rocambole deps change 
// this will certainly break, same is true for esformatter
// use it with care
var proxyquire = require('proxyquire');
var rocambole = proxyquire('rocambole', {
  'esprima': require('esprima-fb')
});

var esformatter = proxyquire('esformatter', {
  rocambole: rocambole
});

// path to your esformatter configuration
var cfg = grunt.file.readJSON('./esformatter.json');

// expand the files from the glob
var files = grunt.file.expand('./js/**/*.jsx');

// do the actual formatting
files.forEach(function (fIn) {
  console.log('formatting', fIn);
  var output = esformatter.format(grunt.file.read(fIn), cfg);
  grunt.file.write(fIn, output);
});

I would actually like that esformatter use a version of rocambole that use esprima-fb instead of esprima, to avoid proxyquire.

roy riojas
  • 2,406
  • 1
  • 28
  • 30
  • 1
    This only worked with esformatter-jsx-ignore for me, but that is good enough. Thanks! – Jasper Jun 27 '15 at 16:08
  • I'm using esformatter-jsx and It works as I expected. However, I also had to add "esformatter-jsx" to "options":{"plugins":{"esformatter-jsx"}} in "Preferences>Package Settings>Sublime JSFMT>Settings - Default" in Sublime 3 to make it works – Kuma Oct 01 '15 at 06:57
  • This is a great answer, I have this used tool for awhile now, but we have switched to using es6/es7 with babel and it doesn't work anymore. –  Oct 05 '15 at 13:25
  • @Rkhayat are you sure? latest version of `esformatter-jsx` should support a bunch of ES6/ES7. If there is something not working you can file a bug, and as workaround you can mark blocks that don't work in `/*esfmt-ignore-start*/ code here /*esfmt-ignore-end*/` – roy riojas Oct 05 '15 at 17:16
  • make sure to use the latest esformatter as well as previous ones don't recognize ES6/ES7 syntax – roy riojas Oct 05 '15 at 17:28
  • @royriojas I am using `@Radium `as a decorator, and the linter blows up with "unexpected token ILLEGAL" unless I remove it, also on static propTypes. How do I go about upgrading it? –  Oct 07 '15 at 17:49
  • @Rkhayat latest version of it handles decorators and class properties as well... https://github.com/royriojas/esformatter-jsx/blob/master/changelog.md#v231 – roy riojas Oct 07 '15 at 17:51
  • Does this have to be installed per project? How do I go about upgrading it? –  Oct 07 '15 at 17:54
  • you can use [esbeautifier](https://github.com/royriojas/esbeautifier) from the command line and you can also use it in sublime with JSFMT, check this guide: https://github.com/royriojas/esformatter-jsx/wiki/Usage-with-jsfmt – roy riojas Oct 07 '15 at 18:01
  • I believe I followed the instructions for JSFMT / Sublime, the error now states `The formatting failed please check the console for more details` https://github.com/ionutvmi/sublime-jsfmt/blob/master/jsfmt-sublime.py#L64 –  Oct 07 '15 at 18:02
  • 1
    check your sublime console, in mac to open the console press ctrl+` you can open a ticket on esformatter-jsx repo or in JSFMT repo with the error you are seeing in the console – roy riojas Oct 07 '15 at 18:53
  • I see that Rdio built (or at least maintained) JSFMT, but since they filed for bankruptcy and got bought by Pandora, I haven't seen progress on the project (last commit Feb 3, 2016): https://github.com/rdio/jsfmt/graphs/contributors?from=2014-03-02&to=2017-03-04&type=c. – mlunoe Mar 15 '17 at 18:37
  • yeah to be fair now I would probably suggest people to use prettier for new projects https://github.com/prettier/prettier, there might be already an addon for sublime. – roy riojas Mar 15 '17 at 23:11
  • 2
    prettier does the job for me. sublime plugin here https://github.com/jonlabelle/SublimeJsPrettier and it also has eslint support https://github.com/prettier/prettier#eslint . Glad this question is finally accepted after nearly 3 years. – fin Sep 29 '17 at 00:57
57

There is a setting in the HTML-CSS-JS Prettify plugin that allows you to ignore xml syntax in the js/jsx file. That way it doesn't mess up the jsx code. The setting is: "e4x": true in the "js" section of the settings file

Preferences > Package Settings > HTML\CSS\JS Prettify > Set Prettify Preferences

This does not work well if you have self closing tags eg. tags ending in />

Shoobah
  • 697
  • 5
  • 7
  • 1
    This still works really well with the latest versions of babel-sublime and HTML-CSS-JS Prettify. Just make sure you add "jsx" or whatever file extension you use to "allowed_file_extensions". – damd Apr 01 '16 at 13:39
  • Is this answer outdated? There is not `/>` in my code, but it still does not work – Andy Darwin Apr 11 '17 at 03:36
  • 2
    prettier (aka jsprettier) for sublime is by far the best!! See the answer below: http://stackoverflow.com/a/42169688/2178112 – tnrich Apr 19 '17 at 01:51
20

You can install a JsPrettier package for Sublime 2 & 3. It's a fairly new JavaScript formatter (at the time of writing this: Feb-2017). It supports most of the latest developments like: ES2017, JSX, and Flow.

Quickstart

  1. Install prettier globally using terminal: $ npm install -g prettier
  2. In Sublime go to Tools -> Command Palette... -> Package Control: Install Package, type the word JsPrettier, then select it to complete the installation.
  3. Format your file using context menu inside the editor or bind it to a keyboard shortcut: { "keys": ["super+b"], "command": "js_prettier" }

Links:

Eugene Kulabuhov
  • 2,349
  • 1
  • 26
  • 25
19

To add to what @Shoobah said:

There is a setting in the HTML-CSS-JS Prettify plugin that allows you to ignore xml syntax in the js/jsx file. That way it doesn't mess up the jsx code. The setting is: "e4x": true in the "js" section of the settings file

Go to: Preferences > Package Settings > HTML\CSS\JS Prettify > Set Prettify Preferences

Go to "js" section:

Add "jsx" to the "allowed_file_extension", and then change "e4x" to "true"

Peter O.
  • 32,158
  • 14
  • 82
  • 96
brownmamba
  • 755
  • 1
  • 8
  • 16
16

the answer in the internet that always told you set 'e4x' to true, but sometimes, we have to set option of 'format_on_save_extensions' then add 'jsx' in array

modify jsFormat.sublime-settings

{
  "e4x": true,
  "format_on_save": true,
  "format_on_save_extensions": ["js", "json", "jsx"]
}
Monochrome Yeh
  • 180
  • 1
  • 8
  • I did as per told by you, but it didn't work for me. :( – Yash Vekaria Apr 27 '16 at 05:58
  • Where is _"jsFormat.sublime-settings"_ found? so that I can modify `"format_on_save_extensions": ["js", "json", "jsx"]`. The closest I find about file extensions is: `"js": { "allowed_file_extensions": ["js", "json",...]` – Chris22 Oct 18 '16 at 23:31
  • You can find it here: Preferences > Package Settings > jsFormat > Settings - User. Didn't work for me though, still indents JSX weirdly :( – Niclas Oct 19 '16 at 19:09
  • Have been looking for a solution and this one solved my problem, thanks ! – Al-Maamari Tareq Jan 22 '17 at 02:20
4

Using Sublime's Package Installer, install Babel. Then:

  1. Open a .jsx file.
  2. Select View from the menu,
  3. Then Syntax -> Open all with current extension as... -> Babel -> JavaScript (Babel).
Alan P.
  • 2,898
  • 6
  • 28
  • 52
1

Not specifically for Sublime Text, but there is a beautifier in JavaScript for React JSX.

http://prettydiff.com/?m=beautify claims to support JSX at: http://prettydiff.com/guide/react_jsx.xhtml