From ea63f245d0786c20e06fa1ba77bde4bc0c744a40 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sat, 13 Feb 2021 05:24:06 +0800 Subject: [PATCH] Refactor `conditionalExpressionChainContainsJsx` (#10282) --- src/language-js/print/ternary.js | 51 ++++++++++---------------------- 1 file changed, 16 insertions(+), 35 deletions(-) diff --git a/src/language-js/print/ternary.js b/src/language-js/print/ternary.js index c4d5e792e..91202114f 100644 --- a/src/language-js/print/ternary.js +++ b/src/language-js/print/ternary.js @@ -58,10 +58,7 @@ const { // } // } // } -// -// We want to traverse over that shape and convert it into a flat structure so -// that we can find if there's a JSXElement somewhere inside. -function getConditionalChainContents(node) { +function conditionalExpressionChainContainsJsx(node) { // Given this code: // // // Using a ConditionalExpression as the consequent is uncommon, but should @@ -88,41 +85,25 @@ function getConditionalChainContents(node) { // } // } // - // we should return this Array: - // - // [ - // Identifier(A), - // Identifier(B), - // Identifier(C), - // Identifier(D), - // Identifier(E), - // Identifier(F), - // Identifier(G), - // Identifier(H), - // Identifier(I) - // ]; - // - // This loses the information about whether each node was the test, - // consequent, or alternate, but we don't care about that here- we are only - // flattening this structure to find if there's any JSXElements inside. - const nonConditionalExpressions = []; + // We don't care about whether each node was the test, consequent, or alternate + // We are only checking if there's any JSXElements inside. + const conditionalExpressions = [node]; + for (let index = 0; index < conditionalExpressions.length; index++) { + const conditionalExpression = conditionalExpressions[index]; + for (const property of ["test", "consequent", "alternate"]) { + const node = conditionalExpression[property]; - function recurse(node) { - if (node.type === "ConditionalExpression") { - recurse(node.test); - recurse(node.consequent); - recurse(node.alternate); - } else { - nonConditionalExpressions.push(node); + if (isJsxNode(node)) { + return true; + } + + if (node.type === "ConditionalExpression") { + conditionalExpressions.push(node); + } } } - recurse(node); - return nonConditionalExpressions; -} - -function conditionalExpressionChainContainsJsx(node) { - return getConditionalChainContents(node).some(isJsxNode); + return false; } function printTernaryTest(path, options, print) {