Skip to content

Commit ba83e1c

Browse files
authored
fix(input_schema): indentation problem in makeInputJsFieldsReadable (#600)
Fixes incorrect indentation applied to multiline JavaScript code inside template literals when using `makeInputJsFieldsReadable`
1 parent a74be3c commit ba83e1c

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

packages/input_schema/src/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ export function makeInputJsFieldsReadable(json: string, jsFields: string[], json
331331
// If it's not a function declaration or multiline JS code then we do nothing.
332332
if (!isSingleFunction && !isMultiline) return;
333333

334-
const spaces = (new Array(isSingleFunction ? jsonSpacing : jsonSpacing * 2)).fill(' ').join('');
334+
const spaces = isSingleFunction ? ' '.repeat(jsonSpacing) : '';
335335
maybeFunction = maybeFunction
336336
.split('\n').join(`\n${spaces}`) // This prefixes each line with spaces.
337337
.trim(); // Trim whitespace on both sides

test/utilities.test.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,24 @@ describe('timeoutPromise()', () => {
288288
},
289289
"rotateUserAgents": false,
290290
"someCode": \`const a = 5;
291-
function sum (a, b) {
292-
return a + b;
293-
}
294-
sum(a, 10);\`
291+
function sum (a, b) {
292+
return a + b;
293+
}
294+
sum(a, 10);\`
295+
}`;
296+
297+
expect(given).toBe(expected);
298+
});
299+
300+
it('should not add extra indentation inside template literals for multiline code', () => {
301+
const json = JSON.stringify({
302+
initScript: "console.log('start');\nconsole.log('end');",
303+
});
304+
305+
const given = makeInputJsFieldsReadable(json, ['initScript'], 4);
306+
const expected = `{
307+
"initScript": \`console.log('start');
308+
console.log('end');\`
295309
}`;
296310

297311
expect(given).toBe(expected);

0 commit comments

Comments
 (0)