From b9f2f02a750bce2c42efa8e137b49343ccefa6d3 Mon Sep 17 00:00:00 2001 From: Sheraff Date: Wed, 19 Nov 2025 21:02:49 +0100 Subject: [PATCH] refactor(router-core): parseSegment buffer type can be asserted w/o returning it --- .../router-core/src/new-process-route-tree.ts | 25 +++++++++---------- packages/router-core/src/path.ts | 4 +-- .../tests/optional-path-params-clean.test.ts | 2 +- .../tests/optional-path-params.test.ts | 2 +- packages/router-core/tests/path.test.ts | 2 +- 5 files changed, 17 insertions(+), 18 deletions(-) diff --git a/packages/router-core/src/new-process-route-tree.ts b/packages/router-core/src/new-process-route-tree.ts index 7dda425f227..4b2f8767f61 100644 --- a/packages/router-core/src/new-process-route-tree.ts +++ b/packages/router-core/src/new-process-route-tree.ts @@ -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 * ``` @@ -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) @@ -71,7 +71,7 @@ export function parseSegment( output[3] = end output[4] = end output[5] = end - return output as ParsedSegment + return } // $ (wildcard) @@ -83,7 +83,7 @@ export function parseSegment( output[3] = total output[4] = total output[5] = total - return output as ParsedSegment + return } // $paramName @@ -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) @@ -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) @@ -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) @@ -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) @@ -147,13 +147,12 @@ 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. @@ -161,7 +160,7 @@ export function parseSegment( */ function parseSegments( defaultCaseSensitive: boolean, - data: Uint16Array, + segment: Uint16Array, route: TRouteLike, start: number, node: AnySegmentNode, @@ -175,7 +174,7 @@ function parseSegments( 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 const start = cursor const end = segment[5] @@ -340,7 +339,7 @@ function parseSegments( for (const child of route.children) { parseSegments( defaultCaseSensitive, - data, + segment, child as TRouteLike, cursor, node, diff --git a/packages/router-core/src/path.ts b/packages/router-core/src/path.ts index 88b489d8aa2..f1990c2f36a 100644 --- a/packages/router-core/src/path.ts +++ b/packages/router-core/src/path.ts @@ -180,7 +180,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 @@ -265,7 +265,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 diff --git a/packages/router-core/tests/optional-path-params-clean.test.ts b/packages/router-core/tests/optional-path-params-clean.test.ts index d8c1411e96b..26f1497aa28 100644 --- a/packages/router-core/tests/optional-path-params-clean.test.ts +++ b/packages/router-core/tests/optional-path-params-clean.test.ts @@ -28,7 +28,7 @@ describe('Optional Path Parameters - Clean Comprehensive Tests', () => { const segments: Array = [] 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] diff --git a/packages/router-core/tests/optional-path-params.test.ts b/packages/router-core/tests/optional-path-params.test.ts index 79a368eb520..0b10704f0bb 100644 --- a/packages/router-core/tests/optional-path-params.test.ts +++ b/packages/router-core/tests/optional-path-params.test.ts @@ -32,7 +32,7 @@ describe('Optional Path Parameters', () => { const segments: Array = [] 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] diff --git a/packages/router-core/tests/path.test.ts b/packages/router-core/tests/path.test.ts index 6503eb0b755..c0b51720661 100644 --- a/packages/router-core/tests/path.test.ts +++ b/packages/router-core/tests/path.test.ts @@ -831,7 +831,7 @@ describe('parsePathname', () => { const segments: Array = [] 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]