From 3a1b6ac097c98ba6e5d26ef5a278e52d439787c4 Mon Sep 17 00:00:00 2001 From: Yannick Croissant Date: Mon, 18 Jan 2016 00:53:36 +0000 Subject: [PATCH] Add auto fix for jsx-boolean-value --- README.md | 2 +- docs/rules/jsx-boolean-value.md | 2 ++ lib/rules/jsx-boolean-value.js | 16 ++++++++++++++-- tests/lib/rules/jsx-boolean-value.js | 8 +++++--- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0f41f9d3e5..98571df220 100644 --- a/README.md +++ b/README.md @@ -117,7 +117,7 @@ Finally, enable all of the rules that you would like to use. * [display-name](docs/rules/display-name.md): Prevent missing `displayName` in a React component definition * [forbid-prop-types](docs/rules/forbid-prop-types.md): Forbid certain propTypes -* [jsx-boolean-value](docs/rules/jsx-boolean-value.md): Enforce boolean attributes notation in JSX +* [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 * [jsx-handler-names](docs/rules/jsx-handler-names.md): Enforce event handler naming conventions in JSX diff --git a/docs/rules/jsx-boolean-value.md b/docs/rules/jsx-boolean-value.md index 5b11d8bed8..c8a9d6c0e7 100644 --- a/docs/rules/jsx-boolean-value.md +++ b/docs/rules/jsx-boolean-value.md @@ -2,6 +2,8 @@ In JSX when using a boolean attribute you can set the attribute value to `true` or omit the value. This rule will enforce one or the other to keep consistency in your code. +**Fixable:** This rule is automatically fixable using the `--fix` flag on the command line. + ## Rule Details This rule takes one argument. If it is `"always"` then it warns whenever an attribute is missing it's value. If `"never"` then it warns if an attribute has a `true` value. The default value of this option is `"never"`. diff --git a/lib/rules/jsx-boolean-value.js b/lib/rules/jsx-boolean-value.js index edd3ca8051..e4169e01d0 100644 --- a/lib/rules/jsx-boolean-value.js +++ b/lib/rules/jsx-boolean-value.js @@ -20,12 +20,24 @@ module.exports = function(context) { switch (configuration) { case 'always': if (node.value === null) { - context.report(node, ALWAYS_MESSAGE); + context.report({ + node: node, + message: ALWAYS_MESSAGE, + fix: function(fixer) { + return fixer.insertTextAfter(node, '={true}'); + } + }); } break; case 'never': if (node.value && node.value.type === 'JSXExpressionContainer' && node.value.expression.value === true) { - context.report(node, NEVER_MESSAGE); + context.report({ + node: node, + message: NEVER_MESSAGE, + fix: function(fixer) { + return fixer.removeRange([node.name.range[1], node.value.range[1]]); + } + }); } break; default: diff --git a/tests/lib/rules/jsx-boolean-value.js b/tests/lib/rules/jsx-boolean-value.js index 712337b984..065ee9dddd 100644 --- a/tests/lib/rules/jsx-boolean-value.js +++ b/tests/lib/rules/jsx-boolean-value.js @@ -30,11 +30,13 @@ ruleTester.run('jsx-boolean-value', rule, { {code: ';', options: ['always'], parserOptions: parserOptions} ], invalid: [ - {code: ';', options: ['never'], + {code: ';', output: ';', options: ['never'], errors: [{message: 'Value must be omitted for boolean attributes'}], parserOptions: parserOptions}, - {code: ';', + {code: ';', output: ';', errors: [{message: 'Value must be omitted for boolean attributes'}], parserOptions: parserOptions}, - {code: ';', options: ['always'], + {code: ';', output: ';', + errors: [{message: 'Value must be omitted for boolean attributes'}], parserOptions: parserOptions}, + {code: ';', output: ';', options: ['always'], errors: [{message: 'Value must be set for boolean attributes'}], parserOptions: parserOptions} ] });