Skip to content

Commit 947e4dd

Browse files
committed
fix(guards): classify functional route guards as guards, not miscellaneous variables
closes #1406
1 parent d4c77de commit 947e4dd

3 files changed

Lines changed: 63 additions & 0 deletions

File tree

‎src/app/compiler/angular-dependencies.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,44 @@ export class AngularDependencies extends FrameworkDependencies {
13341334
deps.description =
13351335
markedAcl(rawDescription);
13361336
}
1337+
if (this.isFunctionalGuardType(infos.type)) {
1338+
const guardDep: any = {
1339+
name,
1340+
id:
1341+
"guard-" +
1342+
name +
1343+
"-" +
1344+
crypto
1345+
.createHash("sha512")
1346+
.update(name + file)
1347+
.digest("hex"),
1348+
file: file,
1349+
type: "guard",
1350+
coverageIgnore: isCoverageIgnore(
1351+
variableNode,
1352+
),
1353+
deprecated,
1354+
deprecationMessage,
1355+
description:
1356+
deps.description || "",
1357+
rawdescription:
1358+
deps.rawdescription || "",
1359+
properties: [],
1360+
methods: [],
1361+
sourceCode: srcFile.getText(),
1362+
};
1363+
if (!isIgnore(variableNode)) {
1364+
if (!this.isSymbolAllowed(name, file)) {
1365+
logger.debug(
1366+
`Skipping guard ${name} (not in public API)`,
1367+
);
1368+
return;
1369+
}
1370+
this.debug(guardDep);
1371+
outputSymbols.guards.push(guardDep);
1372+
}
1373+
return;
1374+
}
13371375
if (isModuleWithProviders(variableNode)) {
13381376
const routingInitializer =
13391377
getModuleWithProviders(
@@ -1828,6 +1866,15 @@ export class AngularDependencies extends FrameworkDependencies {
18281866
);
18291867
}
18301868

1869+
private isFunctionalGuardType(type?: string): boolean {
1870+
if (!type) {
1871+
return false;
1872+
}
1873+
return /(?:^|\W)(CanActivateFn|CanActivateChildFn|CanDeactivateFn|CanLoadFn|CanMatchFn|ResolveFn)(?:\W|$)/.test(
1874+
type,
1875+
);
1876+
}
1877+
18311878
private getSymboleName(node): string {
18321879
return node.name?.text;
18331880
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { CanActivateFn } from '@angular/router';
2+
3+
export const functionalAuthGuard: CanActivateFn = () => {
4+
return true;
5+
};

‎test/src/cli/cli-generation-big-app.spec.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ describe("CLI simple generation - big app", () => {
2424
todoStoreFile,
2525
typeAliasesFile,
2626
functionsFile,
27+
variablesFile,
2728
contactInfoInterfaceFile;
2829

2930
let routesIndex;
@@ -82,6 +83,7 @@ describe("CLI simple generation - big app", () => {
8283

8384
typeAliasesFile = read(`${distFolder}/miscellaneous/typealiases.html`);
8485
functionsFile = read(`${distFolder}/miscellaneous/functions.html`);
86+
variablesFile = read(`${distFolder}/miscellaneous/variables.html`);
8587

8688
contactInfoInterfaceFile = read(
8789
`${distFolder}/interfaces/ContactInfo.html`,
@@ -253,6 +255,15 @@ describe("CLI simple generation - big app", () => {
253255
expect(file).to.be.false;
254256
});
255257

258+
it("should have generated functional guards", () => {
259+
const file = exists(`${distFolder}/guards/functionalAuthGuard.html`);
260+
expect(file).to.be.true;
261+
});
262+
263+
it("shouldn't list functional guards in miscellaneous variables", () => {
264+
expect(variablesFile).to.not.contain("functionalAuthGuard");
265+
});
266+
256267
it("should have generated modules", () => {
257268
const file = exists(distFolder + "/modules/AboutModule.html");
258269
expect(file).to.be.true;

0 commit comments

Comments
 (0)