Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
118 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,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 |
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,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 = []; |
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,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" | ||
}] | ||
} | ||
] | ||
}); |