|
4 | 4 |
|
5 | 5 | 'use strict'; |
6 | 6 |
|
7 | | -const estraverse = require('estraverse'); |
8 | 7 | const elementType = require('jsx-ast-utils/elementType'); |
9 | 8 |
|
10 | 9 | const astUtil = require('./ast'); |
@@ -95,63 +94,48 @@ function isWhiteSpaces(value) { |
95 | 94 | * @returns {Boolean} True if the node is returning JSX or null, false if not |
96 | 95 | */ |
97 | 96 | function isReturningJSX(isCreateElement, ASTnode, context, strict, ignoreNull) { |
98 | | - let found = false; |
99 | | - astUtil.traverseReturns(ASTnode, context, (node) => { |
100 | | - // Traverse return statement |
101 | | - astUtil.traverse(node, { |
102 | | - enter(childNode) { |
103 | | - const setFound = () => { |
104 | | - found = true; |
105 | | - this.skip(); |
106 | | - }; |
107 | | - switch (childNode.type) { |
108 | | - case 'FunctionExpression': |
109 | | - case 'FunctionDeclaration': |
110 | | - case 'ArrowFunctionExpression': |
111 | | - // Do not traverse into inner function definitions |
112 | | - return this.skip(); |
113 | | - case 'ConditionalExpression': |
114 | | - if (!strict) break; |
115 | | - if (isJSX(childNode.consequent) && isJSX(childNode.alternate)) { |
116 | | - setFound(); |
117 | | - } |
118 | | - this.skip(); |
119 | | - break; |
120 | | - case 'LogicalExpression': |
121 | | - if (!strict) break; |
122 | | - if (isJSX(childNode.left) && isJSX(childNode.right)) { |
123 | | - setFound(); |
124 | | - } |
125 | | - this.skip(); |
126 | | - break; |
127 | | - case 'JSXElement': |
128 | | - case 'JSXFragment': |
129 | | - setFound(); |
130 | | - break; |
131 | | - case 'CallExpression': |
132 | | - if (isCreateElement(childNode)) { |
133 | | - setFound(); |
134 | | - } |
135 | | - this.skip(); |
136 | | - break; |
137 | | - case 'Literal': |
138 | | - if (!ignoreNull && childNode.value === null) { |
139 | | - setFound(); |
140 | | - } |
141 | | - break; |
142 | | - case 'Identifier': { |
143 | | - const variable = variableUtil.findVariableByName(context, childNode.name); |
144 | | - if (isJSX(variable)) { |
145 | | - setFound(); |
146 | | - } |
147 | | - break; |
148 | | - } |
149 | | - default: |
| 97 | + const isJSXValue = (node) => { |
| 98 | + if (!node) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + switch (node.type) { |
| 102 | + case 'ConditionalExpression': |
| 103 | + if (strict) { |
| 104 | + return isJSXValue(node.consequent) && isJSXValue(node.alternate); |
150 | 105 | } |
151 | | - }, |
152 | | - }); |
| 106 | + return isJSXValue(node.consequent) || isJSXValue(node.alternate); |
| 107 | + case 'LogicalExpression': |
| 108 | + if (strict) { |
| 109 | + return isJSXValue(node.left) && isJSXValue(node.right); |
| 110 | + } |
| 111 | + return isJSXValue(node.left) || isJSXValue(node.right); |
| 112 | + case 'SequenceExpression': |
| 113 | + return isJSXValue(node.expressions[node.expressions.length - 1]); |
| 114 | + case 'JSXElement': |
| 115 | + case 'JSXFragment': |
| 116 | + return true; |
| 117 | + case 'CallExpression': |
| 118 | + return isCreateElement(node); |
| 119 | + case 'Literal': |
| 120 | + if (!ignoreNull && node.value === null) { |
| 121 | + return true; |
| 122 | + } |
| 123 | + return false; |
| 124 | + case 'Identifier': { |
| 125 | + const variable = variableUtil.findVariableByName(context, node.name); |
| 126 | + return isJSX(variable); |
| 127 | + } |
| 128 | + default: |
| 129 | + return false; |
| 130 | + } |
| 131 | + }; |
153 | 132 |
|
154 | | - return found && estraverse.VisitorOption.Break; |
| 133 | + let found = false; |
| 134 | + astUtil.traverseReturns(ASTnode, context, (node, breakTraverse) => { |
| 135 | + if (isJSXValue(node)) { |
| 136 | + found = true; |
| 137 | + breakTraverse(); |
| 138 | + } |
155 | 139 | }); |
156 | 140 |
|
157 | 141 | return found; |
|
0 commit comments