Skip to content

Commit 71fdbf3

Browse files
committed
fix: escape backslahes in string literals
Fix #2640
1 parent 11f8268 commit 71fdbf3

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

packages/router/src/unplugin/codegen/generateRouteRecords.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,18 @@ describe('generateRouteRecord', () => {
365365
})
366366
})
367367

368+
it('preserves backslashes in path overrides', () => {
369+
const tree = new PrefixTree(DEFAULT_OPTIONS)
370+
tree.insert('test/[id]', 'test/[id].vue')
371+
const node = tree.insert('test/[id]/index', 'test/[id]/index.vue')
372+
node.setCustomRouteBlock('test/[id]/index', {
373+
path: '/:id(\\d+)',
374+
})
375+
376+
const result = generateRouteRecordSimple(tree)
377+
expect(result).toContain("path: '/:id(\\\\d+)'")
378+
})
379+
368380
describe('raw paths insertions', () => {
369381
it('works with raw paths', () => {
370382
const tree = new PrefixTree(DEFAULT_OPTIONS)

packages/router/src/unplugin/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,5 @@ export function formatMultilineUnion(items: string[], spaces: number): string {
5555
* toStringLiteral("it's fine") // returns "'it\'s fine'"
5656
*/
5757
export function toStringLiteral(str: string): string {
58-
return `'${str.replace(/'/g, "\\'")}'`
58+
return `'${str.replace(/\\/g, '\\\\').replace(/'/g, "\\'")}'`
5959
}

0 commit comments

Comments
 (0)