Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions packages/router-core/src/new-process-route-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type ParsedSegment = Uint16Array & {
* let output
* let cursor = 0
* while (cursor < path.length) {
* output = parseSegment(path, cursor, output)
* parseSegment(path, cursor, output)
* const end = output[5]
* cursor = end + 1
* ```
Expand All @@ -58,7 +58,7 @@ export function parseSegment(
start: number,
/** A Uint16Array (length: 6) to populate with the parsed segment data. */
output: Uint16Array = new Uint16Array(6),
): ParsedSegment {
): asserts output is ParsedSegment {
const next = path.indexOf('/', start)
const end = next === -1 ? path.length : next
const part = path.substring(start, end)
Expand All @@ -71,7 +71,7 @@ export function parseSegment(
output[3] = end
output[4] = end
output[5] = end
return output as ParsedSegment
return
}

// $ (wildcard)
Expand All @@ -83,7 +83,7 @@ export function parseSegment(
output[3] = total
output[4] = total
output[5] = total
return output as ParsedSegment
return
}

// $paramName
Expand All @@ -94,7 +94,7 @@ export function parseSegment(
output[3] = end
output[4] = end
output[5] = end
return output as ParsedSegment
return
}

const wildcardBracesMatch = part.match(WILDCARD_W_CURLY_BRACES_RE)
Expand All @@ -107,7 +107,7 @@ export function parseSegment(
output[3] = start + pLength + 2 // '$'
output[4] = start + pLength + 3 // skip '}'
output[5] = path.length
return output as ParsedSegment
return
}

const optionalParamBracesMatch = part.match(OPTIONAL_PARAM_W_CURLY_BRACES_RE)
Expand All @@ -122,7 +122,7 @@ export function parseSegment(
output[3] = start + pLength + 3 + paramName.length
output[4] = end - suffix.length
output[5] = end
return output as ParsedSegment
return
}

const paramBracesMatch = part.match(PARAM_W_CURLY_BRACES_RE)
Expand All @@ -137,7 +137,7 @@ export function parseSegment(
output[3] = start + pLength + 2 + paramName.length
output[4] = end - suffix.length
output[5] = end
return output as ParsedSegment
return
}

// fallback to static pathname (should never happen)
Expand All @@ -147,21 +147,20 @@ export function parseSegment(
output[3] = end
output[4] = end
output[5] = end
return output as ParsedSegment
}

/**
* Recursively parses the segments of the given route tree and populates a segment trie.
*
* @param data A reusable Uint16Array for parsing segments. (non important, we're just avoiding allocations)
* @param segment A reusable Uint16Array for parsing segments. (non important, we're just avoiding allocations)
* @param route The current route to parse.
* @param start The starting index for parsing within the route's full path.
* @param node The current segment node in the trie to populate.
* @param onRoute Callback invoked for each route processed.
*/
function parseSegments<TRouteLike extends RouteLike>(
defaultCaseSensitive: boolean,
data: Uint16Array,
segment: Uint16Array,
route: TRouteLike,
start: number,
node: AnySegmentNode<TRouteLike>,
Expand All @@ -175,7 +174,7 @@ function parseSegments<TRouteLike extends RouteLike>(
const length = path.length
const caseSensitive = route.options?.caseSensitive ?? defaultCaseSensitive
while (cursor < length) {
const segment = parseSegment(path, cursor, data)
parseSegment(path, cursor, segment)
let nextNode: AnySegmentNode<TRouteLike>
const start = cursor
const end = segment[5]
Expand Down Expand Up @@ -340,7 +339,7 @@ function parseSegments<TRouteLike extends RouteLike>(
for (const child of route.children) {
parseSegments(
defaultCaseSensitive,
data,
segment,
child as TRouteLike,
cursor,
node,
Expand Down
4 changes: 2 additions & 2 deletions packages/router-core/src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export function resolvePath({
if (i > 0) joined += '/'
const part = baseSegments[i]!
if (!part) continue
segment = parseSegment(part, 0, segment)
parseSegment(part, 0, segment)
const kind = segment[0]
if (kind === SEGMENT_TYPE_PATHNAME) {
joined += part
Expand Down Expand Up @@ -266,7 +266,7 @@ export function interpolatePath({
let joined = ''
while (cursor < length) {
const start = cursor
segment = parseSegment(path, start, segment)
parseSegment(path, start, segment)
const end = segment[5]
cursor = end + 1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('Optional Path Parameters - Clean Comprehensive Tests', () => {
const segments: Array<PathSegment> = []
while (cursor < path.length) {
const start = cursor
data = parseSegment(path, start, data)
parseSegment(path, start, data)
const end = data[5]
cursor = end + 1
const type = data[0]
Expand Down
2 changes: 1 addition & 1 deletion packages/router-core/tests/optional-path-params.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('Optional Path Parameters', () => {
const segments: Array<PathSegment> = []
while (cursor < path.length) {
const start = cursor
data = parseSegment(path, start, data)
parseSegment(path, start, data)
const end = data[5]
cursor = end + 1
const type = data[0]
Expand Down
2 changes: 1 addition & 1 deletion packages/router-core/tests/path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ describe('parsePathname', () => {
const segments: Array<PathSegment> = []
while (cursor < path.length) {
const start = cursor
data = parseSegment(path, start, data)
parseSegment(path, start, data)
const end = data[5]
cursor = end + 1
const type = data[0]
Expand Down
Loading