Skip to content

Commit 7b88115

Browse files
committed
fix(router): avoid crashes on spread call expressions
closes #1317 closes #1293
1 parent 440f92c commit 7b88115

2 files changed

Lines changed: 111 additions & 9 deletions

File tree

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

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,14 +1162,27 @@ export class RouterParserUtil {
11621162

11631163
// inline the ArrayLiteralExpression SpreadElements
11641164
for (const spreadElement of spreadElementsInRoutesVariableStatement) {
1165-
let spreadElementIdentifier = spreadElement
1166-
.getExpression()
1167-
.getText(),
1165+
const spreadExpression = spreadElement.getExpression();
1166+
let spreadElementIdentifier = spreadExpression.getText(),
11681167
searchedImport,
11691168
aliasOriginalName = "",
11701169
foundWithAliasInImports = false,
11711170
foundWithAlias = false;
11721171

1172+
// Route spreads can be function calls (e.g. ...buildRoutes()).
1173+
// These are not always inlineable, but they should never crash parsing.
1174+
if (Node.isCallExpression(spreadExpression)) {
1175+
const callee = spreadExpression.getExpression();
1176+
if (Node.isIdentifier(callee)) {
1177+
spreadElementIdentifier = callee.getText();
1178+
} else {
1179+
logger.warn(
1180+
`Spread call "${spreadExpression.getText()}" cannot be inlined because its callee is not an identifier.`,
1181+
);
1182+
continue;
1183+
}
1184+
}
1185+
11731186
// Try to find it in imports
11741187
const imports = file.getImportDeclarations();
11751188

@@ -1300,10 +1313,14 @@ export class RouterParserUtil {
13001313
}
13011314
} else {
13021315
// if not, try directly in file
1303-
referencedDeclaration = spreadElement
1304-
.getExpression()
1305-
.getSymbolOrThrow()
1306-
.getValueDeclarationOrThrow();
1316+
const spreadSymbol = spreadExpression.getSymbol();
1317+
if (!spreadSymbol) {
1318+
logger.warn(
1319+
`Spread element "${spreadExpression.getText()}" cannot be resolved and will be skipped.`,
1320+
);
1321+
continue;
1322+
}
1323+
referencedDeclaration = spreadSymbol.getValueDeclaration();
13071324
}
13081325

13091326
if (typeof referencedDeclaration === "undefined") {
@@ -1312,9 +1329,11 @@ export class RouterParserUtil {
13121329
}
13131330

13141331
if (!Node.isVariableDeclaration(referencedDeclaration)) {
1315-
throw new Error(
1316-
`Not implemented referenced declaration kind: ${referencedDeclaration.getKindName()}`,
1332+
logger.warn(
1333+
`Spread element "${spreadExpression.getText()}" references a ${referencedDeclaration.getKindName()} and cannot be inlined. ` +
1334+
`This spread will be skipped in route documentation.`,
13171335
);
1336+
continue;
13181337
}
13191338

13201339
const referencedArray = referencedDeclaration.getInitializerIfKind(

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

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,43 @@ const routes: Routes = [
313313
});
314314
});
315315

316+
describe("cleanCallExpressions() — function calls in route values (issue #1293)", () => {
317+
it("should allow JSON5.parse when path is defined by a function call", () => {
318+
const project = new Project({ useInMemoryFileSystem: true });
319+
const sourceFile = project.createSourceFile(
320+
"/tmp/routes-1293.ts",
321+
`
322+
import { Routes } from "@angular/router";
323+
export function homePageRoutePath(): string {
324+
return "";
325+
}
326+
const routes: Routes = [
327+
{
328+
path: homePageRoutePath(),
329+
component: "HomePageComponent"
330+
}
331+
];
332+
`,
333+
);
334+
335+
routerParser.cleanCallExpressions(sourceFile);
336+
routerParser.cleanFileDynamics(sourceFile);
337+
338+
const initializer = sourceFile
339+
.getVariableDeclarationOrThrow("routes")
340+
.getInitializerOrThrow().compilerNode;
341+
const generated = new CodeGenerator().generate(initializer);
342+
const cleaned = routerParser.cleanRawRoute(generated);
343+
344+
let parsed: any;
345+
expect(() => {
346+
parsed = require("json5").parse(cleaned);
347+
}).not.to.throw();
348+
349+
expect(parsed[0].path).to.equal("homePageRoutePath()");
350+
});
351+
});
352+
316353
// ── cleanFileSpreads() — path alias resolution (issue #1545) ─────────────
317354

318355
describe("cleanFileSpreads() — path alias resolution (issue #1545)", () => {
@@ -346,6 +383,52 @@ export const routes: Routes = [...missingRoutes];`,
346383
}
347384
});
348385

386+
it("should not throw when spread uses a function call expression (issue #1317)", () => {
387+
const warnStub = sinon.stub(logger, "warn");
388+
try {
389+
const project = new Project({ useInMemoryFileSystem: true });
390+
const sourceFile = project.createSourceFile(
391+
"/tmp/test-spread-call-routes.ts",
392+
`import { Routes } from '@angular/router';
393+
const communicationCommonRoutes = () => [{ path: 'communication' }];
394+
export const routes: Routes = [{ path: 'root', children: [...communicationCommonRoutes(), { path: 'fallback' }] }];`,
395+
{ overwrite: true },
396+
);
397+
398+
expect(() =>
399+
routerParser.cleanFileSpreads(sourceFile),
400+
).not.to.throw();
401+
expect(sourceFile.getText()).to.contain(
402+
"...communicationCommonRoutes()",
403+
);
404+
expect(warnStub.called).to.equal(true);
405+
} finally {
406+
warnStub.restore();
407+
}
408+
});
409+
410+
it("should not throw when spread uses a property-access call expression (issue #1293)", () => {
411+
const warnStub = sinon.stub(logger, "warn");
412+
try {
413+
const project = new Project({ useInMemoryFileSystem: true });
414+
const sourceFile = project.createSourceFile(
415+
"/tmp/test-spread-map-routes.ts",
416+
`import { Routes } from '@angular/router';
417+
const langs = ['en', 'pl'];
418+
export const routes: Routes = [{ path: 'root', children: [...langs.map(lang => ({ path: lang }))] }];`,
419+
{ overwrite: true },
420+
);
421+
422+
expect(() =>
423+
routerParser.cleanFileSpreads(sourceFile),
424+
).not.to.throw();
425+
expect(sourceFile.getText()).to.contain("...langs.map");
426+
expect(warnStub.called).to.equal(true);
427+
} finally {
428+
warnStub.restore();
429+
}
430+
});
431+
349432
it("should resolve a path alias from tsconfig.paths and inline the spread without crashing (issue #1545)", () => {
350433
Configuration.mainData.tsconfig = path.join(
351434
fixtureRoot,

0 commit comments

Comments
 (0)