Detalhes do pacote

esformatter-jsx

royriojas315kMIT8.0.1

esformatter plugin: format javascript files that contain React JSX Elements

esformatter-plugin, esformatter, jsx, react jsx

readme (leia-me)

esformatter-jsx

esformatter plugin: format javascript files that contain React JSX blocks

NPM Version Build Status

Demo

Live demo: esformatter-jsx

Usage with JSFMT

check this guide

best configuration

If you're running into troubles with the formatting applied to your files I found this configuration to work the best:

{
  "jsx": {
    "formatJSX": true, //Duh! that's the default
    "attrsOnSameLineAsTag": false, // move each attribute to its own line
    "maxAttrsOnTag": 3, // if lower or equal than 3 attributes, they will be kept on a single line
    "firstAttributeOnSameLine": true, // keep the first attribute in the same line as the tag
    "formatJSXExpressions": true, // default true, if false jsxExpressions won't be recursively formatted
    "JSXExpressionsSingleLine": true, // default true, if false the JSXExpressions might span several lines
    "alignWithFirstAttribute": false, // do not align attributes with the first tag
    "spaceInJSXExpressionContainers": " ", // default to one space. Make it empty if you don't like spaces between JSXExpressionContainers
    "removeSpaceBeforeClosingJSX": false, // default false. if true <React.Something /> => <React.Something/>
    "closingTagOnNewLine": false, // default false. if true attributes on multiple lines will close the tag on a new line
    "JSXAttributeQuotes": "", // possible values "single" or "double". Leave it as empty string if you don't want to modify the attributes' quotes
    "htmlOptions": {
      // put here the options for js-beautify.html
    }
  }
}

Overview

Esformatter-jsx is a plugin for esformatter meant to allow the code formatting of jsx files or js files with React code blocks, using js-beautify to beautify the "html like" syntax of the react components. Use at your own risk. I have tested this against complex JSX structures and seems to be workfing fine, but bugs might appear, so don't blame me :).

It works for my main use case and I hope it works for you too.

This plugin is based on esformatter-jsx-ignore

If you want a bit of history about what this plugin was develop, check:

So this plugin will turn this:

var React = require('react');

var Hello = React.createClass({
render: function () {
return (<div

className="hello-div">{this.props.message}</div>)
;
}
});

React.render(<Hello
message="world"/>,      document.body);

into:

var React = require('react');

var Hello = React.createClass({
  render: function() {
    return (
    <div className="hello-div">
      {this.props.message}
    </div>
    );
  }
});

React.render(<Hello message="world"/>, document.body);

Installation

$ npm install esformatter-jsx --save-dev

Config

Newest esformatter versions autoload plugins from your node_modules See this

Add to your esformatter config file:

In order for this to work, this plugin should be the first one! (I Know too picky, but who isn't).

{
  "plugins": [
    "esformatter-jsx"
  ],
  // this is the section this plugin will use to store the settings for the jsx formatting
  "jsx": {
    // whether to recursively format jsx expressions with esformatter
    // set this to false if you don't want JSXExpressions to be formatted recursively, like when using problematic plugins
    "formatJSXExpressions": true,
    // By default ObjectExpression and ArrayExpression in JSXExpressions are inlined,
    // if false, the Expression might expand several lines
    "JSXExpressionsSingleLine": true,
    // by default is true if set to false it works the same as esformatter-jsx-ignore
    "formatJSX": true,
    // keep the node attributes on the same line as the open tag. Default is true.
    // Setting this to false will put each one of the attributes on a single line
    "attrsOnSameLineAsTag": true,
     // how many attributes should the node have before having to put each
     // attribute in a new line. Default 1
    "maxAttrsOnTag": 1,
    // if the attributes are going to be put each one on its own line, then keep the first
    // on the same line as the open tag
    "firstAttributeOnSameLine": false,
    // default to one space. Make it empty if you don't like spaces between JSXExpressionContainers
    "spaceInJSXExpressionContainers": " ",
    // align the attributes with the first attribute (if the first attribute was kept on the same line as on the open tag)
    "alignWithFirstAttribute": true,
    "htmlOptions": { // same as the ones passed to js-beautifier.html
      "brace_style": "collapse",
      "indent_char": " ",
      "indent_size": 2,
      "max_preserve_newlines": 2,
      "preserve_newlines": true
      //wrap_line_length: 250
    }
  }
}

The htmlOptions are passed directly to js-beautify, please check its documentation for all the possible options.

Or you can manually register your plugin:

// register plugin
esformatter.register(require('esformatter-jsx'));

Usage

var fs = require('fs');
var esformatter = require('esformatter');
//register plugin manually
esformatter.register(require('esformatter-jsx'));

var str = fs.readFileSync('someKewlFile.js').toString();
var output = esformatter.format(str);
//-> output will now contain the formatted code

See esformatter for more options and further usage info.

License

MIT

changelog (log de mudanças)

esformatter-jsx - Changelog

v8.0.1

  • Other changes

v8.0.0

  • Bug Fixes

v7.4.1

  • Other changes
    • Fix restoreContainers regexp - e11cdb1, wkich, 11/01/2017 03:27:07
  • Build Scripts Changes

v7.4.0

  • Build Scripts Changes

v7.3.3

  • Features

    • Add JSXAttributeQuotes option. Closes #93 - ebbbc7c, Roy Riojas, 29/11/2016 21:20:27

      JSXAttributeQuotes option will now control whether or not the quotes of JSXAttributes should use single or double quotes. Leave it empty to not change the quotes being used.

v7.3.2

  • Documentation

v7.3.0

v7.2.0

  • Other changes

    • Update README.md - d189083, Edan Edison, 02/11/2016 10:58:25

      Missing comma at end of line. Copying and pasting into sublime caused an error message. Simples ;)

      v7.1.0

  • Other changes

