From 1748485b1b859da5894a5940f9cca43d62ec86f1 Mon Sep 17 00:00:00 2001 From: Kuitos Date: Thu, 19 Oct 2023 17:22:41 +0800 Subject: [PATCH] fix: fix eslint error --- packages/loader/src/index.ts | 3 ++- packages/qiankun/src/core/loadApp.ts | 4 ---- .../src/core/compartment/globalProps.ts | 2 +- .../sandbox/src/core/compartment/index.ts | 2 +- packages/sandbox/src/core/sandbox/globals.ts | 3 ++- packages/sandbox/src/core/sandbox/index.ts | 1 + packages/sandbox/src/patchers/index.ts | 4 ++-- packages/sandbox/src/utils.ts | 21 ++++++++----------- .../shared/src/assets-transpilers/types.ts | 8 +++---- 9 files changed, 21 insertions(+), 27 deletions(-) diff --git a/packages/loader/src/index.ts b/packages/loader/src/index.ts index c2cb93bad..7f073143c 100644 --- a/packages/loader/src/index.ts +++ b/packages/loader/src/index.ts @@ -1,3 +1,4 @@ +import type { Sandbox } from '@qiankunjs/sandbox'; import { qiankunHeadTagName } from '@qiankunjs/sandbox'; import type { BaseTranspilerOpts } from '@qiankunjs/shared'; import { @@ -25,7 +26,7 @@ type Entry = HTMLEntry; export type ImportOpts = { decoder?: (chunk: string) => string; nodeTransformer?: typeof transpileAssets; -} & BaseTranspilerOpts; +} & BaseTranspilerOpts & { sandbox?: Sandbox }; /** * @param entry diff --git a/packages/qiankun/src/core/loadApp.ts b/packages/qiankun/src/core/loadApp.ts index 2a2b1483d..cb832655a 100644 --- a/packages/qiankun/src/core/loadApp.ts +++ b/packages/qiankun/src/core/loadApp.ts @@ -154,10 +154,6 @@ export default async function loadApp( } function initContainer(container: HTMLElement, appName: string, sandboxCfg: AppConfiguration['sandbox']): void { - if (!container) { - throw new QiankunError('container is not existed'); - } - while (container.firstChild) { container.removeChild(container.firstChild); } diff --git a/packages/sandbox/src/core/compartment/globalProps.ts b/packages/sandbox/src/core/compartment/globalProps.ts index fc81d9b74..56acfefa2 100644 --- a/packages/sandbox/src/core/compartment/globalProps.ts +++ b/packages/sandbox/src/core/compartment/globalProps.ts @@ -53,7 +53,7 @@ function shouldSkipProperty(global: WindowProxy, p: string | number): boolean { if (isIE11()) { // https://github.com/kuitos/import-html-entry/pull/32,最小化 try 范围 try { - return global[p as number] && typeof window !== 'undefined' && global[p as number].parent === window; + return !!global[p as keyof WindowProxy] && typeof window !== 'undefined' && global[p as number].parent === window; } catch (err) { return true; } diff --git a/packages/sandbox/src/core/compartment/index.ts b/packages/sandbox/src/core/compartment/index.ts index a08c57b18..aae93b39c 100644 --- a/packages/sandbox/src/core/compartment/index.ts +++ b/packages/sandbox/src/core/compartment/index.ts @@ -17,7 +17,7 @@ declare global { interface Window { __compartment_window__?: Window; - [p: CompartmentGlobalId]: WindowProxy; + [p: CompartmentGlobalId]: WindowProxy | undefined; } } diff --git a/packages/sandbox/src/core/sandbox/globals.ts b/packages/sandbox/src/core/sandbox/globals.ts index b10530f0a..dd6703eb5 100644 --- a/packages/sandbox/src/core/sandbox/globals.ts +++ b/packages/sandbox/src/core/sandbox/globals.ts @@ -1,5 +1,6 @@ // generated from https://github.com/sindresorhus/globals/blob/main/globals.json es2015 part // only init its values while Proxy is supported +// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition export const globals = window.Proxy ? [ 'Array', @@ -60,4 +61,4 @@ export const globals = window.Proxy 'WeakMap', 'WeakSet', ].filter((p) => /* just keep the available properties in current window context */ p in window) - : []; \ No newline at end of file + : []; diff --git a/packages/sandbox/src/core/sandbox/index.ts b/packages/sandbox/src/core/sandbox/index.ts index 9401cd998..600a42b59 100644 --- a/packages/sandbox/src/core/sandbox/index.ts +++ b/packages/sandbox/src/core/sandbox/index.ts @@ -36,6 +36,7 @@ export function createSandboxContainer( ) { const { globalContext, extraGlobals = {} } = opts; let sandbox: Sandbox; + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition if (window.Proxy) { sandbox = new StandardSandbox(appName, extraGlobals, globalContext); } else { diff --git a/packages/sandbox/src/patchers/index.ts b/packages/sandbox/src/patchers/index.ts index cd8215bf9..7138150d8 100644 --- a/packages/sandbox/src/patchers/index.ts +++ b/packages/sandbox/src/patchers/index.ts @@ -17,7 +17,7 @@ export function patchAtBootstrapping(appName: string, getContainer: () => HTMLEl [SandboxType.Snapshot]: [], } as const; - return patchersInSandbox[sandbox.type]?.map((patch) => patch()); + return patchersInSandbox[sandbox.type].map((patch) => patch()); } export function patchAtMounting(appName: string, getContainer: () => HTMLElement, sandbox: Sandbox): Free[] { @@ -35,5 +35,5 @@ export function patchAtMounting(appName: string, getContainer: () => HTMLElement [SandboxType.Snapshot]: basePatchers, }; - return patchersInSandbox[sandbox.type]?.map((patch) => patch()); + return patchersInSandbox[sandbox.type].map((patch) => patch()); } diff --git a/packages/sandbox/src/utils.ts b/packages/sandbox/src/utils.ts index 8353c368c..7ba4b6de4 100644 --- a/packages/sandbox/src/utils.ts +++ b/packages/sandbox/src/utils.ts @@ -16,19 +16,16 @@ export function isConstructable(fn: CallableFunction): fn is CallableFunction { if (hasPrototypeMethods) return true; - const cachedValue = fnRegexCheckCacheMap.get(fn); - if (typeof cachedValue !== 'undefined') { - return cachedValue; + const cachedResult = fnRegexCheckCacheMap.get(fn); + if (typeof cachedResult !== 'undefined') { + return cachedResult; } - let constructable: boolean = hasPrototypeMethods; - if (!constructable) { - // fn.toString has a significant performance overhead, if hasPrototypeMethods check not passed, we will check the function string with regex - const fnString = fn.toString(); - const constructableFunctionRegex = /^function\b\s[A-Z].*/; - const classRegex = /^class\b/; - constructable = constructableFunctionRegex.test(fnString) || classRegex.test(fnString); - } + // fn.toString has a significant performance overhead, if hasPrototypeMethods check not passed, we will check the function string with regex + const fnString = fn.toString(); + const constructableFunctionRegex = /^function\b\s[A-Z].*/; + const classRegex = /^class\b/; + const constructable = constructableFunctionRegex.test(fnString) || classRegex.test(fnString); fnRegexCheckCacheMap.set(fn, constructable); return constructable; @@ -56,7 +53,7 @@ export function isCallable(fn: unknown): fn is CallableFunction { const frozenPropertyCacheMap = new WeakMap>(); export function isPropertyFrozen(target: object, p?: PropertyKey): boolean { - if (!target || !p) { + if (!p) { return false; } diff --git a/packages/shared/src/assets-transpilers/types.ts b/packages/shared/src/assets-transpilers/types.ts index 7e0344c8a..72fa4f3ce 100644 --- a/packages/shared/src/assets-transpilers/types.ts +++ b/packages/shared/src/assets-transpilers/types.ts @@ -2,16 +2,14 @@ * @author Kuitos * @since 2023-08-26 */ -// import type { Sandbox } from '@qiankunjs/sandbox'; import type { BaseLoaderOpts } from '../common'; - import type { MatchResult } from '../module-resolver'; export type BaseTranspilerOpts = BaseLoaderOpts & { moduleResolver?: (url: string) => MatchResult | undefined; - // TODO: 先把 sandbox 类型设置如下,解除和 @qiankunjs/loader 相互依赖的问题 - // eslint-disable-next-line @typescript-eslint/no-explicit-any - sandbox?: Record; + sandbox?: { + makeEvaluateFactory(source: string, sourceURL?: string): string; + }; }; export type AssetsTranspilerOpts = BaseTranspilerOpts & { rawNode: Node };