Skip to content

Commit

Permalink
fix(): do not scan INSTALLED_APPS and APP in storyboard functions
Browse files Browse the repository at this point in the history
  • Loading branch information
weareoutman committed Jul 25, 2024
1 parent d36bf7b commit 7b793f9
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 29 deletions.
4 changes: 3 additions & 1 deletion packages/brick-utils/src/scanAppInStoryboard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe("scanPermissionActionsInStoryboard", () => {
bg: true,
properties: {
any: "<% APP.getMenu('main-menu-1') %>",
ignore: "<% APP.config.abc %>",
},
},
],
Expand Down Expand Up @@ -60,7 +61,7 @@ describe("scanPermissionActionsInStoryboard", () => {
} as any;
expect(scanAppGetMenuInStoryboard(storyboard).sort()).toEqual([
"main-menu-1",
"main-menu-2",
// "main-menu-2",
"main-menu-3",
]);
});
Expand Down Expand Up @@ -90,6 +91,7 @@ describe("scanPermissionActionsInAny", () => {
bad9: "<% APP.getMenu('menu-9' %>",
bad10: "<% (APP) => APP.check('menu-10') %>",
bad11: "<% APP.getMenu('menu-11', 'menu-12') %>",
ignore: "<% APP.config.abc %>",
},
};
expect(scanAppGetMenuInAny(brickConf).sort()).toEqual([
Expand Down
32 changes: 18 additions & 14 deletions packages/brick-utils/src/scanAppInStoryboard.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
import { Storyboard } from "@next-core/brick-types";
import { PrecookHooks } from "./cook";
import {
visitStoryboardExpressions,
visitStoryboardFunctions,
} from "./visitStoryboard";
import { EstreeLiteral } from "./cook";
import { PrecookHooks, EstreeLiteral } from "./cook";
import { visitStoryboardExpressions } from "./visitStoryboard";

const APP = "APP";
const GET_MENUS = "getMenu";
const GET_MENU = "getMenu";

export function scanAppGetMenuInStoryboard(storyboard: Storyboard): string[] {
const collection = new Set<string>();
const beforeVisitPermissions = beforeVisitAppFactory(collection);
const { customTemplates, functions } = storyboard.meta ?? {};
const beforeVisitApp = beforeVisitAppFactory(collection);
const { customTemplates } = storyboard.meta ?? {};
visitStoryboardExpressions(
[storyboard.routes, customTemplates],
beforeVisitPermissions,
APP
beforeVisitApp,
{
matchExpressionString: matchAppGetMenu,
}
);
visitStoryboardFunctions(functions, beforeVisitPermissions);
// // `APP` is not available in storyboard functions
return Array.from(collection);
}

export function scanAppGetMenuInAny(data: unknown): string[] {
const collection = new Set<string>();
visitStoryboardExpressions(data, beforeVisitAppFactory(collection), APP);
visitStoryboardExpressions(data, beforeVisitAppFactory(collection), {
matchExpressionString: matchAppGetMenu,
});
return Array.from(collection);
}

function matchAppGetMenu(source: string): boolean {
return source.includes(APP) && source.includes(GET_MENU);
}

function beforeVisitAppFactory(
collection: Set<string>
): PrecookHooks["beforeVisitGlobal"] {
Expand All @@ -42,7 +46,7 @@ function beforeVisitAppFactory(
memberParent.key === "object" &&
!memberParent.node.computed &&
memberParent.node.property.type === "Identifier" &&
memberParent.node.property.name === GET_MENUS
memberParent.node.property.name === GET_MENU
) {
if (callParent.node.arguments.length === 1) {
const menuId = (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { Storyboard } from "@next-core/brick-types";
import {
scanPermissionActionsInStoryboard,
scanPermissionActionsInAny,
} from "./scanPermissionActionsInStoryboard";
import { scanInstalledAppsInStoryboard } from "./scanInstalledAppsInStoryboard";

describe("scanInstalledAppsInStoryboard", () => {
Expand Down Expand Up @@ -68,7 +64,7 @@ describe("scanInstalledAppsInStoryboard", () => {
},
} as any;
expect(scanInstalledAppsInStoryboard(storyboard).sort()).toEqual([
"my-app-in-functions",
// "my-app-in-functions",
"my-app-in-menus",
"my-app-in-routes",
"my-app-in-templates",
Expand Down
9 changes: 3 additions & 6 deletions packages/brick-utils/src/scanInstalledAppsInStoryboard.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { Storyboard } from "@next-core/brick-types";
import { EstreeLiteral } from "@next-core/cook";
import { PrecookHooks } from "./cook";
import {
visitStoryboardExpressions,
visitStoryboardFunctions,
} from "./visitStoryboard";
import { visitStoryboardExpressions } from "./visitStoryboard";

const INSTALLED_APPS = "INSTALLED_APPS";
const has = "has";
Expand All @@ -14,13 +11,13 @@ export function scanInstalledAppsInStoryboard(
): string[] {
const collection = new Set<string>();
const beforeVisitInstalledApps = beforeVisitInstalledAppsFactory(collection);
const { customTemplates, functions, menus } = storyboard.meta ?? {};
const { customTemplates, menus } = storyboard.meta ?? {};
visitStoryboardExpressions(
[storyboard.routes, customTemplates, menus],
beforeVisitInstalledApps,
INSTALLED_APPS
);
visitStoryboardFunctions(functions, beforeVisitInstalledApps);
// `INSTALLED_APPS` is not available in storyboard functions
return Array.from(collection);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ export function scanPermissionActionsInStoryboard(
beforeVisitPermissions,
PERMISSIONS
);
visitStoryboardFunctions(functions, beforeVisitPermissions);
visitStoryboardFunctions(functions, beforeVisitPermissions, {
matchSource: (source) => source.includes(PERMISSIONS),
});
return Array.from(collection);
}

Expand Down
11 changes: 9 additions & 2 deletions packages/brick-utils/src/visitStoryboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ import {
precookFunction,
PrecookHooks,
isEvaluable,
isSnippetEvaluation,
preevaluate,
} from "./cook";
import { isObject } from "./isObject";

export function visitStoryboardFunctions(
functions: StoryboardFunction[],
beforeVisitGlobal: PrecookHooks["beforeVisitGlobal"]
beforeVisitGlobal: PrecookHooks["beforeVisitGlobal"],
options?: VisitStoryboardFunctionsOptions
): void {
if (Array.isArray(functions)) {
for (const fn of functions) {
if (options?.matchSource && !options.matchSource(fn.source)) {
continue;
}
try {
precookFunction(fn.source, {
typescript: fn.typescript,
Expand All @@ -28,6 +31,10 @@ export function visitStoryboardFunctions(
}
}

interface VisitStoryboardFunctionsOptions {
matchSource?: (source: string) => boolean;
}

interface VisitStoryboardExpressionsOptions {
matchExpressionString: (v: string) => boolean;
visitNonExpressionString?: (v: string) => unknown;
Expand Down

0 comments on commit 7b793f9

Please sign in to comment.