Skip to content

Commit fd97255

Browse files
committed
fix(router): preserve relative spread import resolution
closes #1307
1 parent 6bc2dae commit fd97255

4 files changed

Lines changed: 42 additions & 1 deletion

File tree

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1313,6 +1313,9 @@ export class RouterParserUtil {
13131313
const leadingFilePath = searchedImportPath
13141314
.split("/")
13151315
.shift();
1316+
const isRelativeImport =
1317+
searchedImportPath.startsWith("./") ||
1318+
searchedImportPath.startsWith("../");
13161319

13171320
// Try tsconfig path alias first (e.g. "@shared/*" → "src/app/shared/*")
13181321
const aliasResolved =
@@ -1324,7 +1327,11 @@ export class RouterParserUtil {
13241327
dirNamePath + "/" + searchedImportPath + ".ts",
13251328
);
13261329

1327-
if (!aliasResolved && routePathIsBad(importPath)) {
1330+
if (
1331+
!aliasResolved &&
1332+
!isRelativeImport &&
1333+
routePathIsBad(importPath)
1334+
) {
13281335
const leadingIndices = getIndicesOf(
13291336
leadingFilePath,
13301337
importPath,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { Routes } from "@angular/router";
2+
import { appRoutes } from "./custom-app-routes";
3+
4+
export const routes: Routes = [...appRoutes];
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Route } from "@angular/router";
2+
3+
export const appRoutes: Route[] = [
4+
{ path: "custom", component: "CustomComponent" as any },
5+
];

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,10 @@ const routes = [
497497
__dirname,
498498
"../../../../../test/fixtures/path-alias-spread",
499499
);
500+
const relativeFixtureRoot = path.join(
501+
__dirname,
502+
"../../../../../test/fixtures/relative-spread-import",
503+
);
500504

501505
afterEach(() => {
502506
Configuration.mainData.tsconfig = "";
@@ -582,5 +586,26 @@ export const routes: Routes = [{ path: 'root', children: [...langs.map(lang => (
582586
routerParser.cleanFileSpreads(sourceFile),
583587
).not.to.throw();
584588
});
589+
590+
it("should inline relative spread imports without mangling the path (issue #1307)", () => {
591+
const previousScannedFiles = routerParser.scannedFiles;
592+
try {
593+
routerParser.scannedFiles = [];
594+
const project = new Project();
595+
const sourceFile = project.addSourceFileAtPath(
596+
path.join(
597+
relativeFixtureRoot,
598+
"src/app/app-routing.module.ts",
599+
),
600+
);
601+
expect(() =>
602+
routerParser.cleanFileSpreads(sourceFile),
603+
).not.to.throw();
604+
expect(sourceFile.getText()).not.to.contain("...appRoutes");
605+
expect(sourceFile.getText()).to.contain('path: "custom"');
606+
} finally {
607+
routerParser.scannedFiles = previousScannedFiles;
608+
}
609+
});
585610
});
586611
});

0 commit comments

Comments
 (0)