Skip to content

Commit 4beb5b1

Browse files
authored
fix(unplugin): unescape static parts of parsed paths (#2981)
1 parent 97a1685 commit 4beb5b1

4 files changed

Lines changed: 36 additions & 4 deletions

File tree

‎packages/router/src/unplugin/codegen/generateRouteResolver.spec.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,21 @@ describe('generateRouteResolver', () => {
15911591
`)
15921592
})
15931593

1594+
it('unescapes a static path override', () => {
1595+
const tree = new PrefixTree(DEFAULT_OPTIONS)
1596+
const node = tree.insert('foo', 'foo.vue')
1597+
node.value.setOverride('foo', { path: '/foo\\:bar' })
1598+
1599+
expect(
1600+
generateRouteResolver(
1601+
tree,
1602+
DEFAULT_OPTIONS,
1603+
new ImportsMap(),
1604+
new Map()
1605+
)
1606+
).toContain(`new MatcherPatternPathStatic('/foo:bar')`)
1607+
})
1608+
15941609
it.each([
15951610
['users/index', undefined],
15961611
['users/settings', ''],

‎packages/router/src/unplugin/codegen/generateRouteResolver.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,14 @@ function generatePathCode(
354354
${node.endsWithSplat || trailingSlash === 'ignore' ? 'null,' : trailingSlash === 'always' ? 'true,' : '/* trailingSlash */'}
355355
),`
356356
} else {
357+
// unlike `fullPath`, the parts have no escapes, e.g. `\:`
358+
const path =
359+
'/' +
360+
node.matcherPatternPathDynamicParts
361+
.map(part => (Array.isArray(part) ? part.join('') : part))
362+
.join('/')
357363
return `path: new MatcherPatternPathStatic(${toStringLiteral(
358-
trailingSlash === 'always' ? joinPath(fullPath, '/') : fullPath
364+
trailingSlash === 'always' ? joinPath(path, '/') : path
359365
)}${node.options.caseSensitive ? '' : ', true'}),`
360366
}
361367
}

‎packages/router/src/unplugin/core/tree.spec.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,6 +1465,16 @@ describe('Tree', () => {
14651465
)
14661466
})
14671467

1468+
it('unescapes static parts of a parsed path', () => {
1469+
const node = new PrefixTree(RESOLVED_OPTIONS).insertParsedPath(
1470+
'foo\\:bar'
1471+
)
1472+
expect(node.regexp).toBe('/^\\/foo:bar$/')
1473+
expect(node.matcherPatternPathDynamicParts).toEqual(['foo:bar'])
1474+
// legacy routes are parsed at runtime
1475+
expect(node.fullPath).toBe('/foo\\:bar')
1476+
})
1477+
14681478
it('keeps the slash in the group of a leading optional param of a parsed path', () => {
14691479
const node = new PrefixTree(RESOLVED_OPTIONS).insertParsedPath(':id?/foo')
14701480
expect(node.regexp).toBe('/^(?:\\/([^/]+?))?\\/foo$/')

‎packages/router/src/unplugin/core/treeNodeValue.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,9 +445,10 @@ export class TreeNodeValueStatic extends _TreeNodeValueBase {
445445
constructor(
446446
rawSegment: string,
447447
parent: TreeNodeValue | undefined,
448-
pathSegment = rawSegment
448+
pathSegment = rawSegment,
449+
subSegments?: PathSubSegment[]
449450
) {
450-
super(rawSegment, parent, pathSegment)
451+
super(rawSegment, parent, pathSegment, subSegments)
451452
}
452453
}
453454

@@ -706,7 +707,7 @@ export function createTreeNodeValue(
706707
return new TreeNodeValueParam(segment, parent, pathSegment, subSegments)
707708
}
708709

709-
return new TreeNodeValueStatic(segment, parent, pathSegment)
710+
return new TreeNodeValueStatic(segment, parent, pathSegment, subSegments)
710711
}
711712

712713
/**

0 commit comments

Comments
 (0)