diff --git a/README.md b/README.md index 611007b..ed47f88 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,8 @@ 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 } } ``` @@ -52,7 +53,8 @@ Finally, enable all of the rules that you would like to use. # 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). diff --git a/docs/rules/jsx-for-single-child.md b/docs/rules/jsx-for-single-child.md new file mode 100644 index 0000000..c9e5596 --- /dev/null +++ b/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 + +
+
+ +``` + +The following patterns are not warnings: + +```js + +
+ +``` + +## When Not To Use It + +If you are not using JSX, you can disable this rule diff --git a/lib/rules/jsx-for-single-child.js b/lib/rules/jsx-for-single-child.js new file mode 100644 index 0000000..ac97d25 --- /dev/null +++ b/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 = []; diff --git a/tests/lib/rules/jsx-for-single-child.js b/tests/lib/rules/jsx-for-single-child.js new file mode 100644 index 0000000..143b198 --- /dev/null +++ b/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: "
", + ecmaFeatures: { + jsx: true + } + }, { + code: "foobar", + ecmaFeatures: { + jsx: true + } + }, { + code: "\n\n\t\n", + ecmaFeatures: { + jsx: true + } + } + ], + + invalid: [ + { + code: "\n\n\t
\n\t
\n", + ecmaFeatures: { + jsx: true + }, + errors: [{ + message: "'For' tag must have single child.", + type: "JSXOpeningElement" + }] + } + ] +});