Skip to content

Commit 0eded8d

Browse files
committed
style(projects): optimize code style
1 parent f27d4c3 commit 0eded8d

File tree

44 files changed

+164
-207
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+164
-207
lines changed

ErrorBoundary.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const ErrorPage: FC<Props> = ({ error, resetErrorBoundary }) => {
3434
// 可以在这里根据不同的业务逻辑处理错误或者上报给日志服务
3535
const hook = HookSupportChecker()
3636

37-
console.error(error, 999);
37+
console.error(error);
3838

3939
return (
4040
<div className="size-full min-h-520px flex-col-center gap-16px overflow-hidden">

build/plugins/auto-import.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export function setupAutoImport(viteEnv: Env.ImportMeta) {
66
return AutoImport({
77
imports: ['react', 'react-router-dom', 'react-i18next', 'ahooks', { from: 'react', imports: ['FC'], type: true }],
88
include: [/\.[tj]sx?$/],
9-
dirs: ['src/hooks/**','src/components/**'],
9+
dirs: ['src/hooks/**', 'src/components/**'],
1010
dts: 'src/types/auto-imports.d.ts',
1111
resolvers: [
1212
IconsResolver({
@@ -20,11 +20,10 @@ export function setupAutoImport(viteEnv: Env.ImportMeta) {
2020
});
2121
}
2222

23-
24-
function autoImportAntd(componentName:string)
25-
{
23+
function autoImportAntd(componentName: string) {
2624
const pattern = /^A[A-Z]/;
27-
if (pattern.test(componentName)) {
28-
return { name: componentName.slice(1), from: 'antd' }
25+
if (!pattern.test(componentName)) {
26+
return { name: componentName.slice(1), from: 'antd' };
2927
}
28+
return null;
3029
}

build/plugins/router.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export function setupElegantRouter() {
99
base: 'src/layouts/base-layout/index.tsx',
1010
blank: 'src/layouts/blank-layout/index.tsx'
1111
},
12-
log:false,
12+
log: false,
1313
customRoutes: {
1414
names: [
1515
'exception_403',

eslint.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ export default defineConfig(
88
singleAttributePerLine: true,
99
trailingCommas: 'none'
1010
},
11-
ignores: ['src/layouts/modules/global-tab/index.tsx']
11+
ignores: ['src/layouts/modules/global-tab/index.tsx', 'ErrorBoundary.tsx']
1212
},
1313
{
1414
rules: {
1515
'no-underscore-dangle': 'off',
16-
'react/jsx-no-undef': [true, { allowGlobals: true }]
16+
'react/jsx-no-undef': 'off'
1717
}
1818
}
1919
);

packages/simple-router/src/hooks/useRouter/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createContext, useContext } from 'react';
22

3-
import Router from '../../router'
3+
import type Router from '../../router';
44

55
export const RouterContext = createContext<Router>({} as Router);
66
export function useRouter() {

packages/simple-router/src/matcher/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ class CreateRouterMatcher {
6464
const parentPath = parent.record.path as string;
6565
const connectingSlash = parentPath[parentPath.length - 1] === '/' ? '' : '/';
6666
normalizedRecord.path = parent.record.path + (path && connectingSlash + path);
67-
6867
}
6968

7069
// create the object beforehand, so it can be passed to children
@@ -204,7 +203,7 @@ class CreateRouterMatcher {
204203
* @returns An array of route names.
205204
*/
206205
getAllRouteNames() {
207-
return Array.from(this.matcherMap.keys())
206+
return Array.from(this.matcherMap.keys());
208207
}
209208

210209
/**
@@ -213,7 +212,6 @@ class CreateRouterMatcher {
213212
* @param matcher - The matcher object to insert.
214213
*/
215214
insertMatcher(matcher: RouteRecordRaw) {
216-
217215
if (matcher.record.path === '*') {
218216
this.matchers.unshift(matcher);
219217
} else {

packages/simple-router/src/matcher/pathMatcher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export function createRouteRecordMatcher(record: RouteRecordNormalized, parent?:
55
parent,
66
// these needs to be populated by the parent
77
children: [],
8-
name:record.name
8+
name: record.name
99
};
1010

1111
if (parent) {

packages/simple-router/src/matcher/shared.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@ export function generatePath(pathTemplate: string, params: { [key: string]: stri
4141
* @returns the normalized version
4242
*/
4343
export function normalizeRouteRecord(record: ElegantConstRoute): RouteRecordNormalized {
44-
4544
return {
46-
redirect: record.redirect||record.children&&record.children[0].path,
45+
redirect: record.redirect || (record.children && record.children[0].path),
4746
path: record.path || '',
4847
name: record.name,
4948
meta: record.meta || {},
50-
children: record.children?.map(child => {
51-
child.redirect=child.redirect||child.children&&child.children[0].path
52-
return child
53-
}) || [],
49+
children:
50+
record.children?.map(child => {
51+
child.redirect ||= child.children && child.children[0].path;
52+
return child;
53+
}) || [],
5454
component: record.component
5555
};
5656
}

packages/simple-router/src/matcher/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ export interface RouteRecordRaw {
1919
record: RouteRecordNormalized;
2020
children: RouteRecordRaw[];
2121
parent: RouteRecordRaw | undefined;
22-
name?:string
22+
name?: string;
2323
}

packages/simple-router/src/router.tsx

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ import type { ElegantConstRoute } from '@ohh-889/react-auto-route';
55
import React from 'react';
66
import type { RouteLocationNamedRaw, RouteLocationNormalizedLoaded, RouterOptions } from './types';
77
import CreateRouterMatcher from './matcher';
8-
import type { RouteRecordNormalized } from './matcher/types';
8+
import type { RouteRecordNormalized, RouteRecordRaw } from './matcher/types';
99
import { START_LOCATION_NORMALIZED } from './types';
1010
import { RouterContext } from './hooks/useRouter';
1111
import { RouteContext } from './hooks/useRoute';
1212
import { warn } from './warning';
13-
import type { RouteRecordRaw } from './matcher/types'
1413

1514
const historyCreatorMap: Record<
1615
'hash' | 'history' | 'memory',
@@ -90,33 +89,30 @@ class CreateRouter {
9089
});
9190

9291
// Update react-router's routes
93-
this.#changeRoutes()
92+
this.#changeRoutes();
9493
}
9594

9695
#changeRoutes() {
9796
this.reactRouter._internalSetRoutes([...this.initReactRoutes, ...this.reactRoutes]);
9897
}
9998

10099
removeRoute(name: string) {
101-
const matched = this.matcher.getRecordMatcher(name)
102-
if (!matched) return
100+
const matched = this.matcher.getRecordMatcher(name);
101+
if (!matched) return;
103102
if (matched.parent) {
103+
const parentNames = findParentNames(matched.parent);
104+
let routes = this.reactRoutes;
104105

105-
const parentNames = findParentNames(matched.parent)
106-
let routes = this.reactRoutes
107-
108-
parentNames.forEach(name => {
109-
const finalRoute = routes.find(route => route.id === name)
110-
if (finalRoute && finalRoute.children) routes = finalRoute.children
111-
})
112-
removeElement(routes, matched.name)
113-
106+
parentNames.forEach(parentName => {
107+
const finalRoute = routes.find(route => route.id === parentName);
108+
if (finalRoute && finalRoute.children) routes = finalRoute.children;
109+
});
110+
removeElement(routes, matched.name);
114111
} else {
115-
this.reactRoutes = this.reactRoutes.filter(route => route.id !== matched.record.name)
112+
this.reactRoutes = this.reactRoutes.filter(route => route.id !== matched.record.name);
116113
}
117-
this.#changeRoutes()
114+
this.#changeRoutes();
118115
this.matcher.removeRoute(name);
119-
120116
}
121117

122118
#onBeforeRouteChange = (
@@ -142,22 +138,20 @@ class CreateRouter {
142138
if (to.redirect) {
143139
if (to.redirect.startsWith('/')) {
144140
if (to.redirect === this.currentRoute.fullPath) {
145-
return true
141+
return true;
146142
}
147143
} else {
148-
const finalRoute = to.matched[to.matched.length - 1]
149-
144+
const finalRoute = to.matched[to.matched.length - 1];
150145

151-
const finalPath = getFullPath(finalRoute)
146+
const finalPath = getFullPath(finalRoute);
152147

153-
if (finalPath === this.currentRoute.fullPath) return true
148+
if (finalPath === this.currentRoute.fullPath) return true;
154149
}
155150
}
156151

157152
return beforeEach(to, this.currentRoute, this.#next);
158153
};
159154

160-
161155
#next(param?: boolean | string | Location | RouteLocationNamedRaw) {
162156
if (!param) return false;
163157
if (typeof param === 'string') {
@@ -319,26 +313,21 @@ function cleanParams(params: Record<string, any> | undefined): Record<string, an
319313
return Object.fromEntries(Object.entries(params).filter(([_, value]) => value !== null));
320314
}
321315

322-
323-
324-
325-
326316
function findParentNames(matched: RouteRecordRaw | undefined): (string | undefined)[] {
327-
const parentNames: (string | undefined)[] = []
317+
const parentNames: (string | undefined)[] = [];
328318

329319
function helper(current: RouteRecordRaw | undefined) {
330320
if (current?.parent) {
331-
helper(current.parent)
321+
helper(current.parent);
332322
}
333-
parentNames.push(current?.name)
323+
parentNames.push(current?.name);
334324
}
335325

336-
helper(matched)
326+
helper(matched);
337327

338-
return parentNames
328+
return parentNames;
339329
}
340330

341-
342331
function removeElement(arr: RouteObject[], name: string | undefined) {
343332
const index = arr.findIndex(route => route.id === name);
344333
if (index !== -1) {
@@ -347,15 +336,13 @@ function removeElement(arr: RouteObject[], name: string | undefined) {
347336
return arr;
348337
}
349338

350-
351-
function getFullPath(route: RouteRecordNormalized | ElegantConstRoute ): string {
339+
function getFullPath(route: RouteRecordNormalized | ElegantConstRoute): string {
352340
// 如果当前 route 存在并且有 children
353341
if (route && route.redirect && route.children && route.children.length > 0) {
354342
// 获取第一个子路由
355-
const firstChild = route.children.find(child => child.path === route.redirect)
343+
const firstChild = route.children.find(child => child.path === route.redirect);
356344
// 递归调用,继续拼接子路由的 path
357-
if (firstChild)
358-
return `${route.path}/${getFullPath(firstChild)}`;
345+
if (firstChild) return `${route.path}/${getFullPath(firstChild)}`;
359346
}
360347
// 如果没有 children,返回当前 route 的 path
361348
return route.path;

0 commit comments

Comments
 (0)