v7.0.1

v6.1.2

  • Bug Fixes
    • proper fix for #77. Don't forget to add semicolons if they were there - d9c29c0, Roy Riojas, 12/06/2016 03:26:09

v6.1.1

  • Bug Fixes

v6.1.0

v6.0.0

v5.0.6

  • Build Scripts Changes

v5.0.5

  • Bug Fixes
  • Other changes

v5.0.4

  • Bug Fixes
    • Avoid introducing _AVOID_ESFMT_BUG_WITH_COMMENTS tokens. closes #17. - 0ac45fb, Roy Riojas, 16/05/2016 10:29:51

v5.0.3

  • Other changes
    • Tried the example and forgot to put variable on new line - 5eae9bb, Kevin Simper, 29/04/2016 22:50:19
  • Update with a better react component example - d97f136, Kevin Simper, 29/04/2016 22:48:21

    The component is always wrapped in two parenteses and this shows a more real example on how esformatter will format things.

    v5.0.2

    • Bug Fixes
  • Fix the package name - afc289e, Roy Riojas, 26/04/2016 18:10:37

v5.0.1

  • Bug Fixes
    • test now verify code passes beautification and lint - 44fe581, Roy Riojas, 26/04/2016 18:07:06
  • adding posibility to remove whitespace before closing tag in reformatted jsx elements - 76b0e75, Samvel Avanesov, 23/04/2016 14:55:49
  • Build Scripts Changes

v5.0.0

  • Features

    • Add JSXExpressionsSingleLine property to control if a jsxExpression will be put into a single line or multiple lines - 7f89846, Roy Riojas, 25/03/2016 20:40:47

      Closes #19, #30 and #32

  • Refactoring

v4.1.4

  • Other changes

    • Added tests, reversed assertion order - 20904fc, R, 09/03/2016 00:35:48

      Added tests for new functionality. Reversed mocha test assertion order so that “expected” in console aligns with “expected” files and actual in console aligns with formatted code.

  • undefined

    • Added default values for attr indentation - 0e89c2c, R, 24/02/2016 15:32:42

      Default values if no indent options specified in htmlOptions

    • attribute indentation should respect options - f498454, R, 24/02/2016 15:29:22

      Previously, the attributes would indent either with the first attribute or 2 spaces. Now, it will indent either with the first attribute or by the number of spaces defined in htmlOptions.

v4.1.3

v4.1.2

  • Bug Fixes

v4.1.1

