@@ -5,12 +5,11 @@ import type { ElegantConstRoute } from '@ohh-889/react-auto-route';
55import React from 'react' ;
66import type { RouteLocationNamedRaw , RouteLocationNormalizedLoaded , RouterOptions } from './types' ;
77import CreateRouterMatcher from './matcher' ;
8- import type { RouteRecordNormalized } from './matcher/types' ;
8+ import type { RouteRecordNormalized , RouteRecordRaw } from './matcher/types' ;
99import { START_LOCATION_NORMALIZED } from './types' ;
1010import { RouterContext } from './hooks/useRouter' ;
1111import { RouteContext } from './hooks/useRoute' ;
1212import { warn } from './warning' ;
13- import type { RouteRecordRaw } from './matcher/types'
1413
1514const 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-
326316function 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-
342331function 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