|
6 | 6 | import java.util.Iterator; |
7 | 7 | import java.util.List; |
8 | 8 | import java.util.ListIterator; |
| 9 | +import java.util.NoSuchElementException; |
9 | 10 | import java.util.Optional; |
10 | 11 | import java.util.stream.Collectors; |
11 | 12 | import java.util.stream.IntStream; |
|
47 | 48 | import org.jqassistant.contrib.plugin.javascript.scanner.visitor.helpers.ObjectStoreHelper; |
48 | 49 | import org.jqassistant.contrib.plugin.javascript.scanner.visitor.helpers.ParameterStoreHelper; |
49 | 50 | import org.jqassistant.contrib.plugin.javascript.scanner.visitor.helpers.PrimitiveStoreHelper; |
| 51 | +import org.jqassistant.contrib.plugin.javascript.scanner.visitor.helpers.StreamHelper; |
50 | 52 | import org.jqassistant.contrib.plugin.javascript.scanner.visitor.helpers.VariableStoreHelper; |
51 | 53 |
|
52 | 54 | import com.buschmais.jqassistant.core.store.api.Store; |
@@ -172,38 +174,37 @@ public void exitMethodDefinition(MethodDefinitionContext ctx) { |
172 | 174 | artifactStack.pop(); |
173 | 175 | } |
174 | 176 |
|
175 | | - private FunctionDescriptor getLastDefinedFunction() { |
| 177 | + @Override |
| 178 | + public void enterFormalParameterArg(FormalParameterArgContext ctx) { |
| 179 | + int countParamtersBefore = countLastParameters().intValue(); |
| 180 | + FunctionDescriptor function = getLastDefinedFunction(); |
| 181 | + FunctionParameterDescriptor ecmaParam = new ParameterStoreHelper(countParamtersBefore).createNodeIn(store, ctx); |
| 182 | + setQualifiedName(function, ecmaParam, ecmaParam.getName()); |
| 183 | + amendStack(ecmaParam); |
| 184 | + function.getParameters().add(ecmaParam); |
| 185 | + } |
| 186 | + |
| 187 | + private Long countLastParameters() { |
176 | 188 | // https://stackoverflow.com/questions/24010109/java-8-stream-reverse-order |
177 | 189 | ListIterator<JavaScriptDescriptor> listIterator = artifacts.listIterator(artifacts.size()); |
178 | | - return Stream.generate(listIterator::previous) |
179 | | - .limit(artifacts.size()) |
180 | | - .filter((e) -> e instanceof FunctionDescriptor) |
181 | | - .map((e) -> (FunctionDescriptor)e) |
182 | | - .findFirst() |
183 | | - .orElseThrow(); |
| 190 | + Stream<JavaScriptDescriptor> reverseParamterStream = Stream.generate(listIterator::previous).limit(artifacts.size()); |
| 191 | + return StreamHelper.takeWhile(reverseParamterStream, (desc) -> desc instanceof FunctionParameterDescriptor) |
| 192 | + .collect(Collectors.counting()); |
| 193 | + |
184 | 194 |
|
185 | | - |
186 | 195 | } |
187 | 196 |
|
188 | | - private Long countLastParameters() { |
| 197 | + private FunctionDescriptor getLastDefinedFunction() { |
189 | 198 | // https://stackoverflow.com/questions/24010109/java-8-stream-reverse-order |
190 | 199 | ListIterator<JavaScriptDescriptor> listIterator = artifacts.listIterator(artifacts.size()); |
191 | 200 | return Stream.generate(listIterator::previous) |
192 | | - .limit(artifacts.size()) |
193 | | - .takeWhile((desc) -> desc instanceof FunctionParameterDescriptor) // JAVA 9 |
194 | | - .collect(Collectors.counting()); |
| 201 | + .limit(artifacts.size()) |
| 202 | + .filter((e) -> e instanceof FunctionDescriptor) |
| 203 | + .map((e) -> (FunctionDescriptor)e) |
| 204 | + .findFirst() |
| 205 | + .orElseThrow(() -> new NoSuchElementException("the last function wasn't found")); // Function not found shoudn't be happening |
| 206 | + |
195 | 207 |
|
196 | | - |
197 | | - } |
198 | | - |
199 | | - @Override |
200 | | - public void enterFormalParameterArg(FormalParameterArgContext ctx) { |
201 | | - int countParamtersBefore = countLastParameters().intValue(); |
202 | | - FunctionDescriptor function = getLastDefinedFunction(); |
203 | | - FunctionParameterDescriptor ecmaParam = new ParameterStoreHelper(countParamtersBefore).createNodeIn(store, ctx); |
204 | | - setQualifiedName(function, ecmaParam, ecmaParam.getName()); |
205 | | - amendStack(ecmaParam); |
206 | | - function.getParameters().add(ecmaParam); |
207 | 208 | } |
208 | 209 |
|
209 | 210 | @Override |
|
0 commit comments