v4.1.0

  • Features

    • Add option to avoid formatting of JSXExpressions. Fixes #48 - 6ce091b, royriojas, 25/12/2015 07:12:53

      This commit adds a new option formatJSXExpressions. The default value is true.

      In case you need to stop this behavior, for example when using a plugin that modify the code like the esformatter-semicolons.

      You should opt out of the formatting of the JSXExpressions to avoid potentially risky issues

      // example of the options
      {
        "jsx": {
          "formatJSX": true,
          "attrsOnSameLineAsTag": true,
          "maxAttrsOnTag": 3,
          "firstAttributeOnSameLine": true,
          "spaceInJSXExpressionContainers": " ",
          "alignWithFirstAttribute": false,
          // default is true, setting it to false JSXExpressions won't be recursively formatted
          "formatJSXExpressions": false,
          "htmlOptions": {
            "brace_style": "collapse",
            "indent_char": " ",
            "indent_size": 2,
            "max_preserve_newlines": 2,
            "preserve_newlines": true
          }
        }
      }
      

v4.0.6

  • Bug Fixes
    • properly format code with comments in decorators. Fix #45 - ac2dc2d, royriojas, 01/12/2015 14:39:24

v4.0.5

  • Bug Fixes
    • support async ArrowFunctionExpressions - 8ab19cb, royriojas, 30/11/2015 14:00:17

v4.0.4

  • Build Scripts Changes
    • Use the latest esformatter for the tests - af472da, Roy Riojas, 22/11/2015 17:32:11

v4.0.3

  • Bug Fixes
    • Proper fix for #41. Properly handle the async/await in class methods - ed76453, Roy Riojas, 22/11/2015 17:31:01

v4.0.2

  • Bug Fixes
    • Properly format code with async token on function expressions. Fixes #42 - 5715fcb, Roy Riojas, 22/11/2015 17:09:44

v4.0.1

  • Bug Fixes
    • Properly format blocks of code containing comments at the end of a function or method body. - 1da006e, Roy Riojas, 22/11/2015 16:33:23

v4.0.0

  • Bug Fixes
    • Properly format blocks of code containing async/await tokens. Fixes #41 - 6a0e044, Roy Riojas, 22/11/2015 16:06:19

v3.0.0

  • Enhancements
    • Use babylon directly instead of babel-core Fixes #38 - b4498f3, royriojas, 15/11/2015 17:14:59
  • Bug Fixes
    • use babylon directly instead of thru babel - bb595fb, royriojas, 15/11/2015 17:14:59

v2.3.11

v2.3.10

  • Bug Fixes
    • Properly format code that contains a Bind Expression. Fixes #39 - 5e3f334, royriojas, 13/11/2015 06:42:14

v2.3.9

  • Bug Fixes
    • properly support export declarations, even the ones that babel allows. Fixes #34 - cc69735, royriojas, 29/10/2015 12:05:29

v2.3.8

v2.3.7

v2.3.6

  • Bug Fixes
    • require acorn only if no other parser was set. Fixes #31 - 4ae4479, royriojas, 11/10/2015 12:55:10

v2.3.5

  • Bug Fixes
  • Build Scripts Changes
    • Update esformatter-ignore to latest to properly ignore lines that have no empty spaces at the beginning - e0ce5f3, royriojas, 08/10/2015 13:32:31

v2.3.4

  • Bug Fixes

v2.3.3

  • Bug Fixes
    • Issue with the parser returning negative indexes - eafe7bf, royriojas, 02/10/2015 02:13:17

v2.3.2

  • Features
    • Support SpreadProperties outside of JSX blocks. Fixes #27 - 2ad355d, royriojas, 02/10/2015 01:34:20

v2.3.1

  • Features
    • Support for decorators and class properties. Fixes #26 - 8d546ef, royriojas, 02/10/2015 01:02:16

v2.3.0

  • Features

    • support decorators and static props - e6e1dc2, royriojas, 02/10/2015 00:25:39

      Please note that static props are just ignored from beautification all together for now

      It shouldn't be difficult to format it, but for now it is just OK

v2.2.0

  • Refactoring
    • use acorn-babel for more es6, es7 features - 6043e32, royriojas, 01/10/2015 21:38:15

v2.1.4

  • Enhancements
    • properly ignore blocks with ignore block comments - b8f5c52, royriojas, 01/10/2015 12:24:18

v2.1.3

  • Enhancements

    • ignore blocks using esformatter-ignore - 460d6e8, royriojas, 01/10/2015 01:40:21

      Required because espree still does not recognize some of the new fancy syntax of ES6 and ES7 and that makes it break badly when trying to beautify new files

v2.1.2

v2.1.1

  • Enhancements

v2.1.0

  • Enhancements

