Repository: yannickcr/eslint-plugin-react
Pull request #506 (2016-03-18 14:23:07)

Add auto fix for jsx-equals-spacing

coveralls: 
[![Coverage Status](https://coveralls.io/builds/5466677/badge)](https://coveralls.io/builds/5466677)

Coverage increased (+0.007%) to 97.041% when pulling **682d528811659273059560d6248548144c1a3c54 on peet:autofix-jsx-equals-spacing** into **33652317e7f1e96ad0b2377a4bad7d33354038b4 on yannickcr:master**.


Patch:
diff --git a/README.md b/README.md
index 556f000b..715cd712 100644
--- a/README.md
+++ b/README.md
@@ -101,7 +101,7 @@ The plugin has a [recommended configuration](#user-content-recommended-configura
 * [jsx-boolean-value](docs/rules/jsx-boolean-value.md): Enforce boolean attributes notation in JSX (fixable)
 * [jsx-closing-bracket-location](docs/rules/jsx-closing-bracket-location.md): Validate closing bracket location in JSX
 * [jsx-curly-spacing](docs/rules/jsx-curly-spacing.md): Enforce or disallow spaces inside of curly braces in JSX attributes (fixable)
-* [jsx-equals-spacing](docs/rules/jsx-equals-spacing.md): Enforce or disallow spaces around equal signs in JSX attributes
+* [jsx-equals-spacing](docs/rules/jsx-equals-spacing.md): Enforce or disallow spaces around equal signs in JSX attributes (fixable)
 * [jsx-handler-names](docs/rules/jsx-handler-names.md): Enforce event handler naming conventions in JSX
 * [jsx-indent-props](docs/rules/jsx-indent-props.md): Validate props indentation in JSX (fixable)
 * [jsx-indent](docs/rules/jsx-indent.md): Validate JSX indentation
diff --git a/docs/rules/jsx-equals-spacing.md b/docs/rules/jsx-equals-spacing.md
index 7c202ecd..d32d7ef8 100644
--- a/docs/rules/jsx-equals-spacing.md
+++ b/docs/rules/jsx-equals-spacing.md
@@ -2,6 +2,8 @@
 
 Some style guides require or disallow spaces around equal signs.
 
+**Fixable:** This rule is automatically fixable using the `--fix` flag on the command line.
+
 ## Rule Details
 
 This rule will enforce consistency of spacing around equal signs in JSX attributes, by requiring or disallowing one or more spaces before and after `=`.
diff --git a/lib/rules/jsx-equals-spacing.js b/lib/rules/jsx-equals-spacing.js
index c4e00ec3..579088e9 100644
--- a/lib/rules/jsx-equals-spacing.js
+++ b/lib/rules/jsx-equals-spacing.js
@@ -43,14 +43,20 @@ module.exports = function(context) {
               context.report({
                 node: attrNode,
                 loc: equalToken.loc.start,
-                message: 'There should be no space before \'=\''
+                message: 'There should be no space before \'=\'',
+                fix: function(fixer) {
+                  return fixer.removeRange([attrNode.name.range[1], equalToken.start]);
+                }
               });
             }
             if (spacedAfter) {
               context.report({
                 node: attrNode,
                 loc: equalToken.loc.start,
-                message: 'There should be no space after \'=\''
+                message: 'There should be no space after \'=\'',
+                fix: function(fixer) {
+                  return fixer.removeRange([equalToken.end, attrNode.value.range[0]]);
+                }
               });
             }
             break;
@@ -59,14 +65,20 @@ module.exports = function(context) {
               context.report({
                 node: attrNode,
                 loc: equalToken.loc.start,
-                message: 'A space is required before \'=\''
+                message: 'A space is required before \'=\'',
+                fix: function(fixer) {
+                  return fixer.insertTextBefore(equalToken, ' ');
+                }
               });
             }
             if (!spacedAfter) {
               context.report({
                 node: attrNode,
                 loc: equalToken.loc.start,
-                message: 'A space is required after \'=\''
+                message: 'A space is required after \'=\'',
+                fix: function(fixer) {
+                  return fixer.insertTextAfter(equalToken, ' ');
+                }
               });
             }
             break;


Test patch:
diff --git a/tests/lib/rules/jsx-equals-spacing.js b/tests/lib/rules/jsx-equals-spacing.js
index dce47af1..2e02ab67 100644
--- a/tests/lib/rules/jsx-equals-spacing.js
+++ b/tests/lib/rules/jsx-equals-spacing.js
@@ -83,6 +83,7 @@ ruleTester.run('jsx-equals-spacing', rule, {
 
   invalid: [{
     code: '<App foo = {bar} />',
+    output: '<App foo={bar} />',
     parserOptions: parserOptions,
     errors: [
       {message: 'There should be no space before \'=\''},
@@ -90,6 +91,7 @@ ruleTester.run('jsx-equals-spacing', rule, {
     ]
   }, {
     code: '<App foo = {bar} />',
+    output: '<App foo={bar} />',
     options: ['never'],
     parserOptions: parserOptions,
     errors: [
@@ -98,6 +100,7 @@ ruleTester.run('jsx-equals-spacing', rule, {
     ]
   }, {
     code: '<App foo ={bar} />',
+    output: '<App foo={bar} />',
     options: ['never'],
     parserOptions: parserOptions,
     errors: [
@@ -105,6 +108,7 @@ ruleTester.run('jsx-equals-spacing', rule, {
     ]
   }, {
     code: '<App foo= {bar} />',
+    output: '<App foo={bar} />',
     options: ['never'],
     parserOptions: parserOptions,
     errors: [
@@ -112,6 +116,7 @@ ruleTester.run('jsx-equals-spacing', rule, {
     ]
   }, {
     code: '<App foo= {bar} bar = {baz} />',
+    output: '<App foo={bar} bar={baz} />',
     options: ['never'],
     parserOptions: parserOptions,
     errors: [
@@ -121,6 +126,7 @@ ruleTester.run('jsx-equals-spacing', rule, {
     ]
   }, {
     code: '<App foo={bar} />',
+    output: '<App foo = {bar} />',
     options: ['always'],
     parserOptions: parserOptions,
     errors: [
@@ -129,6 +135,7 @@ ruleTester.run('jsx-equals-spacing', rule, {
     ]
   }, {
     code: '<App foo ={bar} />',
+    output: '<App foo = {bar} />',
     options: ['always'],
     parserOptions: parserOptions,
     errors: [
@@ -136,6 +143,7 @@ ruleTester.run('jsx-equals-spacing', rule, {
     ]
   }, {
     code: '<App foo= {bar} />',
+    output: '<App foo = {bar} />',
     options: ['always'],
     parserOptions: parserOptions,
     errors: [
@@ -143,6 +151,7 @@ ruleTester.run('jsx-equals-spacing', rule, {
     ]
   }, {
     code: '<App foo={bar} bar ={baz} />',
+    output: '<App foo = {bar} bar = {baz} />',
     options: ['always'],
     parserOptions: parserOptions,
     errors: [