Skip to content

Commit 6bc2dae

Browse files
committed
fix(router): detect aliased and inferred route arrays
closes #1293
1 parent d73622a commit 6bc2dae

2 files changed

Lines changed: 133 additions & 56 deletions

File tree

‎src/utils/router-parser.util.ts‎

Lines changed: 63 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -977,11 +977,62 @@ export class RouterParserUtil {
977977
public isVariableRoutes(node) {
978978
let result = false;
979979
if (node.declarationList && node.declarationList.declarations) {
980+
const routeLikePropertyNames = new Set([
981+
"path",
982+
"component",
983+
"redirectTo",
984+
"loadChildren",
985+
"loadComponent",
986+
"children",
987+
"data",
988+
"canActivate",
989+
]);
990+
991+
const hasRouteLikeArrayInitializer = (declaration): boolean => {
992+
if (!declaration.initializer) {
993+
return false;
994+
}
995+
if (!ts.isArrayLiteralExpression(declaration.initializer)) {
996+
return false;
997+
}
998+
if (!ts.isIdentifier(declaration.name)) {
999+
return false;
1000+
}
1001+
1002+
const variableName = declaration.name.text.toLowerCase();
1003+
if (!variableName.includes("route")) {
1004+
return false;
1005+
}
1006+
1007+
return declaration.initializer.elements.some((element) => {
1008+
if (ts.isSpreadElement(element)) {
1009+
return true;
1010+
}
1011+
if (!ts.isObjectLiteralExpression(element)) {
1012+
return false;
1013+
}
1014+
return element.properties.some((property) => {
1015+
if (!ts.isPropertyAssignment(property)) {
1016+
return false;
1017+
}
1018+
const propertyName = property.name.getText().replace(
1019+
/^['"]|['"]$/g,
1020+
"",
1021+
);
1022+
return routeLikePropertyNames.has(propertyName);
1023+
});
1024+
});
1025+
};
1026+
9801027
let i = 0;
9811028
const len = node.declarationList.declarations.length;
9821029
for (i; i < len; i++) {
983-
const declarationType = node.declarationList.declarations[i].type;
1030+
const declaration = node.declarationList.declarations[i];
1031+
const declarationType = declaration.type;
9841032
if (!declarationType) {
1033+
if (hasRouteLikeArrayInitializer(declaration)) {
1034+
result = true;
1035+
}
9851036
continue;
9861037
}
9871038

@@ -1032,6 +1083,10 @@ export class RouterParserUtil {
10321083
result = true;
10331084
}
10341085
}
1086+
1087+
if (!result && hasRouteLikeArrayInitializer(declaration)) {
1088+
result = true;
1089+
}
10351090
}
10361091
}
10371092
return result;
@@ -1533,63 +1588,15 @@ export class RouterParserUtil {
15331588
*/
15341589
public cleanCallExpressions(sourceFile: SourceFile): SourceFile {
15351590
const file = sourceFile;
1536-
1537-
const isRouteVariableDeclaration = (declaration): boolean => {
1538-
const declarationType = declaration.compilerNode.type;
1539-
if (!declarationType) {
1591+
const routesVariableDeclarations = sourceFile.getVariableDeclarations().filter((declaration) => {
1592+
const variableStatement = declaration.getFirstAncestorByKind(
1593+
SyntaxKind.VariableStatement,
1594+
);
1595+
if (!variableStatement) {
15401596
return false;
15411597
}
1542-
1543-
if (
1544-
ts.isTypeReferenceNode(declarationType) &&
1545-
declarationType.typeName
1546-
) {
1547-
const typeName = declarationType.typeName.getText();
1548-
if (typeName === "Routes" || typeName.endsWith(".Routes")) {
1549-
return true;
1550-
}
1551-
1552-
if (
1553-
(typeName === "Array" || typeName === "ReadonlyArray") &&
1554-
declarationType.typeArguments &&
1555-
declarationType.typeArguments.length === 1
1556-
) {
1557-
const routeType = declarationType.typeArguments[0];
1558-
if (
1559-
ts.isTypeReferenceNode(routeType) &&
1560-
routeType.typeName
1561-
) {
1562-
const routeTypeName = routeType.typeName.getText();
1563-
if (
1564-
routeTypeName === "Route" ||
1565-
routeTypeName.endsWith(".Route")
1566-
) {
1567-
return true;
1568-
}
1569-
}
1570-
}
1571-
}
1572-
1573-
if (
1574-
ts.isArrayTypeNode(declarationType) &&
1575-
ts.isTypeReferenceNode(declarationType.elementType) &&
1576-
declarationType.elementType.typeName
1577-
) {
1578-
const elementTypeName = declarationType.elementType.typeName.getText();
1579-
if (
1580-
elementTypeName === "Route" ||
1581-
elementTypeName.endsWith(".Route")
1582-
) {
1583-
return true;
1584-
}
1585-
}
1586-
1587-
return false;
1588-
};
1589-
1590-
const routesVariableDeclarations = sourceFile
1591-
.getVariableDeclarations()
1592-
.filter((declaration) => isRouteVariableDeclaration(declaration));
1598+
return this.isVariableRoutes(variableStatement.compilerNode);
1599+
});
15931600

15941601
for (const variableDeclaration of routesVariableDeclarations) {
15951602
const initializer = variableDeclaration.getInitializer();

‎test/src/utils/router-parser.util.spec.ts‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,76 @@ const routes: ReadonlyArray<Route> = [
418418

419419
expect(parsed[0].path).to.equal("buildPath()");
420420
});
421+
422+
it("should allow JSON5.parse for Route type aliases with function calls", () => {
423+
const project = new Project({ useInMemoryFileSystem: true });
424+
const sourceFile = project.createSourceFile(
425+
"/tmp/routes-1293-route-alias.ts",
426+
`
427+
import { Route } from "@angular/router";
428+
type AppRoutes = Route[];
429+
function resolvePath(): string {
430+
return "";
431+
}
432+
const routes: AppRoutes = [
433+
{
434+
path: resolvePath(),
435+
component: "HomePageComponent"
436+
}
437+
];
438+
`,
439+
);
440+
441+
routerParser.cleanCallExpressions(sourceFile);
442+
routerParser.cleanFileDynamics(sourceFile);
443+
444+
const initializer = sourceFile
445+
.getVariableDeclarationOrThrow("routes")
446+
.getInitializerOrThrow().compilerNode;
447+
const generated = new CodeGenerator().generate(initializer);
448+
const cleaned = routerParser.cleanRawRoute(generated);
449+
450+
let parsed: any;
451+
expect(() => {
452+
parsed = require("json5").parse(cleaned);
453+
}).not.to.throw();
454+
455+
expect(parsed[0].path).to.equal("resolvePath()");
456+
});
457+
458+
it("should allow JSON5.parse for inferred routes arrays without explicit route typing", () => {
459+
const project = new Project({ useInMemoryFileSystem: true });
460+
const sourceFile = project.createSourceFile(
461+
"/tmp/routes-1293-inferred-array.ts",
462+
`
463+
function dynamicPath(): string {
464+
return "";
465+
}
466+
const routes = [
467+
{
468+
path: dynamicPath(),
469+
component: "HomePageComponent"
470+
}
471+
];
472+
`,
473+
);
474+
475+
routerParser.cleanCallExpressions(sourceFile);
476+
routerParser.cleanFileDynamics(sourceFile);
477+
478+
const initializer = sourceFile
479+
.getVariableDeclarationOrThrow("routes")
480+
.getInitializerOrThrow().compilerNode;
481+
const generated = new CodeGenerator().generate(initializer);
482+
const cleaned = routerParser.cleanRawRoute(generated);
483+
484+
let parsed: any;
485+
expect(() => {
486+
parsed = require("json5").parse(cleaned);
487+
}).not.to.throw();
488+
489+
expect(parsed[0].path).to.equal("dynamicPath()");
490+
});
421491
});
422492

423493
// ── cleanFileSpreads() — path alias resolution (issue #1545) ─────────────

0 commit comments

Comments
 (0)