Is there a JSX formatter for sublime text?

ReactjsSublimetextReact Jsx

Reactjs Problem Overview


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?

Reactjs Solutions


Solution 1 - Reactjs

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.

Solution 2 - Reactjs

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 />

Solution 3 - Reactjs

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:

Solution 4 - Reactjs

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"

Solution 5 - Reactjs

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"]
}

Solution 6 - Reactjs

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

Solution 7 - Reactjs

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfinView Question on Stackoverflow
Solution 1 - Reactjsroy riojasView Answer on Stackoverflow
Solution 2 - ReactjsShoobahView Answer on Stackoverflow
Solution 3 - ReactjsEugene KulabuhovView Answer on Stackoverflow
Solution 4 - ReactjsbrownmambaView Answer on Stackoverflow
Solution 5 - ReactjsMonochrome YehView Answer on Stackoverflow
Solution 6 - ReactjsAlan P.View Answer on Stackoverflow
Solution 7 - ReactjsAustin CheneyView Answer on Stackoverflow