Skip to content

Commit

Permalink
refactor: check for edge cases too
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Feb 28, 2022
1 parent e3f43f9 commit 42f82ed
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions src/matcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,28 +211,17 @@ export function createRouterMatcher(
return matchers
}

function isChildOf(
child: RouteRecordMatcher,
parent: RouteRecordMatcher
): Boolean {
return parent.children.some(currChild => {
if (currChild === child) return true

return isChildOf(child, currChild)
})
}

function insertMatcher(matcher: RouteRecordMatcher) {
let i = 0
// console.log('i is', { i })
while (
i < matchers.length &&
comparePathParserScore(matcher, matchers[i]) >= 0 &&
!isChildOf(matcher, matchers[i])
// Adding children with empty path should still appear before the parent
// https://github.com/vuejs/router/issues/1124
(matcher.record.path !== matchers[i].record.path ||
!isRecordChildOf(matcher, matchers[i]))
)
i++
// console.log('END i is', { i })
// while (i < matchers.length && matcher.score <= matchers[i].score) i++
matchers.splice(i, 0, matcher)
// only add the original record to the name map
if (matcher.record.name && !isAliasRecord(matcher))
Expand Down Expand Up @@ -474,4 +463,13 @@ function checkMissingParamsInAbsolutePath(
}
}

function isRecordChildOf(
record: RouteRecordMatcher,
parent: RouteRecordMatcher
): boolean {
return parent.children.some(
child => child === record || isRecordChildOf(record, child)
)
}

export type { PathParserOptions, _PathParserOptions }

0 comments on commit 42f82ed

Please sign in to comment.