(sourceFile: SourceFile)
| 1381 | } |
| 1382 | |
| 1383 | public cleanFileSpreads(sourceFile: SourceFile): SourceFile { |
| 1384 | const file = sourceFile; |
| 1385 | const spreadElements = file |
| 1386 | .getDescendantsOfKind(SyntaxKind.SpreadElement) |
| 1387 | .filter(p => Node.isArrayLiteralExpression(p.getParentOrThrow())); |
| 1388 | |
| 1389 | const spreadElementsInRoutesVariableStatement = []; |
| 1390 | |
| 1391 | for (const spreadElement of spreadElements) { |
| 1392 | // Loop through their parents nodes, and if one is a variableStatement and === 'routes' |
| 1393 | let foundParentVariableStatement = false; |
| 1394 | spreadElement.getParentWhile(n => { |
| 1395 | if (n.getKind() === SyntaxKind.VariableStatement) { |
| 1396 | if (this.isVariableRoutes(n.compilerNode)) { |
| 1397 | foundParentVariableStatement = true; |
| 1398 | } |
| 1399 | } |
| 1400 | return true; |
| 1401 | }); |
| 1402 | if (foundParentVariableStatement) { |
| 1403 | spreadElementsInRoutesVariableStatement.push(spreadElement); |
| 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | // inline the ArrayLiteralExpression SpreadElements |
| 1408 | for (const spreadElement of spreadElementsInRoutesVariableStatement) { |
| 1409 | const spreadExpression = spreadElement.getExpression(); |
| 1410 | let spreadElementIdentifier = spreadExpression.getText(), |
| 1411 | searchedImport, |
| 1412 | aliasOriginalName = '', |
| 1413 | foundWithAliasInImports = false, |
| 1414 | foundWithAlias = false; |
| 1415 | |
| 1416 | // Route spreads can be function calls (e.g. ...buildRoutes()). |
| 1417 | // These are not always inlineable, but they should never crash parsing. |
| 1418 | if (Node.isCallExpression(spreadExpression)) { |
| 1419 | const callee = spreadExpression.getExpression(); |
| 1420 | if (Node.isIdentifier(callee)) { |
| 1421 | spreadElementIdentifier = callee.getText(); |
| 1422 | } else { |
| 1423 | logger.warn( |
| 1424 | `Spread call "${spreadExpression.getText()}" cannot be inlined because its callee is not an identifier.` |
| 1425 | ); |
| 1426 | continue; |
| 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | // Try to find it in imports |
| 1431 | const imports = file.getImportDeclarations(); |
| 1432 | |
| 1433 | imports.forEach(i => { |
| 1434 | let namedImports = i.getNamedImports(), |
| 1435 | namedImportsLength = namedImports.length, |
| 1436 | j = 0; |
| 1437 | |
| 1438 | if (namedImportsLength > 0) { |
| 1439 | for (j; j < namedImportsLength; j++) { |
| 1440 | let importName = namedImports[j].getNameNode().getText() as string, |
no test coverage detected