Skip to content

Commit a6c6791

Browse files
authored
Merge pull request #137 from contentstack/fix/DX-1438
fix: UID clash in v3.0 causing TS file generation issues
2 parents 640dd90 + 9f1a285 commit a6c6791

File tree

3 files changed

+25
-32
lines changed

3 files changed

+25
-32
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "contentstack-cli-tsgen",
33
"description": "Generate TypeScript typings from a Stack.",
4-
"version": "3.0.0",
4+
"version": "3.0.1",
55
"author": "Michael Davis",
66
"bugs": "https://github.com/Contentstack-Solutions/contentstack-cli-tsgen/issues",
77
"dependencies": {

src/lib/tsgen/factory.ts

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ export default function (userOptions: TSGenOptions) {
7171
const cachedGlobalFields: GlobalFieldCache = {}
7272
const cachedModularBlocks: ModularBlockCache = {}
7373
const modularBlockInterfaces = new Set<string>()
74+
const uniqueBlockInterfaces = new Set<string>()
75+
let counter = 1
7476

7577
const typeMap: TypeMap = {
7678
text: {func: type_text, track: true, flag: TypeFlags.BuiltinJS},
@@ -178,21 +180,6 @@ export default function (userOptions: TSGenOptions) {
178180
return op_paren(choices.map(v => get_value(v)).join(' | '))
179181
}
180182

181-
function visit_block_names(
182-
field: ContentstackTypes.Field,
183-
except: ContentstackTypes.Block
184-
) {
185-
const uids: string[] = []
186-
187-
field.blocks.forEach(block => {
188-
if (block.uid !== except.uid) {
189-
uids.push(`${block.uid}: undefined;`)
190-
}
191-
})
192-
193-
return uids.join('\n')
194-
}
195-
196183
function visit_field_type(field: ContentstackTypes.Field) {
197184
let type = 'any'
198185

@@ -260,28 +247,34 @@ export default function (userOptions: TSGenOptions) {
260247
}
261248

262249
function type_modular_blocks(field: ContentstackTypes.Field): string {
263-
const blockInterfaceName = name_type(field.uid);
264-
if(!cachedModularBlocks[blockInterfaceName]){
265-
const blockInterfaces = field.blocks.map((block) => {
266-
const fieldType = block.reference_to && cachedGlobalFields[name_type(block.reference_to)]
267-
? name_type(block.reference_to)
268-
: visit_fields(block.schema || []);
269-
270-
const schema = block.reference_to ? `${fieldType};` : `{\n ${fieldType} }`;
271-
return `${block.uid}: ${schema}`;
272-
});
273-
250+
let blockInterfaceName = name_type(field.uid);
251+
252+
const blockInterfaces = field.blocks.map((block) => {
253+
const fieldType = block.reference_to && cachedGlobalFields[name_type(block.reference_to)]
254+
? name_type(block.reference_to)
255+
: visit_fields(block.schema || []);
256+
257+
const schema = block.reference_to ? `${fieldType};` : `{\n ${fieldType} }`;
258+
return `${block.uid}: ${schema}`;
259+
});
260+
const blockInterfacesKey = blockInterfaces.join(';');
261+
262+
if(!uniqueBlockInterfaces.has(blockInterfacesKey)) {
263+
uniqueBlockInterfaces.add(blockInterfacesKey);
264+
// Keep appending a counter until a unique name is found
265+
while (cachedModularBlocks[blockInterfaceName]) {
266+
blockInterfaceName = `${blockInterfaceName}${counter}`;
267+
counter++;
268+
}
274269
const modularInterface = [
275270
`export interface ${blockInterfaceName} {`,
276271
blockInterfaces.join('\n'),
277272
'}',
278273
].join('\n');
279-
280-
// Store or track the generated block interface for later use
281274
modularBlockInterfaces.add(modularInterface);
282275
cachedModularBlocks[blockInterfaceName] = blockInterfaceName;
283276
}
284-
277+
285278
return field.multiple ? `${blockInterfaceName}[]` : blockInterfaceName;
286279
}
287280

0 commit comments

Comments
 (0)