v2.0.11

  • Bug Fixes
    • properly handle ObjectExpressions and ArrayExpressions inside JSXContainers - 53941aa, royriojas, 12/08/2015 02:46:59

v2.0.10

  • Bug Fixes
    • prevent other plugins from messing with the expression containers - 161de77, royriojas, 07/08/2015 13:26:51

v2.0.9

  • Bug Fixes
    • exclude identifiers from recursive formatting - db23cd4, royriojas, 07/08/2015 02:16:39

v2.0.8

  • Bug Fixes
    • properly format JSXExpression containers content. Fixes #14 - d8d21e7, royriojas, 07/08/2015 01:54:59

v2.0.7

  • Bug Fixes
    • nested JSXElements inside JSXExpressionContainers now are properly beautified - 3ef3595, royriojas, 06/08/2015 19:35:14

v2.0.6

  • Documentation

    • document new option spaceInJSXExpressionContainers - 5601b77, royriojas, 05/08/2015 03:02:19

        "spaceInJSXExpressionContainers": " " // set it to "" to remove spaces in JSXExpressionContainers
      

v2.0.5

  • Build Scripts Changes
  • Enhancements
    • Add option to add spaces around JSXExpressionContainers spaceInJSXExpressionContainers. Closes #11 - 2a11c8b, royriojas, 05/08/2015 02:57:19

v2.0.4

  • Bug Fixes
    • keep selfclosing char (/>) in the same of the last attribute - e066931, royriojas, 28/07/2015 19:34:19

v2.0.3

  • Bug Fixes

    • Major issue with nodes being deleted because some weird issue with rocambole. - 48a0a71, royriojas, 28/07/2015 04:56:49

      Switched back to use falafel-espree. Definitively it works better and do not destroy the code

v2.0.2

  • Build Scripts Changes
    • Fix pre-version script so it executes tests only once - 384d14e, royriojas, 28/07/2015 03:10:37

v2.0.1

v2.0.0

  • Build Scripts Changes
  • Refactoring
    • Big refactoring of the beautifier to address several bugs. Fixes #4, Fixes #9 - d1630a9, royriojas, 27/07/2015 17:57:02

v1.3.0

  • Enhancements
    • Make more predictive the parsing of jsx tags - ca7f190, royriojas, 17/06/2015 05:07:13
  • Bug Fixes
    • remove initial and final text surrounding a tag to prevent react from creating span tags - 916c0b2, royriojas, 17/06/2015 04:05:36

v1.1.1

  • Build Scripts Changes
  • Refactoring

v1.0.9

  • Features
    • Ternary operators in jsx will try to remain in the same line - eb1ee17, royriojas, 16/06/2015 06:22:31
  • Bug Fixes

    • Fix for nested jsx structures issue - f10a429, Roy Riojas, 09/03/2015 15:45:51

      Fixes an issue reported under esformatter-jsx-ignore when using nested jsx blocks.

      Check the readme for details about this issue.

  • Documentation

    • minor cosmetic change to make the comments in the json structure be properly highlighted - 5f76bb9, Roy Riojas, 03/03/2015 07:20:04
  • Add comment about the best configuration to work with JSX files - 6a1135f, Roy Riojas, 02/03/2015 13:59:19
  • Build Scripts Changes
  • Tests Related fixes
    • Add test for the case of an already formatted component - 4d28556, Roy Riojas, 27/02/2015 04:31:11

tag attributes

  • Bug Fixes
    • prevent some tags from been formatted using the same escape list from js-beautify.html related to #1 - 3c2e2f7, Roy Riojas, 01/03/2015 23:27:37
  • only try to format the attributes if the flag attrsOnSameLineAsTag is not true - e1b9525, Roy Riojas, 01/03/2015 21:29:50
  • Documentation
    • Fixed quotes in the json configuration section in the README - 4ab8683, Roy Riojas, 01/03/2015 19:56:32
  • Features
    • Add option to format the attributes of a tag. Fix #1 - 653fad8, Roy Riojas, 01/03/2015 19:53:15

v1.0.1

  • Documentation

v1.0.0

  • Add a note about the failure to load the plugin using the configuration JSON - 13a137a, Roy Riojas, 24/02/2015 05:52:32
  • Other changes
    • Add esprima-fb as peer dependecy to make travis happy - 1e73618, Roy Riojas, 24/02/2015 06:14:20