From 96ebf72bf0f57abb22fbae4139ade6989f62b000 Mon Sep 17 00:00:00 2001 From: Kuitos Date: Thu, 15 Oct 2020 23:23:49 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20format=20code=20(#1003)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .prettierignore | 3 +++ package.json | 13 +++++-------- src/apis.ts | 4 ++-- src/loader.ts | 6 +++--- src/prefetch.ts | 4 ++-- src/sandbox/__tests__/proxySandbox.test.ts | 6 +++--- src/sandbox/index.ts | 6 +++--- src/sandbox/patchers/css.ts | 8 ++++---- src/sandbox/patchers/dynamicAppend/common.ts | 8 ++++---- .../patchers/dynamicAppend/forLooseSandbox.ts | 4 ++-- .../patchers/dynamicAppend/forStrictSandbox.ts | 6 +++--- src/sandbox/patchers/historyListener.ts | 4 ++-- src/sandbox/patchers/index.ts | 4 ++-- src/sandbox/patchers/interval.ts | 4 ++-- src/sandbox/patchers/windowListener.ts | 2 +- src/sandbox/proxySandbox.ts | 6 +++--- src/sandbox/snapshotSandbox.ts | 4 ++-- src/utils.ts | 2 +- 18 files changed, 47 insertions(+), 47 deletions(-) diff --git a/.prettierignore b/.prettierignore index 516ff7c..ccde68f 100644 --- a/.prettierignore +++ b/.prettierignore @@ -4,3 +4,6 @@ /docs /es /lib +/coverage +.cache +examples diff --git a/package.json b/package.json index dcec6d6..03ab57b 100644 --- a/package.json +++ b/package.json @@ -39,8 +39,8 @@ "lint": "yarn lint:js && yarn lint:prettier", "lint:js": "eslint --cache --ext .js,.jsx,.ts,.tsx --format=pretty ./src", "lint:fix": "yarn lint:js -- --fix", - "lint:prettier": "check-prettier lint", - "prettier": "prettier -c --write **/*", + "lint:prettier": "prettier -c ./src/**/*", + "prettier": "prettier --write ./src/**/*", "docs:dev": "dumi dev", "docs:build": "dumi build", "ci": "yarn lint && yarn test", @@ -74,7 +74,6 @@ "@types/vfile-message": "1.x", "@umijs/fabric": "^2.0.7", "babel-plugin-import": "^1.12.1", - "check-prettier": "^1.0.3", "cross-env": "^7.0.2", "dumi": "^1.1.0-beta.24", "father-build": "^1.7.0", @@ -83,7 +82,7 @@ "lint-staged": "^9.4.2", "np": "^5.0.3", "npm-run-all": "^4.1.5", - "prettier": "^1.18.2", + "prettier": "^2.1.2", "rimraf": "^3.0.0", "ts-jest": "^25.2.1", "typescript": "^3.4.5" @@ -96,12 +95,10 @@ }, "lint-staged": { "**/*.{js,ts,json,css,md}": [ - "yarn prettier", - "git add" + "prettier -w" ], "**/*.{js,ts}": [ - "yarn lint:fix", - "git add" + "yarn lint:fix" ] }, "jest": { diff --git a/src/apis.ts b/src/apis.ts index f1cda4d..eb7daf3 100644 --- a/src/apis.ts +++ b/src/apis.ts @@ -16,11 +16,11 @@ export function registerMicroApps( lifeCycles?: FrameworkLifeCycles, ) { // Each app only needs to be registered once - const unregisteredApps = apps.filter(app => !microApps.some(registeredApp => registeredApp.name === app.name)); + const unregisteredApps = apps.filter((app) => !microApps.some((registeredApp) => registeredApp.name === app.name)); microApps = [...microApps, ...unregisteredApps]; - unregisteredApps.forEach(app => { + unregisteredApps.forEach((app) => { const { name, activeRule, loader = noop, props, ...appConfig } = app; registerApplication({ diff --git a/src/loader.ts b/src/loader.ts index d49a92a..b2f0520 100644 --- a/src/loader.ts +++ b/src/loader.ts @@ -311,7 +311,7 @@ export async function loadApp( offGlobalStateChange, }: Record = getMicroAppStateActions(appInstanceId); - const parcelConfigGetter: ParcelConfigObjectGetter = remountContainer => { + const parcelConfigGetter: ParcelConfigObjectGetter = (remountContainer) => { const parcelConfig: ParcelConfigObject = { name: appInstanceId, bootstrap, @@ -341,7 +341,7 @@ export async function loadApp( mountSandbox, // exec the chain after rendering to keep the behavior with beforeLoad async () => execHooksChain(toArray(beforeMount), app, global), - async props => mount({ ...props, container: appWrapperGetter(), setGlobalState, onGlobalStateChange }), + async (props) => mount({ ...props, container: appWrapperGetter(), setGlobalState, onGlobalStateChange }), // finish loading after app mounted async () => render({ element: appWrapperElement, loading: false, remountContainer }, 'mounted'), async () => execHooksChain(toArray(afterMount), app, global), @@ -360,7 +360,7 @@ export async function loadApp( ], unmount: [ async () => execHooksChain(toArray(beforeUnmount), app, global), - async props => unmount({ ...props, container: appWrapperGetter() }), + async (props) => unmount({ ...props, container: appWrapperGetter() }), unmountSandbox, async () => execHooksChain(toArray(afterUnmount), app, global), async () => { diff --git a/src/prefetch.ts b/src/prefetch.ts index 7a41f9b..d745a4e 100644 --- a/src/prefetch.ts +++ b/src/prefetch.ts @@ -77,7 +77,7 @@ function prefetch(entry: Entry, opts?: ImportEntryOpts): void { function prefetchAfterFirstMounted(apps: AppMetadata[], opts?: ImportEntryOpts): void { window.addEventListener('single-spa:first-mount', function listener() { - const notLoadedApps = apps.filter(app => getAppStatus(app.name) === NOT_LOADED); + const notLoadedApps = apps.filter((app) => getAppStatus(app.name) === NOT_LOADED); if (process.env.NODE_ENV === 'development') { const mountedApps = getMountedApps(); @@ -103,7 +103,7 @@ export function doPrefetchStrategy( prefetchStrategy: PrefetchStrategy, importEntryOpts?: ImportEntryOpts, ) { - const appsName2Apps = (names: string[]): AppMetadata[] => apps.filter(app => names.includes(app.name)); + const appsName2Apps = (names: string[]): AppMetadata[] => apps.filter((app) => names.includes(app.name)); if (Array.isArray(prefetchStrategy)) { prefetchAfterFirstMounted(appsName2Apps(prefetchStrategy as string[]), importEntryOpts); diff --git a/src/sandbox/__tests__/proxySandbox.test.ts b/src/sandbox/__tests__/proxySandbox.test.ts index 22618fd..d46b997 100644 --- a/src/sandbox/__tests__/proxySandbox.test.ts +++ b/src/sandbox/__tests__/proxySandbox.test.ts @@ -214,7 +214,7 @@ test('document accessing should modify the attachDocProxySymbol value every time expect(d1).toBe(document); }); -test('document attachDocProxySymbol mark should be remove before next tasl', done => { +test('document attachDocProxySymbol mark should be remove before next tasl', (done) => { const { proxy } = new ProxySandbox('doc-symbol'); const d1 = proxy.document; expect(documentAttachProxyMap.get(d1)).toBe(proxy); @@ -225,10 +225,10 @@ test('document attachDocProxySymbol mark should be remove before next tasl', don }); }); -test('document should work well with MutationObserver', done => { +test('document should work well with MutationObserver', (done) => { const docProxy = new ProxySandbox('doc').proxy; - const observer = new MutationObserver(mutations => { + const observer = new MutationObserver((mutations) => { if (mutations[0]) { expect(mutations[0].target).toBe(document.body); observer.disconnect(); diff --git a/src/sandbox/index.ts b/src/sandbox/index.ts index 4d4487b..16e37de 100644 --- a/src/sandbox/index.ts +++ b/src/sandbox/index.ts @@ -68,7 +68,7 @@ export function createSandbox( // must rebuild the side effects which added at bootstrapping firstly to recovery to nature state if (sideEffectsRebuildersAtBootstrapping.length) { - sideEffectsRebuildersAtBootstrapping.forEach(rebuild => rebuild()); + sideEffectsRebuildersAtBootstrapping.forEach((rebuild) => rebuild()); } /* ------------------------------------------ 2. 开启全局变量补丁 ------------------------------------------*/ @@ -78,7 +78,7 @@ export function createSandbox( /* ------------------------------------------ 3. 重置一些初始化时的副作用 ------------------------------------------*/ // 存在 rebuilder 则表明有些副作用需要重建 if (sideEffectsRebuildersAtMounting.length) { - sideEffectsRebuildersAtMounting.forEach(rebuild => rebuild()); + sideEffectsRebuildersAtMounting.forEach((rebuild) => rebuild()); } // clean up rebuilders @@ -91,7 +91,7 @@ export function createSandbox( async unmount() { // record the rebuilders of window side effects (event listeners or timers) // note that the frees of mounting phase are one-off as it will be re-init at next mounting - sideEffectsRebuilders = [...bootstrappingFreers, ...mountingFreers].map(free => free()); + sideEffectsRebuilders = [...bootstrappingFreers, ...mountingFreers].map((free) => free()); sandbox.inactive(); }, diff --git a/src/sandbox/patchers/css.ts b/src/sandbox/patchers/css.ts index 1b97f6e..daa5637 100644 --- a/src/sandbox/patchers/css.ts +++ b/src/sandbox/patchers/css.ts @@ -53,7 +53,7 @@ export class ScopedCSS { return; } - const mutator = new MutationObserver(mutations => { + const mutator = new MutationObserver((mutations) => { for (let i = 0; i < mutations.length; i += 1) { const mutation = mutations[i]; @@ -83,7 +83,7 @@ export class ScopedCSS { private rewrite(rules: CSSRule[], prefix: string = '') { let css = ''; - rules.forEach(rule => { + rules.forEach((rule) => { switch (rule.type) { case RuleType.STYLE: css += this.ruleStyle(rule as CSSStyleRule, prefix); @@ -135,11 +135,11 @@ export class ScopedCSS { } // handle grouping selector, a,span,p,div { ... } - cssText = cssText.replace(/^[\s\S]+{/, selectors => + cssText = cssText.replace(/^[\s\S]+{/, (selectors) => selectors.replace(/(^|,\n?)([^,]+)/g, (item, p, s) => { // handle div,body,span { ... } if (rootSelectorRE.test(item)) { - return item.replace(rootSelectorRE, m => { + return item.replace(rootSelectorRE, (m) => { // do not discard valid previous character, such as body,html or *:not(:root) const whitePrevChars = [',', '(']; diff --git a/src/sandbox/patchers/dynamicAppend/common.ts b/src/sandbox/patchers/dynamicAppend/common.ts index 0ad81b2..664b54f 100644 --- a/src/sandbox/patchers/dynamicAppend/common.ts +++ b/src/sandbox/patchers/dynamicAppend/common.ts @@ -44,7 +44,7 @@ const styledComponentCSSRulesMap = new WeakMap(); const dynamicScriptAttachedCommentMap = new WeakMap(); export function recordStyledComponentsCSSRules(styleElements: HTMLStyleElement[]): void { - styleElements.forEach(styleElement => { + styleElements.forEach((styleElement) => { /* With a styled-components generated style element, we need to record its cssRules for restore next re-mounting time. We're doing this because the sheet of style element is going to be cleaned automatically by browser after the style element dom removed from document. @@ -267,11 +267,11 @@ export function patchHTMLDynamicAppendPrototypeFunctions( ) { HTMLHeadElement.prototype.removeChild = getNewRemoveChild( rawHeadRemoveChild, - element => containerConfigGetter(element).appWrapperGetter, + (element) => containerConfigGetter(element).appWrapperGetter, ); HTMLBodyElement.prototype.removeChild = getNewRemoveChild( rawBodyRemoveChild, - element => containerConfigGetter(element).appWrapperGetter, + (element) => containerConfigGetter(element).appWrapperGetter, ); } @@ -289,7 +289,7 @@ export function rebuildCSSRules( styleSheetElements: HTMLStyleElement[], reAppendElement: (stylesheetElement: HTMLStyleElement) => void, ) { - styleSheetElements.forEach(stylesheetElement => { + styleSheetElements.forEach((stylesheetElement) => { // re-append the dynamic stylesheet to sub-app container // Using document.head.appendChild ensures that appendChild invocation can also directly use the HTMLHeadElement.prototype.appendChild method which is overwritten at mounting phase reAppendElement(stylesheetElement); diff --git a/src/sandbox/patchers/dynamicAppend/forLooseSandbox.ts b/src/sandbox/patchers/dynamicAppend/forLooseSandbox.ts index 6cdc99f..8642d09 100644 --- a/src/sandbox/patchers/dynamicAppend/forLooseSandbox.ts +++ b/src/sandbox/patchers/dynamicAppend/forLooseSandbox.ts @@ -40,7 +40,7 @@ export function patchLooseSandbox( and remove them after the url change triggered and qiankun app is unmouting see https://github.com/ReactTraining/history/blob/master/modules/createHashHistory.js#L222-L230 */ - () => checkActivityFunctions(window.location).some(name => name === appName), + () => checkActivityFunctions(window.location).some((name) => name === appName), () => ({ appName, appWrapperGetter, @@ -70,7 +70,7 @@ export function patchLooseSandbox( // the dynamic style sheet would be removed automatically while unmoutting return function rebuild() { - rebuildCSSRules(dynamicStyleSheetElements, stylesheetElement => + rebuildCSSRules(dynamicStyleSheetElements, (stylesheetElement) => // Using document.head.appendChild ensures that appendChild invocation can also directly use the HTMLHeadElement.prototype.appendChild method which is overwritten at mounting phase document.head.appendChild.call(appWrapperGetter(), stylesheetElement), ); diff --git a/src/sandbox/patchers/dynamicAppend/forStrictSandbox.ts b/src/sandbox/patchers/dynamicAppend/forStrictSandbox.ts index b2605eb..6d84671 100644 --- a/src/sandbox/patchers/dynamicAppend/forStrictSandbox.ts +++ b/src/sandbox/patchers/dynamicAppend/forStrictSandbox.ts @@ -75,8 +75,8 @@ export function patchStrictSandbox( const unpatchDocumentCreate = patchDocumentCreateElement(); const unpatchDynamicAppendPrototypeFunctions = patchHTMLDynamicAppendPrototypeFunctions( - element => elementAttachContainerConfigMap.has(element), - element => elementAttachContainerConfigMap.get(element)!, + (element) => elementAttachContainerConfigMap.has(element), + (element) => elementAttachContainerConfigMap.get(element)!, ); if (!mounting) bootstrappingPatchCount++; @@ -102,7 +102,7 @@ export function patchStrictSandbox( // the dynamic style sheet would be removed automatically while unmoutting return function rebuild() { - rebuildCSSRules(dynamicStyleSheetElements, stylesheetElement => + rebuildCSSRules(dynamicStyleSheetElements, (stylesheetElement) => rawHeadAppendChild.call(appWrapperGetter(), stylesheetElement), ); }; diff --git a/src/sandbox/patchers/historyListener.ts b/src/sandbox/patchers/historyListener.ts index bf6a717..19636be 100644 --- a/src/sandbox/patchers/historyListener.ts +++ b/src/sandbox/patchers/historyListener.ts @@ -41,12 +41,12 @@ export default function patch() { if (historyListeners.length) { rebuild = () => { // 必须使用 window.g_history.listen 的方式重新绑定 listener,从而能保证 rebuild 这部分也能被捕获到,否则在应用卸载后无法正确的移除这部分副作用 - historyListeners.forEach(listener => (window as any).g_history.listen(listener)); + historyListeners.forEach((listener) => (window as any).g_history.listen(listener)); }; } // 卸载余下的 listener - historyUnListens.forEach(unListen => unListen()); + historyUnListens.forEach((unListen) => unListen()); // restore if ((window as any).g_history && isFunction((window as any).g_history.listen)) { diff --git a/src/sandbox/patchers/index.ts b/src/sandbox/patchers/index.ts index fa41f5b..40c4877 100644 --- a/src/sandbox/patchers/index.ts +++ b/src/sandbox/patchers/index.ts @@ -38,7 +38,7 @@ export function patchAtMounting( ], }; - return patchersInSandbox[sandbox.type]?.map(patch => patch()); + return patchersInSandbox[sandbox.type]?.map((patch) => patch()); } export function patchAtBootstrapping( @@ -60,7 +60,7 @@ export function patchAtBootstrapping( ], }; - return patchersInSandbox[sandbox.type]?.map(patch => patch()); + return patchersInSandbox[sandbox.type]?.map((patch) => patch()); } export { css }; diff --git a/src/sandbox/patchers/interval.ts b/src/sandbox/patchers/interval.ts index 74b568b..5b38664 100644 --- a/src/sandbox/patchers/interval.ts +++ b/src/sandbox/patchers/interval.ts @@ -13,7 +13,7 @@ export default function patch(global: Window) { let intervals: number[] = []; global.clearInterval = (intervalId: number) => { - intervals = intervals.filter(id => id !== intervalId); + intervals = intervals.filter((id) => id !== intervalId); return rawWindowClearInterval(intervalId); }; @@ -24,7 +24,7 @@ export default function patch(global: Window) { }; return function free() { - intervals.forEach(id => global.clearInterval(id)); + intervals.forEach((id) => global.clearInterval(id)); global.setInterval = rawWindowInterval; global.clearInterval = rawWindowClearInterval; diff --git a/src/sandbox/patchers/windowListener.ts b/src/sandbox/patchers/windowListener.ts index 20caeb8..677ddde 100644 --- a/src/sandbox/patchers/windowListener.ts +++ b/src/sandbox/patchers/windowListener.ts @@ -36,7 +36,7 @@ export default function patch(global: WindowProxy) { return function free() { listenerMap.forEach((listeners, type) => - [...listeners].forEach(listener => global.removeEventListener(type, listener)), + [...listeners].forEach((listener) => global.removeEventListener(type, listener)), ); global.addEventListener = rawAddEventListener; global.removeEventListener = rawRemoveEventListener; diff --git a/src/sandbox/proxySandbox.ts b/src/sandbox/proxySandbox.ts index 6e0e8c5..6140c9e 100644 --- a/src/sandbox/proxySandbox.ts +++ b/src/sandbox/proxySandbox.ts @@ -73,11 +73,11 @@ function createFakeWindow(global: Window) { > A property cannot be reported as non-configurable, if it does not exists as an own property of the target object or if it exists as a configurable own property of the target object. */ Object.getOwnPropertyNames(global) - .filter(p => { + .filter((p) => { const descriptor = Object.getOwnPropertyDescriptor(global, p); return !descriptor?.configurable; }) - .forEach(p => { + .forEach((p) => { const descriptor = Object.getOwnPropertyDescriptor(global, p); if (descriptor) { const hasGetter = Object.prototype.hasOwnProperty.call(descriptor, 'get'); @@ -150,7 +150,7 @@ export default class ProxySandbox implements SandBox { } if (--activeSandboxCount === 0) { - variableWhiteList.forEach(p => { + variableWhiteList.forEach((p) => { if (this.proxy.hasOwnProperty(p)) { // @ts-ignore delete window[p]; diff --git a/src/sandbox/snapshotSandbox.ts b/src/sandbox/snapshotSandbox.ts index cc4b14d..19681cc 100644 --- a/src/sandbox/snapshotSandbox.ts +++ b/src/sandbox/snapshotSandbox.ts @@ -38,7 +38,7 @@ export default class SnapshotSandbox implements SandBox { active() { // 记录当前快照 this.windowSnapshot = {} as Window; - iter(window, prop => { + iter(window, (prop) => { this.windowSnapshot[prop] = window[prop]; }); @@ -53,7 +53,7 @@ export default class SnapshotSandbox implements SandBox { inactive() { this.modifyPropsMap = {}; - iter(window, prop => { + iter(window, (prop) => { if (window[prop] !== this.windowSnapshot[prop]) { // 记录变更,恢复环境 this.modifyPropsMap[prop] = window[prop]; diff --git a/src/utils.ts b/src/utils.ts index f02e2e9..b4890c5 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -11,7 +11,7 @@ export function toArray(array: T | T[]): T[] { } export function sleep(ms: number) { - return new Promise(resolve => setTimeout(resolve, ms)); + return new Promise((resolve) => setTimeout(resolve, ms)); } /**