@@ -128,6 +128,35 @@ function getEncloseFunctionDeclaration(reference) { |
128 | 128 | return null; |
129 | 129 | } |
130 | 130 |
|
| 131 | +/** |
| 132 | + * Checks whether a given modifier is in a loop. |
| 133 | + * |
| 134 | + * Besides checking for the condition being in the loop, this also checks |
| 135 | + * whether the function that this modifier is belonging to is called |
| 136 | + * in the loop. |
| 137 | + * @param {LoopConditionInfo} condition The condition to check. |
| 138 | + * @param {Reference} modifier The modifier to check. |
| 139 | + * @returns {boolean} `true` if the modifier is in a loop. |
| 140 | + */ |
| 141 | +function hasModifierInLoop(condition, modifier) { |
| 142 | + if (condition.isInLoop(modifier)) { |
| 143 | + return true; |
| 144 | + } |
| 145 | + |
| 146 | + const funcNode = getEncloseFunctionDeclaration(modifier); |
| 147 | + |
| 148 | + if (!funcNode) { |
| 149 | + return false; |
| 150 | + } |
| 151 | + |
| 152 | + const funcVar = astUtils.getVariableByName( |
| 153 | + modifier.from.upper, |
| 154 | + funcNode.id.name, |
| 155 | + ); |
| 156 | + |
| 157 | + return Boolean(funcVar && funcVar.references.some(condition.isInLoop)); |
| 158 | +} |
| 159 | + |
131 | 160 | /** |
132 | 161 | * Updates the "modified" flags of given loop conditions with given modifiers. |
133 | 162 | * @param {LoopConditionInfo[]} conditions The loop conditions to be updated. |
@@ -140,26 +169,8 @@ function updateModifiedFlag(conditions, modifiers) { |
140 | 169 |
|
141 | 170 | for (let j = 0; !condition.modified && j < modifiers.length; ++j) { |
142 | 171 | const modifier = modifiers[j]; |
143 | | - let funcNode, funcVar; |
144 | 172 |
|
145 | | - /* |
146 | | - * Besides checking for the condition being in the loop, we want to |
147 | | - * check the function that this modifier is belonging to is called |
148 | | - * in the loop. |
149 | | - * FIXME: This should probably be extracted to a function. |
150 | | - */ |
151 | | - const inLoop = |
152 | | - condition.isInLoop(modifier) || |
153 | | - Boolean( |
154 | | - (funcNode = getEncloseFunctionDeclaration(modifier)) && |
155 | | - (funcVar = astUtils.getVariableByName( |
156 | | - modifier.from.upper, |
157 | | - funcNode.id.name, |
158 | | - )) && |
159 | | - funcVar.references.some(condition.isInLoop), |
160 | | - ); |
161 | | - |
162 | | - condition.modified = inLoop; |
| 173 | + condition.modified = hasModifierInLoop(condition, modifier); |
163 | 174 | } |
164 | 175 | } |
165 | 176 | } |
|