Skip to content

Commit

Permalink
Add auto fix for jsx-boolean-value
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickcr committed Jan 18, 2016
1 parent 21e4857 commit 3a1b6ac
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/jsx-boolean-value.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"`.
Expand Down
16 changes: 14 additions & 2 deletions lib/rules/jsx-boolean-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 5 additions & 3 deletions tests/lib/rules/jsx-boolean-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ ruleTester.run('jsx-boolean-value', rule, {
{code: '<App foo={true} />;', options: ['always'], parserOptions: parserOptions}
],
invalid: [
{code: '<App foo={true} />;', options: ['never'],
{code: '<App foo={true} />;', output: '<App foo />;', options: ['never'],
errors: [{message: 'Value must be omitted for boolean attributes'}], parserOptions: parserOptions},
{code: '<App foo={true} />;',
{code: '<App foo={true} />;', output: '<App foo />;',
errors: [{message: 'Value must be omitted for boolean attributes'}], parserOptions: parserOptions},
{code: '<App foo />;', options: ['always'],
{code: '<App foo = {true} />;', output: '<App foo />;',
errors: [{message: 'Value must be omitted for boolean attributes'}], parserOptions: parserOptions},
{code: '<App foo />;', output: '<App foo={true} />;', options: ['always'],
errors: [{message: 'Value must be set for boolean attributes'}], parserOptions: parserOptions}
]
});

0 comments on commit 3a1b6ac

Please sign in to comment.