Skip to content

Commit 509bddb

Browse files
committed
fix(router): avoid spread import path truncation
closes #1394
1 parent 7f4b74f commit 509bddb

2 files changed

Lines changed: 106 additions & 1 deletion

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1498,8 +1498,12 @@ export class RouterParserUtil {
14981498
!isRelativeImport &&
14991499
routePathIsBad(importPath)
15001500
) {
1501+
// Match only full path segments when trying to remove
1502+
// duplicated import prefixes. This avoids false positives
1503+
// when user folders contain dots/partial names (e.g. "felipe.jesus").
1504+
const duplicatedSegmentPattern = `${path.sep}${leadingFilePath}${path.sep}`;
15011505
const leadingIndices = getIndicesOf(
1502-
leadingFilePath,
1506+
duplicatedSegmentPattern,
15031507
importPath,
15041508
true,
15051509
);
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import { expect } from "chai";
2+
import { shell, read, exists } from "../helpers";
3+
import * as fs from "fs-extra";
4+
import * as os from "os";
5+
import * as path from "path";
6+
7+
describe("CLI issue #1394 - route spread path resolution", () => {
8+
const projectRoot = path.join(
9+
os.tmpdir(),
10+
`compodoc.issue-1394.felipe.jesus.${process.pid}`,
11+
);
12+
const distFolder = path.join(projectRoot, "documentation");
13+
14+
before(() => {
15+
fs.removeSync(projectRoot);
16+
fs.ensureDirSync(path.join(projectRoot, "src/app"));
17+
fs.ensureDirSync(path.join(projectRoot, "src/felipe"));
18+
19+
fs.writeFileSync(
20+
path.join(projectRoot, "tsconfig.json"),
21+
JSON.stringify(
22+
{
23+
include: ["src/**/*.ts"],
24+
},
25+
null,
26+
2,
27+
),
28+
);
29+
30+
fs.writeFileSync(
31+
path.join(projectRoot, "src/main.ts"),
32+
"import './app/app-routing.module';\n",
33+
);
34+
fs.writeFileSync(path.join(projectRoot, "src/polyfills.ts"), "\n");
35+
36+
fs.writeFileSync(
37+
path.join(projectRoot, "src/felipe/routes.ts"),
38+
`export const EXTRA_ROUTES = [
39+
{ path: 'spread-route', component: 'SpreadComponent' }
40+
];
41+
`,
42+
);
43+
44+
fs.writeFileSync(
45+
path.join(projectRoot, "src/app/app-routing.module.ts"),
46+
`import { NgModule } from '@angular/core';
47+
import { RouterModule, Routes } from '@angular/router';
48+
import { EXTRA_ROUTES } from 'felipe/routes';
49+
50+
const APP_ROUTES: Routes = [
51+
...EXTRA_ROUTES
52+
];
53+
54+
@NgModule({
55+
imports: [RouterModule.forRoot(APP_ROUTES)],
56+
exports: [RouterModule]
57+
})
58+
export class AppRoutingModule {}
59+
`,
60+
);
61+
});
62+
63+
after(() => {
64+
fs.removeSync(projectRoot);
65+
});
66+
67+
it("should resolve spread imports without truncating paths when cwd contains dots", () => {
68+
const ls = shell(
69+
"node",
70+
[
71+
path.resolve(process.cwd(), "bin/index-cli.js"),
72+
"-p",
73+
"./tsconfig.json",
74+
"-e",
75+
"json",
76+
"-d",
77+
distFolder,
78+
],
79+
{
80+
cwd: projectRoot,
81+
},
82+
);
83+
84+
const stdout = ls.stdout.toString();
85+
const stderr = ls.stderr.toString();
86+
87+
expect(stderr).to.equal("");
88+
expect(stdout).to.contain("Documentation generated");
89+
expect(stdout).to.not.contain("FileNotFoundError");
90+
expect(stdout).to.not.contain("Unhandled Rejection");
91+
expect(stdout).to.not.contain("/Users/felipe.ts");
92+
93+
expect(exists(path.join(distFolder, "documentation.json"))).to.equal(
94+
true,
95+
);
96+
const documentation = JSON.parse(
97+
read(path.join(distFolder, "documentation.json")),
98+
);
99+
expect(JSON.stringify(documentation)).to.contain("spread-route");
100+
});
101+
});

0 commit comments

Comments
 (0)