Skip to content

Commit d73622a

Browse files
committed
fix(router): clean call expressions for Route[] types
closes #1436
1 parent 7b88115 commit d73622a

2 files changed

Lines changed: 123 additions & 19 deletions

File tree

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

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,37 +1534,71 @@ export class RouterParserUtil {
15341534
public cleanCallExpressions(sourceFile: SourceFile): SourceFile {
15351535
const file = sourceFile;
15361536

1537-
// Find all variable declarations with Routes type
1538-
const variableDeclarations = sourceFile.getVariableDeclarations();
1539-
const routesVariableDeclarations = variableDeclarations.filter((v) => {
1540-
const type = v.compilerNode.type;
1537+
const isRouteVariableDeclaration = (declaration): boolean => {
1538+
const declarationType = declaration.compilerNode.type;
1539+
if (!declarationType) {
1540+
return false;
1541+
}
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+
15411573
if (
1542-
typeof type !== "undefined" &&
1543-
ts.isTypeReferenceNode(type) &&
1544-
typeof type.typeName !== "undefined"
1574+
ts.isArrayTypeNode(declarationType) &&
1575+
ts.isTypeReferenceNode(declarationType.elementType) &&
1576+
declarationType.elementType.typeName
15451577
) {
1546-
return (type.typeName as ts.Identifier).text === "Routes";
1578+
const elementTypeName = declarationType.elementType.typeName.getText();
1579+
if (
1580+
elementTypeName === "Route" ||
1581+
elementTypeName.endsWith(".Route")
1582+
) {
1583+
return true;
1584+
}
15471585
}
1586+
15481587
return false;
1549-
});
1588+
};
15501589

1551-
if (routesVariableDeclarations.length === 0) {
1552-
return file;
1553-
}
1590+
const routesVariableDeclarations = sourceFile
1591+
.getVariableDeclarations()
1592+
.filter((declaration) => isRouteVariableDeclaration(declaration));
15541593

1555-
// Process all Routes variable declarations
15561594
for (const variableDeclaration of routesVariableDeclarations) {
15571595
const initializer = variableDeclaration.getInitializer();
1558-
if (!initializer) {
1559-
continue;
1560-
}
1596+
if (!initializer) continue;
15611597

15621598
for (const callExpr of initializer.getDescendantsOfKind(
15631599
SyntaxKind.CallExpression,
15641600
)) {
1565-
if (callExpr.wasForgotten()) {
1566-
continue;
1567-
}
1601+
if (callExpr.wasForgotten()) continue;
15681602
callExpr.replaceWithText((writer) =>
15691603
writer.quote(callExpr.getText()),
15701604
);

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,76 @@ const routes: Routes = [
348348

349349
expect(parsed[0].path).to.equal("homePageRoutePath()");
350350
});
351+
352+
it("should allow JSON5.parse for Route[] declarations with function calls", () => {
353+
const project = new Project({ useInMemoryFileSystem: true });
354+
const sourceFile = project.createSourceFile(
355+
"/tmp/routes-1293-route-array.ts",
356+
`
357+
import { Route } from "@angular/router";
358+
function homePageRoutePath(): string {
359+
return "";
360+
}
361+
const routes: Route[] = [
362+
{
363+
path: homePageRoutePath(),
364+
component: "HomePageComponent"
365+
}
366+
];
367+
`,
368+
);
369+
370+
routerParser.cleanCallExpressions(sourceFile);
371+
routerParser.cleanFileDynamics(sourceFile);
372+
373+
const initializer = sourceFile
374+
.getVariableDeclarationOrThrow("routes")
375+
.getInitializerOrThrow().compilerNode;
376+
const generated = new CodeGenerator().generate(initializer);
377+
const cleaned = routerParser.cleanRawRoute(generated);
378+
379+
let parsed: any;
380+
expect(() => {
381+
parsed = require("json5").parse(cleaned);
382+
}).not.to.throw();
383+
384+
expect(parsed[0].path).to.equal("homePageRoutePath()");
385+
});
386+
387+
it("should allow JSON5.parse for ReadonlyArray<Route> declarations with function calls", () => {
388+
const project = new Project({ useInMemoryFileSystem: true });
389+
const sourceFile = project.createSourceFile(
390+
"/tmp/routes-1293-readonly-route-array.ts",
391+
`
392+
import { Route } from "@angular/router";
393+
function buildPath(): string {
394+
return "";
395+
}
396+
const routes: ReadonlyArray<Route> = [
397+
{
398+
path: buildPath(),
399+
component: "HomePageComponent"
400+
}
401+
];
402+
`,
403+
);
404+
405+
routerParser.cleanCallExpressions(sourceFile);
406+
routerParser.cleanFileDynamics(sourceFile);
407+
408+
const initializer = sourceFile
409+
.getVariableDeclarationOrThrow("routes")
410+
.getInitializerOrThrow().compilerNode;
411+
const generated = new CodeGenerator().generate(initializer);
412+
const cleaned = routerParser.cleanRawRoute(generated);
413+
414+
let parsed: any;
415+
expect(() => {
416+
parsed = require("json5").parse(cleaned);
417+
}).not.to.throw();
418+
419+
expect(parsed[0].path).to.equal("buildPath()");
420+
});
351421
});
352422

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

0 commit comments

Comments
 (0)