From f301ad43ae2e57694aa83804f1b0f6cd5ec61c72 Mon Sep 17 00:00:00 2001 From: Vivek Kumar Bansal Date: Tue, 5 Jan 2016 19:58:10 +0530 Subject: [PATCH] add check for empty/newlines --- lib/rules/jsx-if-single-child.js | 18 +++++++++++++----- tests/lib/rules/jsx-if-single-child.js | 20 +++++++++++++++----- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/lib/rules/jsx-if-single-child.js b/lib/rules/jsx-if-single-child.js index 538eab4..3397208 100644 --- a/lib/rules/jsx-if-single-child.js +++ b/lib/rules/jsx-if-single-child.js @@ -20,11 +20,19 @@ module.exports = function(context) { return n.name && n.name.type === "JSXIdentifier" && n.name.name === "Else" && n.selfClosing; } - function hasSingleChild(node) { - var children = node.parent.children; + function filterChildren(children) { + return children.filter(function(c) { + if (c.type === "JSXElement") return true; - return children.length && children.length === 1 - && ["JSXElement", "Literal"].indexOf(children[0].type) > -1; + if (c.type === "Literal") return c.value.trim() !== ""; + + return false; + }); + } + + function hasSingleChild(node) { + var children = filterChildren(node.parent.children); + return children.length && children.length === 1; } function hasElseCondition(node) { @@ -36,7 +44,7 @@ module.exports = function(context) { } function hasSingleChildrenBetween(node) { - var children = node.parent.children; + var children = filterChildren(node.parent.children); return children.length && children.length === 3 diff --git a/tests/lib/rules/jsx-if-single-child.js b/tests/lib/rules/jsx-if-single-child.js index 699bc08..cab8171 100644 --- a/tests/lib/rules/jsx-if-single-child.js +++ b/tests/lib/rules/jsx-if-single-child.js @@ -26,7 +26,7 @@ ruleTester.run("jsx-if-single-child", rule, { jsx: true } }, { - code: "
", + code: "\n\n\t
\n\n\t
\n", ecmaFeatures: { jsx: true } @@ -40,12 +40,22 @@ ruleTester.run("jsx-if-single-child", rule, { ecmaFeatures: { jsx: true } + }, { + code: "\n\n\t\n", + ecmaFeatures: { + jsx: true + } + }, { + code: "\n\n\t
\n\t\t\n\t
\n\n\t
\n\t\t\n\t
\n
", + ecmaFeatures: { + jsx: true + } } ], invalid: [ { - code: "
", + code: "\n\n\t
\n\t
\n", ecmaFeatures: { jsx: true }, @@ -54,7 +64,7 @@ ruleTester.run("jsx-if-single-child", rule, { type: "JSXOpeningElement" }] }, { - code: "
", + code: "\n\n\t
\n\t
\n\n\t
\n\t
\n", ecmaFeatures: { jsx: true }, @@ -63,7 +73,7 @@ ruleTester.run("jsx-if-single-child", rule, { type: "JSXOpeningElement" }] }, { - code: "
", + code: "\n\n\t
\n\t
\n\n\t
\n", ecmaFeatures: { jsx: true }, @@ -72,7 +82,7 @@ ruleTester.run("jsx-if-single-child", rule, { type: "JSXOpeningElement" }] }, { - code: "
", + code: "\n\n\t
\n\n\t
\n\t
\n", ecmaFeatures: { jsx: true },