From f86bd410e658f03b644fb7c3b3fee4383cac2935 Mon Sep 17 00:00:00 2001 From: Camila Belo Date: Wed, 20 Sep 2023 11:10:09 +0200 Subject: [PATCH] refactor(frontend-app-api): to inline validation Signed-off-by: Camila Belo --- .../frontend-app-api/src/wiring/createApp.tsx | 42 ++++--------------- .../frontend-app-api/src/wiring/parameters.ts | 17 ++++---- 2 files changed, 17 insertions(+), 42 deletions(-) diff --git a/packages/frontend-app-api/src/wiring/createApp.tsx b/packages/frontend-app-api/src/wiring/createApp.tsx index bc16fd9559..abd135cc5f 100644 --- a/packages/frontend-app-api/src/wiring/createApp.tsx +++ b/packages/frontend-app-api/src/wiring/createApp.tsx @@ -161,38 +161,6 @@ export function createExtensionTree(options: { }; } -function hasCyclicalExtensionDependency( - extensionId: string, - extensionAncestorIds: string[] = [], -) { - return extensionAncestorIds.includes(extensionId); -} - -function stringifyExtensionDependencyGraph( - extensionId: string, - extensionAncestorIds: string[] = [], -) { - const [rootAncestorId, ...rest] = extensionAncestorIds.concat(extensionId); - return rest.reduce((graphString, ancestorId) => { - return `${graphString} → ${ancestorId}`; - }, rootAncestorId); -} - -function preventCyclicalExtensionDependency( - extensionId: string, - extensionAncestorIds: string[], -) { - if (hasCyclicalExtensionDependency(extensionId, extensionAncestorIds)) { - const extensionDependencyGraph = stringifyExtensionDependencyGraph( - extensionId, - extensionAncestorIds, - ); - throw new Error( - `There is a cyclical dependency with the extension "${extensionId}": ${extensionDependencyGraph}`, - ); - } -} - /** * @internal */ @@ -248,7 +216,15 @@ export function createInstances(options: { return existingInstance; } - preventCyclicalExtensionDependency(extensionId, extensionAncestorIds); + // Prevent cyclical dependencies + if (extensionAncestorIds.includes(extensionId)) { + const extensionDependencyGraph = extensionAncestorIds + .concat(extensionId) + .join(' → '); + throw new Error( + `There is a cyclical dependency with the extension "${extensionId}": ${extensionDependencyGraph}`, + ); + } const attachments = new Map( Array.from(attachmentMap.get(extensionId)?.entries() ?? []).map( diff --git a/packages/frontend-app-api/src/wiring/parameters.ts b/packages/frontend-app-api/src/wiring/parameters.ts index acde9d9ec0..b18853ab5c 100644 --- a/packages/frontend-app-api/src/wiring/parameters.ts +++ b/packages/frontend-app-api/src/wiring/parameters.ts @@ -188,14 +188,6 @@ export interface ExtensionInstanceParameters { config?: unknown; } -function preventRootExtensionOverride(id: string) { - if (id === 'root') { - throw new Error( - 'There is a root extension in the app config file and root extensions are not configurable', - ); - } -} - /** @internal */ export function mergeExtensionParameters(options: { sources: BackstagePlugin[]; @@ -229,7 +221,14 @@ export function mergeExtensionParameters(options: { for (const overrideParam of parameters) { const extensionId = overrideParam.id; - preventRootExtensionOverride(extensionId); + + // Prevent root parametrization + if (extensionId === 'root') { + throw new Error( + 'There is a root extension in the app config file and root extensions are not configurable', + ); + } + const existingIndex = overrides.findIndex( e => e.extension.id === extensionId, );