Skip to content

Commit a6cc38d

Browse files
committed
Indentation
1 parent 4a65473 commit a6cc38d

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

packages/compass-collection/src/components/mock-data-generator-modal/script-generation-utils.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('Script Generation', () => {
2323

2424
expect(result.success).to.equal(true);
2525
if (result.success) {
26-
expect(result.script).to.contain('use("testdb")');
26+
expect(result.script).to.contain("use('testdb')");
2727
expect(result.script).to.contain('faker.person.fullName()');
2828
expect(result.script).to.contain('faker.internet.email()');
2929
expect(result.script).to.contain('insertMany');
@@ -180,7 +180,7 @@ describe('Script Generation', () => {
180180

181181
expect(result.success).to.equal(true);
182182
if (result.success) {
183-
expect(result.script).to.contain('use("testdb")');
183+
expect(result.script).to.contain("use('testdb')");
184184
expect(result.script).to.contain('insertMany');
185185
// Should generate empty objects
186186
expect(result.script).to.contain('{}');

packages/compass-collection/src/components/mock-data-generator-modal/script-generation-utils.ts

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -277,37 +277,53 @@ console.log(\`Successfully inserted \${documents.length} documents into ${escape
277277
/**
278278
* Generate JavaScript object code from document structure
279279
*/
280-
function generateDocumentCode(structure: DocumentStructure): string {
280+
function generateDocumentCode(
281+
structure: DocumentStructure,
282+
indent: number = 2
283+
): string {
281284
// For each field in structure:
282285
// - If FieldMapping: generate faker call
283286
// - If DocumentStructure: generate nested object
284287
// - If ArrayStructure: generate array
285288

286-
const parts: string[] = [];
289+
const fieldIndent = ' '.repeat(indent);
290+
const closingBraceIndent = ' '.repeat(indent - 2);
291+
const rootLevelFields: string[] = [];
287292

288293
for (const [fieldName, value] of Object.entries(structure)) {
289294
if ('mongoType' in value) {
290295
// It's a field mapping
291296
const fakerCall = generateFakerCall(value as FieldMapping);
292-
parts.push(` ${fieldName}: ${fakerCall}`);
297+
rootLevelFields.push(`${fieldIndent}${fieldName}: ${fakerCall}`);
293298
} else if ('type' in value && value.type === 'array') {
294299
// It's an array
295-
const arrayCode = generateArrayCode(value as ArrayStructure);
296-
parts.push(` ${fieldName}: ${arrayCode}`);
300+
const arrayCode = generateArrayCode(value as ArrayStructure, indent + 2);
301+
rootLevelFields.push(`${fieldIndent}${fieldName}: ${arrayCode}`);
297302
} else {
298303
// It's a nested object: recursive call
299-
const nestedCode = generateDocumentCode(value as DocumentStructure);
300-
parts.push(` ${fieldName}: ${nestedCode}`);
304+
const nestedCode = generateDocumentCode(
305+
value as DocumentStructure,
306+
indent + 2
307+
);
308+
rootLevelFields.push(`${fieldIndent}${fieldName}: ${nestedCode}`);
301309
}
302310
}
303311

304-
return `{\n${parts.join(',\n')}\n}`;
312+
// Handle empty objects
313+
if (rootLevelFields.length === 0) {
314+
return '{}';
315+
}
316+
317+
return `{\n${rootLevelFields.join(',\n')}\n${closingBraceIndent}}`;
305318
}
306319

307320
/**
308321
* Generate array code
309322
*/
310-
function generateArrayCode(arrayStructure: ArrayStructure): string {
323+
function generateArrayCode(
324+
arrayStructure: ArrayStructure,
325+
indent: number = 2
326+
): string {
311327
const elementType = arrayStructure.elementType;
312328

313329
// Fixed length for now - TODO: make configurable
@@ -319,11 +335,17 @@ function generateArrayCode(arrayStructure: ArrayStructure): string {
319335
return `Array.from({length: ${arrayLength}}, () => ${fakerCall})`;
320336
} else if ('type' in elementType && elementType.type === 'array') {
321337
// Nested array (e.g., matrix[][])
322-
const nestedArrayCode = generateArrayCode(elementType as ArrayStructure);
338+
const nestedArrayCode = generateArrayCode(
339+
elementType as ArrayStructure,
340+
indent
341+
);
323342
return `Array.from({length: ${arrayLength}}, () => ${nestedArrayCode})`;
324343
} else {
325344
// Array of objects
326-
const objectCode = generateDocumentCode(elementType as DocumentStructure);
345+
const objectCode = generateDocumentCode(
346+
elementType as DocumentStructure,
347+
indent
348+
);
327349
return `Array.from({length: ${arrayLength}}, () => ${objectCode})`;
328350
}
329351
}

0 commit comments

Comments
 (0)