Skip to content

Commit bb1856f

Browse files
committed
fix: edge-cases when using labels with typed lists (#4)
1 parent 6eb0a95 commit bb1856f

File tree

4 files changed

+88
-18
lines changed

4 files changed

+88
-18
lines changed

kitchen-sink.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,18 @@ collections: # A list of collections the CMS should be able to edit
265265
- { label: "Select", name: "select", widget: "select", options: ["a", "b", "c"] }
266266
- { label: "Datetime", name: "datetime", widget: "datetime" }
267267
- { label: "Markdown", name: "markdown", widget: "markdown" }
268-
- label: "Type 3 Object"
268+
- label: "type 3 Object"
269269
name: "type_3_object"
270270
widget: "object"
271271
fields:
272272
- { label: "Date", name: "date", widget: "date" }
273273
- { label: "Image", name: "image", widget: "image" }
274274
- { label: "File", name: "file", widget: "file" }
275+
- label: "type_4_object"
276+
name: "type_4_object"
277+
widget: "object"
278+
fields:
279+
- { label: "Name", name: "name", widget: "string" }
275280
- label: "Code block"
276281
name: "code"
277282
widget: "code"

src/__snapshots__/generate.spec.ts.snap

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ export interface kitchenSink_typed_list_type_3_object {
151151
file: any;
152152
}
153153
154+
export interface kitchenSink_typed_list_type_4_object {
155+
type: \\"type_4_object\\";
156+
name: string;
157+
}
158+
154159
export interface kitchenSink_code {
155160
code: string;
156161
lang: string;
@@ -181,7 +186,7 @@ export interface kitchenSink {
181186
hidden: any;
182187
object: kitchenSink_object;
183188
list: kitchenSink_list[];
184-
typed_list: (kitchenSink_typed_list_type_1_object | kitchenSink_typed_list_type_2_object | kitchenSink_typed_list_type_3_object)[];
189+
typed_list: (kitchenSink_typed_list_type_1_object | kitchenSink_typed_list_type_2_object | kitchenSink_typed_list_type_3_object | kitchenSink_typed_list_type_4_object)[];
185190
code: kitchenSink_code;
186191
code_alt: kitchenSink_code_alt;
187192
snippet: string;
@@ -311,18 +316,19 @@ export interface KitchenSink_List {
311316
object: KitchenSink_List_Object;
312317
}
313318
314-
export interface KitchenSink_TypedList_Type1Object {
315-
string: string;
316-
boolean: boolean;
317-
text: string;
318-
}
319-
320319
export interface KitchenSink_TypedList_Type2Object_NestedObject {
321320
number: string;
322321
}
323322
324323
export type KitchenSink_TypedList_Type2Object_Select_options = \\"a\\" | \\"b\\" | \\"c\\";
325324
325+
export interface KitchenSink_TypedList_Type1Object {
326+
type: \\"type_1_object\\";
327+
string: string;
328+
boolean: boolean;
329+
text: string;
330+
}
331+
326332
export interface KitchenSink_TypedList_Type2Object {
327333
type: \\"type_2_object\\";
328334
number: string;
@@ -332,13 +338,18 @@ export interface KitchenSink_TypedList_Type2Object {
332338
markdown: string;
333339
}
334340
335-
export interface KitchenSink_TypedList_Type3Object {
341+
export interface KitchenSink_TypedList_type3Object {
336342
type: \\"type_3_object\\";
337343
date: string;
338344
image: string;
339345
file: any;
340346
}
341347
348+
export interface KitchenSink_TypedList_type_4_object {
349+
type: \\"type_4_object\\";
350+
name: string;
351+
}
352+
342353
export interface KitchenSink_CodeBlock {
343354
code: string;
344355
lang: string;
@@ -369,7 +380,7 @@ export interface KitchenSink {
369380
hidden: any;
370381
object: KitchenSink_Object;
371382
list: KitchenSink_List[];
372-
typed_list: (KitchenSink_TypedList_Type1Object | KitchenSink_TypedList_Type2Object | KitchenSink_TypedList_Type3Object)[];
383+
typed_list: (KitchenSink_TypedList_Type1Object | KitchenSink_TypedList_Type2Object | KitchenSink_TypedList_type3Object | KitchenSink_TypedList_type_4_object)[];
373384
code: KitchenSink_CodeBlock;
374385
code_alt: KitchenSink_CodeAltBlock;
375386
snippet: string;

src/widget/transform.spec.ts

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ describe("Widget transformation", () => {
317317
[
318318
"__typename",
319319
{
320-
name: "one",
320+
name: "type_one",
321321
required: true,
322322
multiple: false,
323323
type: [
@@ -341,9 +341,9 @@ describe("Widget transformation", () => {
341341
{ prefix: "parent" },
342342
),
343343
).toEqual([
344-
["list: (parent_list_one | parent_list_two)[];"],
344+
["list: (parent_list_type_one | parent_list_two)[];"],
345345
[
346-
`interface parent_list_one { __typename: "one"; key: string; }`,
346+
`interface parent_list_type_one { __typename: "type_one"; key: string; }`,
347347
`interface parent_list_two { __typename: "two"; id: number; }`,
348348
],
349349
]);
@@ -545,6 +545,55 @@ describe("Widget transformation", () => {
545545
]);
546546
});
547547
});
548+
549+
it("should parse typed lists with labels", () => {
550+
expect(
551+
parse(
552+
{
553+
name: "list",
554+
label: "my_list",
555+
required: true,
556+
multiple: true,
557+
type: [
558+
[
559+
"__typename",
560+
{
561+
name: "type_one",
562+
label: "type one",
563+
required: true,
564+
multiple: false,
565+
type: [
566+
{
567+
name: "key",
568+
singularLabel: "my key",
569+
type: "string",
570+
required: true,
571+
multiple: false,
572+
},
573+
],
574+
},
575+
{
576+
name: "two",
577+
label: "type_two",
578+
required: true,
579+
multiple: false,
580+
type: [
581+
{ name: "id", singularLabel: "my_id", type: "number", required: true, multiple: false },
582+
],
583+
},
584+
],
585+
],
586+
},
587+
{ prefix: "parent", label: true },
588+
),
589+
).toEqual([
590+
["list: (parent_my_list_typeOne | parent_my_list_type_two)[];"],
591+
[
592+
`interface parent_my_list_typeOne { __typename: "type_one"; key: string; }`,
593+
`interface parent_my_list_type_two { __typename: "two"; id: number; }`,
594+
],
595+
]);
596+
});
548597
});
549598

550599
describe("Nested list depth", () => {

src/widget/transform.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import type { Widget } from "../types";
22

3+
export const getWidgetName = (widget: Widget, useLabel: boolean): string =>
4+
useLabel
5+
? (widget.singularLabel || widget.label || widget.name).replace(/\s./gi, toCamelCase)
6+
: widget.name;
7+
38
export const toCamelCase = (str: string): string => str.slice(1).toUpperCase();
49

510
export const wrapEnum = (item: number | string): string =>
@@ -70,9 +75,7 @@ export const transformType =
7075
const required = !widget.required ? "?" : "";
7176
const multiple = widget.multiple ? "[]" : "";
7277

73-
const widgetName = label
74-
? (widget.singularLabel || widget.label || widget.name).replace(/\s./gi, toCamelCase)
75-
: widget.name;
78+
const widgetName = getWidgetName(widget, label);
7679

7780
const name = prefix ? `${prefix}_${widgetName}` : widgetName;
7881

@@ -144,15 +147,17 @@ export const transformType =
144147
transformType({ prefix: name, label }),
145148
empty,
146149
);
147-
const typeNames = (objects as Widget[]).map((w) => w.name);
148150

149-
const pattern = new RegExp(`(${typeNames.map((w) => `${name}_${w}`).join("|")}) {`);
151+
const pattern = new RegExp(
152+
`(${(objects as Widget[]).map((w) => `${name}_${getWidgetName(w, label)}`).join("|")}) {`,
153+
);
150154

151155
// sort typed lists last and splice rest
152156
const rest = interfaces
153157
.sort(sortTypes(pattern))
154158
.splice(0, interfaces.length - names.length);
155159

160+
const typeNames = (objects as Widget[]).map((w) => w.name);
156161
const typeValues = zip(typeNames.map(wrapEnum));
157162

158163
return [

0 commit comments

Comments
 (0)