-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add jsx-boolean-value rule (fixes #11)
- Loading branch information
Showing
5 changed files
with
107 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Enforce boolean attributes notation in JSX (jsx-boolean-value) | ||
|
||
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. | ||
|
||
## 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"`. | ||
|
||
The following patterns are considered warnings when configured `"never"`: | ||
|
||
```js | ||
var Hello = <Hello personal={true} />; | ||
``` | ||
|
||
The following patterns are not considered warnings when configured `"never"`: | ||
|
||
```js | ||
var Hello = <Hello personal />; | ||
``` | ||
|
||
The following patterns are considered warnings when configured `"always"`: | ||
|
||
```js | ||
var Hello = <Hello personal />; | ||
``` | ||
|
||
The following patterns are not considered warnings when configured `"always"`: | ||
|
||
```js | ||
var Hello = <Hello personal={true} />; | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you do not want to enforce any style for boolean attributes, then you can disable this rule. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* @fileoverview Enforce boolean attributes notation in JSX | ||
* @author Yannick Croissant | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = function(context) { | ||
|
||
var configuration = context.options[0] || {}; | ||
|
||
var NEVER_MESSAGE = 'Value must be omitted for boolean attributes'; | ||
var ALWAYS_MESSAGE = 'Value must be set for boolean attributes'; | ||
|
||
return { | ||
JSXAttribute: function(node) { | ||
switch (configuration) { | ||
case 'always': | ||
if (node.value === null) { | ||
context.report(node, ALWAYS_MESSAGE); | ||
} | ||
break; | ||
case 'never': | ||
if (node.value && node.value.type === 'JSXExpressionContainer' && node.value.expression.value === true) { | ||
context.report(node, NEVER_MESSAGE); | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* @fileoverview Enforce boolean attributes notation in JSX | ||
* @author Yannick Croissant | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var eslint = require('eslint').linter; | ||
var ESLintTester = require('eslint-tester'); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
var eslintTester = new ESLintTester(eslint); | ||
eslintTester.addRuleTest('lib/rules/jsx-boolean-value', { | ||
valid: [ | ||
{code: '<App foo />;', args: [1, 'never'], ecmaFeatures: {jsx: true}}, | ||
{code: '<App foo={true} />;', args: [1, 'always'], ecmaFeatures: {jsx: true}} | ||
], | ||
invalid: [ | ||
{code: '<App foo={true} />;', args: [1, 'never'], | ||
errors: [{message: 'Value must be omitted for boolean attributes'}], ecmaFeatures: {jsx: true}}, | ||
{code: '<App foo />;', args: [1, 'always'], | ||
errors: [{message: 'Value must be set for boolean attributes'}], ecmaFeatures: {jsx: true}} | ||
] | ||
}); |