From ef0d1d4fdabe2b44ade0e4d6aacd6c1522b7cdf4 Mon Sep 17 00:00:00 2001 From: Peet Date: Fri, 18 Mar 2016 14:27:08 +0000 Subject: [PATCH] Add auto fix for jsx-equals-spacing --- README.md | 2 +- docs/rules/jsx-equals-spacing.md | 2 ++ lib/rules/jsx-equals-spacing.js | 20 ++++++++++++++++---- tests/lib/rules/jsx-equals-spacing.js | 9 +++++++++ 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 556f000b19..715cd712c3 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 7c202ecd14..d32d7ef89e 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 c4e00ec3d9..579088e995 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; diff --git a/tests/lib/rules/jsx-equals-spacing.js b/tests/lib/rules/jsx-equals-spacing.js index dce47af12d..2e02ab67f1 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: '', + output: '', parserOptions: parserOptions, errors: [ {message: 'There should be no space before \'=\''}, @@ -90,6 +91,7 @@ ruleTester.run('jsx-equals-spacing', rule, { ] }, { code: '', + output: '', options: ['never'], parserOptions: parserOptions, errors: [ @@ -98,6 +100,7 @@ ruleTester.run('jsx-equals-spacing', rule, { ] }, { code: '', + output: '', options: ['never'], parserOptions: parserOptions, errors: [ @@ -105,6 +108,7 @@ ruleTester.run('jsx-equals-spacing', rule, { ] }, { code: '', + output: '', options: ['never'], parserOptions: parserOptions, errors: [ @@ -112,6 +116,7 @@ ruleTester.run('jsx-equals-spacing', rule, { ] }, { code: '', + output: '', options: ['never'], parserOptions: parserOptions, errors: [ @@ -121,6 +126,7 @@ ruleTester.run('jsx-equals-spacing', rule, { ] }, { code: '', + output: '', options: ['always'], parserOptions: parserOptions, errors: [ @@ -129,6 +135,7 @@ ruleTester.run('jsx-equals-spacing', rule, { ] }, { code: '', + output: '', options: ['always'], parserOptions: parserOptions, errors: [ @@ -136,6 +143,7 @@ ruleTester.run('jsx-equals-spacing', rule, { ] }, { code: '', + output: '', options: ['always'], parserOptions: parserOptions, errors: [ @@ -143,6 +151,7 @@ ruleTester.run('jsx-equals-spacing', rule, { ] }, { code: '', + output: '', options: ['always'], parserOptions: parserOptions, errors: [