Skip to content

Commit 0612d58

Browse files
simplify
1 parent ea9a657 commit 0612d58

1 file changed

Lines changed: 27 additions & 93 deletions

File tree

‎packages/router-generator/src/utils.ts‎

Lines changed: 27 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -205,100 +205,44 @@ export function determineInitialRoutePath(routePath: string) {
205205
}
206206

207207
/**
208-
* Checks if a segment is escaped (wrapped in brackets) in the original path.
209-
* This is used to determine if special strings like 'index', 'route', etc.
210-
* should be treated literally or with their special meaning.
211-
*
212-
* @param originalSegment - The segment from originalRoutePath (may contain brackets)
213-
* @param escapedSegment - The segment from routePath (brackets removed)
214-
* @returns true if the segment was escaped with brackets
208+
* Checks if a segment is fully escaped (entirely wrapped in brackets with no nested brackets).
209+
* E.g., "[index]" -> true, "[_layout]" -> true, "foo[.]bar" -> false, "index" -> false
215210
*/
216-
export function isSegmentEscaped(
217-
originalSegment: string,
218-
escapedSegment: string,
219-
): boolean {
220-
// If the original segment contains bracket escaping that matches the escaped segment,
221-
// the entire segment was escaped
222-
// E.g., originalSegment = "[index]", escapedSegment = "index" -> true
223-
// E.g., originalSegment = "index", escapedSegment = "index" -> false
224-
// E.g., originalSegment = "foo[.]bar", escapedSegment = "foo.bar" -> partial escape, not fully escaped
225-
226-
// Check if the original segment is exactly the escaped segment wrapped in brackets
227-
if (originalSegment === `[${escapedSegment}]`) {
228-
return true
229-
}
230-
231-
// Check for leading underscore escaping: [_]foo -> _foo
232-
if (
233-
originalSegment.startsWith('[_]') &&
234-
escapedSegment.startsWith('_') &&
235-
originalSegment.slice(3) === escapedSegment.slice(1)
236-
) {
237-
return true
238-
}
239-
240-
// Check for trailing underscore escaping: foo[_] -> foo_
241-
if (
242-
originalSegment.endsWith('[_]') &&
243-
escapedSegment.endsWith('_') &&
244-
originalSegment.slice(0, -3) === escapedSegment.slice(0, -1)
245-
) {
246-
return true
247-
}
248-
249-
return false
211+
function isFullyEscapedSegment(originalSegment: string): boolean {
212+
return (
213+
originalSegment.startsWith('[') &&
214+
originalSegment.endsWith(']') &&
215+
!originalSegment.slice(1, -1).includes('[') &&
216+
!originalSegment.slice(1, -1).includes(']')
217+
)
250218
}
251219

252220
/**
253221
* Checks if the leading underscore in a segment is escaped.
254-
* E.g., "[_]layout" has an escaped leading underscore
255-
* E.g., "[_1nd3x]" has an escaped leading underscore (whole segment escaped, starts with _)
256-
* E.g., "_layout" has a non-escaped leading underscore (pathless layout)
222+
* Returns true if:
223+
* - Segment starts with [_] pattern: "[_]layout" -> "_layout"
224+
* - Segment is fully escaped and content starts with _: "[_1nd3x]" -> "_1nd3x"
257225
*/
258226
export function hasEscapedLeadingUnderscore(originalSegment: string): boolean {
259-
// Check for [_] prefix pattern: [_]layout -> _layout
260-
if (originalSegment.startsWith('[_]')) {
261-
return true
262-
}
263-
264-
// Check for fully escaped segment that starts with underscore: [_1nd3x] -> _1nd3x
265-
// The segment must be wrapped in brackets and the content must start with underscore
266-
if (
267-
originalSegment.startsWith('[_') &&
268-
originalSegment.endsWith(']') &&
269-
!originalSegment.slice(1, -1).includes('[') &&
270-
!originalSegment.slice(1, -1).includes(']')
271-
) {
272-
return true
273-
}
274-
275-
return false
227+
// Pattern: [_]something or [_something]
228+
return (
229+
originalSegment.startsWith('[_]') ||
230+
(originalSegment.startsWith('[_') && isFullyEscapedSegment(originalSegment))
231+
)
276232
}
277233

278234
/**
279235
* Checks if the trailing underscore in a segment is escaped.
280-
* E.g., "blog[_]" has an escaped trailing underscore
281-
* E.g., "[_r0ut3_]" has an escaped trailing underscore (whole segment escaped, ends with _)
282-
* E.g., "blog_" has a non-escaped trailing underscore (escape from parent layout)
236+
* Returns true if:
237+
* - Segment ends with [_] pattern: "blog[_]" -> "blog_"
238+
* - Segment is fully escaped and content ends with _: "[_r0ut3_]" -> "_r0ut3_"
283239
*/
284240
export function hasEscapedTrailingUnderscore(originalSegment: string): boolean {
285-
// Check for [_] suffix pattern: blog[_] -> blog_
286-
if (originalSegment.endsWith('[_]')) {
287-
return true
288-
}
289-
290-
// Check for fully escaped segment that ends with underscore: [_r0ut3_] -> _r0ut3_
291-
// The segment must be wrapped in brackets and the content must end with underscore
292-
if (
293-
originalSegment.startsWith('[') &&
294-
originalSegment.endsWith('_]') &&
295-
!originalSegment.slice(1, -1).includes('[') &&
296-
!originalSegment.slice(1, -1).includes(']')
297-
) {
298-
return true
299-
}
300-
301-
return false
241+
// Pattern: something[_] or [something_]
242+
return (
243+
originalSegment.endsWith('[_]') ||
244+
(originalSegment.endsWith('_]') && isFullyEscapedSegment(originalSegment))
245+
)
302246
}
303247

304248
const backslashRegex = /\\/g
@@ -432,20 +376,10 @@ export function removeLayoutSegmentsWithEscape(
432376
const routeSegments = routePath.split('/')
433377
const originalSegments = originalPath.split('/')
434378

379+
// Keep segments that are NOT pathless (i.e., don't start with unescaped underscore)
435380
const newSegments = routeSegments.filter((segment, i) => {
436381
const originalSegment = originalSegments[i] || ''
437-
438-
// If the segment starts with underscore but the underscore is escaped,
439-
// keep the segment (it's not a layout segment)
440-
if (
441-
segment.startsWith('_') &&
442-
hasEscapedLeadingUnderscore(originalSegment)
443-
) {
444-
return true
445-
}
446-
447-
// Otherwise, filter out segments starting with underscore (layout segments)
448-
return !segment.startsWith('_')
382+
return !isSegmentPathless(segment, originalSegment)
449383
})
450384

451385
return newSegments.join('/')

0 commit comments

Comments
 (0)