Checks whether a ternary expression has another ternary inside of it.
(rootNode: ts.ConditionalExpression)
| 22 | |
| 23 | /** Checks whether a ternary expression has another ternary inside of it. */ |
| 24 | function hasNestedTernary(rootNode: ts.ConditionalExpression): boolean { |
| 25 | let hasNestedTernaryDescendant = false; |
| 26 | |
| 27 | // Start from the immediate children of the root node. |
| 28 | rootNode.forEachChild(function walk(node: ts.Node) { |
| 29 | // Stop checking if we've hit a ternary. Also note that we don't descend |
| 30 | // into call expressions, because it's valid to have a ternary expression |
| 31 | // inside of a callback which is one of the arguments of a ternary. |
| 32 | if (ts.isConditionalExpression(node)) { |
| 33 | hasNestedTernaryDescendant = true; |
| 34 | } else if (!ts.isCallExpression(node)) { |
| 35 | node.forEachChild(walk); |
| 36 | } |
| 37 | }); |
| 38 | |
| 39 | return hasNestedTernaryDescendant; |
| 40 | } |