Skip to content

Commit 97d0fde

Browse files
ci: apply automated fixes
1 parent 7d2b299 commit 97d0fde

File tree

2 files changed

+127
-54
lines changed

2 files changed

+127
-54
lines changed

packages/router-core/tests/built.test.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import {
55
parsePathname,
66
processRouteTree,
77
} from '../src'
8-
import { SEGMENT_TYPE_OPTIONAL_PARAM, SEGMENT_TYPE_PARAM, SEGMENT_TYPE_PATHNAME, SEGMENT_TYPE_WILDCARD } from "../src/path"
8+
import {
9+
SEGMENT_TYPE_OPTIONAL_PARAM,
10+
SEGMENT_TYPE_PARAM,
11+
SEGMENT_TYPE_PATHNAME,
12+
SEGMENT_TYPE_WILDCARD,
13+
} from '../src/path'
914

1015
interface TestRoute {
1116
id: string
@@ -170,7 +175,10 @@ describe('work in progress', () => {
170175
let fn = 'const baseSegments = parsePathname(from);'
171176
fn += '\nconst l = baseSegments.length;'
172177

173-
type ParsedRoute = { path: string, segments: ReturnType<typeof parsePathname> }
178+
type ParsedRoute = {
179+
path: string
180+
segments: ReturnType<typeof parsePathname>
181+
}
174182

175183
function recursiveStaticMatch(
176184
parsedRoutes: Array<ParsedRoute>,
@@ -181,19 +189,14 @@ describe('work in progress', () => {
181189
for (const route of parsedRoutes) {
182190
if (resolved.has(route)) continue // already resolved
183191
console.log('\n')
184-
console.log(
185-
'resolving: depth=',
186-
depth,
187-
'parsed=',
188-
route.path,
189-
)
192+
console.log('resolving: depth=', depth, 'parsed=', route.path)
190193
console.log('\u001b[34m' + fn + '\u001b[0m')
191194
const currentSegment = route.segments[depth]
192195
if (!currentSegment) {
193196
throw new Error(
194197
'Implementation error: this should not happen, depth=' +
195-
depth +
196-
`, route=${route.path}`,
198+
depth +
199+
`, route=${route.path}`,
197200
)
198201
}
199202
const candidates = parsedRoutes.filter((r) => {
@@ -227,7 +230,10 @@ describe('work in progress', () => {
227230
rParsed.suffixSegment === currentSegment.suffixSegment
228231
)
229232
})
230-
console.log('candidates:', candidates.map(r => r.path))
233+
console.log(
234+
'candidates:',
235+
candidates.map((r) => r.path),
236+
)
231237
if (candidates.length === 0) {
232238
throw new Error('Implementation error: this should not happen')
233239
}
@@ -321,7 +327,9 @@ describe('work in progress', () => {
321327
const leaf = candidates[0]!
322328

323329
// Check if this route contains a wildcard segment
324-
const wildcardIndex = leaf.segments.findIndex((s) => s && s.type === SEGMENT_TYPE_WILDCARD)
330+
const wildcardIndex = leaf.segments.findIndex(
331+
(s) => s && s.type === SEGMENT_TYPE_WILDCARD,
332+
)
325333

326334
if (wildcardIndex !== -1 && wildcardIndex >= depth) {
327335
// This route has a wildcard at or after the current depth

packages/router-core/tests/built2.test.ts

Lines changed: 107 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import {
55
parsePathname,
66
processRouteTree,
77
} from '../src'
8-
import { SEGMENT_TYPE_OPTIONAL_PARAM, SEGMENT_TYPE_PARAM, SEGMENT_TYPE_PATHNAME, SEGMENT_TYPE_WILDCARD } from "../src/path"
8+
import {
9+
SEGMENT_TYPE_OPTIONAL_PARAM,
10+
SEGMENT_TYPE_PARAM,
11+
SEGMENT_TYPE_PATHNAME,
12+
SEGMENT_TYPE_WILDCARD,
13+
} from '../src/path'
914
import { format } from 'prettier'
1015

1116
interface TestRoute {
@@ -162,13 +167,19 @@ describe('work in progress', () => {
162167
`)
163168
})
164169

165-
const parsedRoutes = result.flatRoutes.map((route): ParsedRoute => ({
166-
path: route.fullPath,
167-
segments: parsePathname(route.fullPath),
168-
rank: route.rank!
169-
}))
170+
const parsedRoutes = result.flatRoutes.map(
171+
(route): ParsedRoute => ({
172+
path: route.fullPath,
173+
segments: parsePathname(route.fullPath),
174+
rank: route.rank!,
175+
}),
176+
)
170177

171-
type ParsedRoute = { path: string, segments: ReturnType<typeof parsePathname>, rank: number }
178+
type ParsedRoute = {
179+
path: string
180+
segments: ReturnType<typeof parsePathname>
181+
rank: number
182+
}
172183

173184
let fn = 'const baseSegments = parsePathname(from).map(s => s.value);'
174185
fn += '\nconst l = baseSegments.length;'
@@ -280,16 +291,22 @@ describe('work in progress', () => {
280291
maxLengths[maxLength] = (maxLengths[maxLength] || 0) + 1
281292
}
282293
})
283-
const allMinLengths = Object.keys(minLengths).sort((a, b) => Number(a) - Number(b))
294+
const allMinLengths = Object.keys(minLengths).sort(
295+
(a, b) => Number(a) - Number(b),
296+
)
284297
for (let i = 0; i < allMinLengths.length; i++) {
285298
for (let j = i + 1; j < allMinLengths.length; j++) {
286-
minLengths[Number(allMinLengths[i]!)]! += minLengths[Number(allMinLengths[j]!)]!
299+
minLengths[Number(allMinLengths[i]!)]! +=
300+
minLengths[Number(allMinLengths[j]!)]!
287301
}
288302
}
289-
const allMaxLengths = Object.keys(maxLengths).sort((a, b) => Number(b) - Number(a))
303+
const allMaxLengths = Object.keys(maxLengths).sort(
304+
(a, b) => Number(b) - Number(a),
305+
)
290306
for (let i = 0; i < allMaxLengths.length; i++) {
291307
for (let j = i + 1; j < allMaxLengths.length; j++) {
292-
maxLengths[Number(allMaxLengths[i]!)]! += maxLengths[Number(allMaxLengths[j]!)]!
308+
maxLengths[Number(allMaxLengths[i]!)]! +=
309+
maxLengths[Number(allMaxLengths[j]!)]!
293310
}
294311
}
295312
let bestMinLength
@@ -313,21 +330,30 @@ describe('work in progress', () => {
313330
// console.log(`Best maxLength: ${bestMaxLength} with score: ${maxLengths[bestMaxLength!]} / ${total}`)
314331

315332
// determine which of the 3 discriminants to use (condition, minLength, maxLength) to match as close to 50% of the routes as possible
316-
const discriminant = bestKey && (!bestMinLength || conditionCounts[bestKey] > minLengths[bestMinLength!]) && (!bestMaxLength || conditionCounts[bestKey] > maxLengths[bestMaxLength!])
317-
? { key: bestKey, type: 'condition', } as const
318-
: bestMinLength && (!bestMaxLength || minLengths[bestMinLength!] > maxLengths[bestMaxLength!]) && (!bestKey || minLengths[bestMinLength!] > conditionCounts[bestKey])
319-
? { key: bestMinLength!, type: 'minLength' } as const
320-
: bestMaxLength
321-
? { key: bestMaxLength!, type: 'maxLength' } as const
322-
: undefined
333+
const discriminant =
334+
bestKey &&
335+
(!bestMinLength ||
336+
conditionCounts[bestKey] > minLengths[bestMinLength!]) &&
337+
(!bestMaxLength || conditionCounts[bestKey] > maxLengths[bestMaxLength!])
338+
? ({ key: bestKey, type: 'condition' } as const)
339+
: bestMinLength &&
340+
(!bestMaxLength ||
341+
minLengths[bestMinLength!] > maxLengths[bestMaxLength!]) &&
342+
(!bestKey || minLengths[bestMinLength!] > conditionCounts[bestKey])
343+
? ({ key: bestMinLength!, type: 'minLength' } as const)
344+
: bestMaxLength
345+
? ({ key: bestMaxLength!, type: 'maxLength' } as const)
346+
: undefined
323347

324348
if (discriminant) {
325349
// split all routes into 2 groups (matching and not matching) based on the discriminant
326350
const matchingRoutes: Array<WithConditions> = []
327351
const nonMatchingRoutes: Array<WithConditions> = []
328352
for (const route of parsedRoutes) {
329353
if (discriminant.type === 'condition') {
330-
const condition = route.conditions.find(c => c.key === discriminant.key)
354+
const condition = route.conditions.find(
355+
(c) => c.key === discriminant.key,
356+
)
331357
if (condition) {
332358
matchingRoutes.push(route)
333359
} else {
@@ -352,7 +378,9 @@ describe('work in progress', () => {
352378
} else if (matchingRoutes.length) {
353379
// add `if` for the discriminant
354380
if (discriminant.type === 'condition') {
355-
const condition = matchingRoutes[0]!.conditions.find(c => c.key === discriminant.key)!
381+
const condition = matchingRoutes[0]!.conditions.find(
382+
(c) => c.key === discriminant.key,
383+
)!
356384
fn += `if (${conditionToString(condition) || 'true'}) {`
357385
} else if (discriminant.type === 'minLength') {
358386
if (discriminant.key === length.max) {
@@ -367,25 +395,30 @@ describe('work in progress', () => {
367395
fn += `if (l <= ${discriminant.key}) {`
368396
}
369397
} else {
370-
throw new Error(`Unknown discriminant type: ${JSON.stringify(discriminant)}`)
398+
throw new Error(
399+
`Unknown discriminant type: ${JSON.stringify(discriminant)}`,
400+
)
371401
}
372402
// recurse
373403
recursiveStaticMatch(
374404
matchingRoutes,
375-
{ min: discriminant.type === 'minLength' ? discriminant.key : length.min, max: discriminant.type === 'maxLength' ? discriminant.key : length.max },
376-
discriminant.type === 'condition' ? [...preconditions, discriminant.key] : preconditions
405+
{
406+
min:
407+
discriminant.type === 'minLength' ? discriminant.key : length.min,
408+
max:
409+
discriminant.type === 'maxLength' ? discriminant.key : length.max,
410+
},
411+
discriminant.type === 'condition'
412+
? [...preconditions, discriminant.key]
413+
: preconditions,
377414
)
378415
fn += '}'
379416
}
380417
if (nonMatchingRoutes.length === 1) {
381418
outputRoute(nonMatchingRoutes[0]!, length, preconditions)
382419
} else if (nonMatchingRoutes.length) {
383420
// recurse
384-
recursiveStaticMatch(
385-
nonMatchingRoutes,
386-
length,
387-
preconditions,
388-
)
421+
recursiveStaticMatch(nonMatchingRoutes, length, preconditions)
389422
}
390423
} else {
391424
for (const route of parsedRoutes) {
@@ -394,10 +427,14 @@ describe('work in progress', () => {
394427
}
395428
}
396429

397-
function prepareOptionalParams(parsedRoutes: Array<ParsedRoute>): Array<ParsedRoute> {
430+
function prepareOptionalParams(
431+
parsedRoutes: Array<ParsedRoute>,
432+
): Array<ParsedRoute> {
398433
const result: Array<ParsedRoute> = []
399434
for (const route of parsedRoutes) {
400-
const index = route.segments.findIndex((s) => s.type === SEGMENT_TYPE_OPTIONAL_PARAM)
435+
const index = route.segments.findIndex(
436+
(s) => s.type === SEGMENT_TYPE_OPTIONAL_PARAM,
437+
)
401438
if (index === -1) {
402439
result.push(route)
403440
continue
@@ -412,7 +449,9 @@ describe('work in progress', () => {
412449
}
413450
const withRegular: ParsedRoute = {
414451
...route,
415-
segments: route.segments.map((s, i) => i === index ? { ...s, type: SEGMENT_TYPE_PARAM } : s),
452+
segments: route.segments.map((s, i) =>
453+
i === index ? { ...s, type: SEGMENT_TYPE_PARAM } : s,
454+
),
416455
}
417456
const chunk = prepareOptionalParams([withRegular, withoutOptional])
418457
result.push(...chunk)
@@ -421,12 +460,14 @@ describe('work in progress', () => {
421460
}
422461

423462
type Condition =
424-
| { key: string, kind: 'static'; index: number; value: string }
425-
| { key: string, kind: 'startsWith'; index: number; value: string }
426-
| { key: string, kind: 'endsWith'; index: number; value: string }
427-
| { key: string, kind: 'wildcardEndsWith'; value: string }
463+
| { key: string; kind: 'static'; index: number; value: string }
464+
| { key: string; kind: 'startsWith'; index: number; value: string }
465+
| { key: string; kind: 'endsWith'; index: number; value: string }
466+
| { key: string; kind: 'wildcardEndsWith'; value: string }
428467

429-
const withConditions: Array<WithConditions> = prepareOptionalParams(parsedRoutes).map(r => {
468+
const withConditions: Array<WithConditions> = prepareOptionalParams(
469+
parsedRoutes,
470+
).map((r) => {
430471
let minLength = 0
431472
let maxLength = 0
432473
const conditions: Array<Condition> = r.segments.flatMap((s, i) => {
@@ -437,17 +478,32 @@ describe('work in progress', () => {
437478
return []
438479
}
439480
return [
440-
{ kind: 'static', index: i, value: s.value, key: `static-${i}-${s.value}` },
481+
{
482+
kind: 'static',
483+
index: i,
484+
value: s.value,
485+
key: `static-${i}-${s.value}`,
486+
},
441487
]
442488
} else if (s.type === SEGMENT_TYPE_PARAM) {
443489
minLength += 1
444490
maxLength += 1
445491
const conds: Array<Condition> = []
446492
if (s.prefixSegment) {
447-
conds.push({ kind: 'startsWith', index: i, value: s.prefixSegment, key: `startsWith-${i}-${s.prefixSegment}` })
493+
conds.push({
494+
kind: 'startsWith',
495+
index: i,
496+
value: s.prefixSegment,
497+
key: `startsWith-${i}-${s.prefixSegment}`,
498+
})
448499
}
449500
if (s.suffixSegment) {
450-
conds.push({ kind: 'endsWith', index: i, value: s.suffixSegment, key: `endsWith-${i}-${s.suffixSegment}` })
501+
conds.push({
502+
kind: 'endsWith',
503+
index: i,
504+
value: s.suffixSegment,
505+
key: `endsWith-${i}-${s.suffixSegment}`,
506+
})
451507
}
452508
return conds
453509
} else if (s.type === SEGMENT_TYPE_WILDCARD) {
@@ -457,10 +513,19 @@ describe('work in progress', () => {
457513
minLength += 1
458514
}
459515
if (s.prefixSegment) {
460-
conds.push({ kind: 'startsWith', index: i, value: s.prefixSegment, key: `startsWith-${i}-${s.prefixSegment}` })
516+
conds.push({
517+
kind: 'startsWith',
518+
index: i,
519+
value: s.prefixSegment,
520+
key: `startsWith-${i}-${s.prefixSegment}`,
521+
})
461522
}
462523
if (s.suffixSegment) {
463-
conds.push({ kind: 'wildcardEndsWith', value: s.suffixSegment, key: `wildcardEndsWith-${s.suffixSegment}` })
524+
conds.push({
525+
kind: 'wildcardEndsWith',
526+
value: s.suffixSegment,
527+
key: `wildcardEndsWith-${s.suffixSegment}`,
528+
})
464529
}
465530
return conds
466531
}
@@ -711,4 +776,4 @@ describe('work in progress', () => {
711776
)
712777
expect(buildMatch).toBe(originalMatch)
713778
})
714-
})
779+
})

0 commit comments

Comments
 (0)