Skip to content

Commit

Permalink
add jsx-for-single-child
Browse files Browse the repository at this point in the history
  • Loading branch information
vkbansal committed Jan 6, 2016
1 parent 16ea1b0 commit da2e17c
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 2 deletions.
6 changes: 4 additions & 2 deletions README.md
Expand Up @@ -44,15 +44,17 @@ Finally, enable all of the rules that you would like to use.
{
"rules": {
"jsx-control-statements/jsx-if-condition": 1,
"jsx-control-statements/jsx-if-single-child": 1
"jsx-control-statements/jsx-if-single-child": 1,
"jsx-control-statements/jsx-for-single-child": 1
}
}
```

# List of supported rules

* [jsx-if-condition](docs/rules/jsx-if-condition.md): Warn if `If` tag is missing `condition` attribute.
* [jsx-if-single-child](docs/rules/jsx-if-single-child.md): Warn if `If` and `Else` tags must have single children.
* [jsx-if-single-child](docs/rules/jsx-if-single-child.md): Warn if `If` and `Else` tags does not have single child.
* [jsx-for-single-child](docs/rules/jsx-for-single-child.md): Warn if `For` tags does not have single child.

## Credits
Thanks to @yannickcr for his awesome [eslint-plugin-react](https://github.com/yannickcr/eslint-plugin-react).
Expand Down
27 changes: 27 additions & 0 deletions docs/rules/jsx-for-single-child.md
@@ -0,0 +1,27 @@
# For tag must contain single child (jsx-for-single-child)

Warn if `For` tags must have single children.


## Rule Details

The following patterns are considered warnings:

```js
<For>
<div/>
<div/>
</For>
```

The following patterns are not warnings:

```js
<For>
<div/>
</For>
```

## When Not To Use It

If you are not using JSX, you can disable this rule
34 changes: 34 additions & 0 deletions lib/rules/jsx-for-single-child.js
@@ -0,0 +1,34 @@
/**
* @fileoverview For tag must contain single child
* @author Vivek Kumar Bansal
* @copyright 2016 Vivek Kumar Bansal. All rights reserved.
*/
"use strict";

var utils = require("../utils");

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function(context) {
function isForComponent(node) {
return utils.isTag("For", node);
}

//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------

return {
JSXOpeningElement: function(node) {
if (!isForComponent(node)) return node;

if (!utils.hasSingleChild(node)) {
context.report(node, "'For' tag must have single child.");
}
}
};
};

module.exports.schema = [];
53 changes: 53 additions & 0 deletions tests/lib/rules/jsx-for-single-child.js
@@ -0,0 +1,53 @@
/**
* @fileoverview For tag must have single child
* @author Vivek Kumar Bansal
* @copyright 2016 Vivek Kumar Bansal. All rights reserved.
*/
"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

var rule = require("../../../lib/rules/jsx-for-single-child"),
RuleTester = require("eslint").RuleTester;

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

var ruleTester = new RuleTester();

ruleTester.run("jsx-for-single-child", rule, {
valid: [
{
code: "<For><div/></For>",
ecmaFeatures: {
jsx: true
}
}, {
code: "<For>foobar</For>",
ecmaFeatures: {
jsx: true
}
}, {
code: "\n<For>\n\t<foobar/>\n</For>",
ecmaFeatures: {
jsx: true
}
}
],

invalid: [
{
code: "\n<For>\n\t<div/>\n\t<div/>\n</For>",
ecmaFeatures: {
jsx: true
},
errors: [{
message: "'For' tag must have single child.",
type: "JSXOpeningElement"
}]
}
]
});

0 comments on commit da2e17c

Please sign in to comment.