Skip to content

Commit d4efd5b

Browse files
Copilotmrlubos
andcommitted
Add test for already correctly escaped slashes and improve escaping logic
Co-authored-by: mrlubos <[email protected]>
1 parent 7f6b6d3 commit d4efd5b

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

packages/openapi-ts/src/tsc/__tests__/types.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,14 @@ describe('createRegularExpressionLiteral', () => {
6363

6464
expect(result.text).toBe('//');
6565
});
66+
67+
it('should handle patterns with already correctly escaped slashes', () => {
68+
const result = createRegularExpressionLiteral({
69+
text: '^data:image\\/svg\\+xml;base64,[A-Za-z0-9+\\/]+=*$',
70+
});
71+
72+
expect(result.text).toBe(
73+
'/^data:image\\/svg\\+xml;base64,[A-Za-z0-9+\\/]+=*$/',
74+
);
75+
});
6676
});

packages/openapi-ts/src/tsc/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,8 +1084,8 @@ export const createRegularExpressionLiteral = ({
10841084
const patternContent =
10851085
text.startsWith('/') && text.endsWith('/') ? text.slice(1, -1) : text;
10861086

1087-
// Escape forward slashes in the pattern content
1088-
const escapedPattern = patternContent.replace(/\//g, '\\/');
1087+
// Escape forward slashes in the pattern content, but only if they're not already escaped
1088+
const escapedPattern = patternContent.replace(/(?<!\\)\//g, '\\/');
10891089

10901090
// Wrap with forward slashes
10911091
const textWithSlashes = `/${escapedPattern}/`;

0 commit comments

Comments
 (0)