Skip to content

Commit bd3e4a2

Browse files
committed
frontend: advancedSearch: Add escaping for backslashes
This change ensures that backslashes are properly escaped in addition to double quotes. The fix involves modifying the replace logic to handle both backslashes and double quotes by chaining two replace calls: the first to escape backslashes and the second to escape double quotes.
1 parent 98c4121 commit bd3e4a2

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

frontend/src/components/advancedSearch/utils/inferTypes.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,19 @@ declare var status: "Available" | "Running";`;
9898
expect(generateGlobalVarDeclarations(objects)).toBe(expected);
9999
});
100100

101+
it('should escape double quotes and backslashes in string literals', () => {
102+
const objects = [
103+
{ value: '"quote"' },
104+
{ value: 'backslash\\' },
105+
{ value: 'both\\"' },
106+
{ value: 'plain' },
107+
];
108+
const expected =
109+
'declare var value: "\\"quote\\"" | "backslash\\\\" | "both\\\\\\"" | "plain";';
110+
111+
expect(generateGlobalVarDeclarations(objects)).toBe(expected);
112+
});
113+
101114
it('should handle keys that are not valid identifiers by skipping them', () => {
102115
const objects = [{ 'not-valid': 1, validKey: 2 }];
103116
const expected = 'declare var validKey: number;';

frontend/src/components/advancedSearch/utils/inferTypes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ function generateTypeString(
138138
// Add quoted literals
139139
node.stringLiterals.forEach(literal => {
140140
// Basic escaping for quotes inside the string literal itself
141-
const escapedLiteral = literal.replace(/"/g, '\\"');
141+
const escapedLiteral = literal.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
142142
types.push(`"${escapedLiteral}"`);
143143
});
144144
} else {

0 commit comments

Comments
 (0)