From 5e9c36b0b58e2332982619a3807adc2fff4c7350 Mon Sep 17 00:00:00 2001 From: zernie Date: Wed, 3 Jun 2020 13:37:45 +0300 Subject: [PATCH] fix(renderField): use field.localized --- src/renderers/contentful/renderField.ts | 9 ++++++++- .../contentful/renderLocalizedTypes.ts | 3 ++- .../typescript/renderInterfaceProperty.ts | 7 ++++++- .../contentful/renderContentType.test.ts | 20 +++++++++++++++++-- test/renderers/render.test.ts | 5 +++-- .../renderInterfaceProperty.test.ts | 12 +++++++---- 6 files changed, 45 insertions(+), 11 deletions(-) diff --git a/src/renderers/contentful/renderField.ts b/src/renderers/contentful/renderField.ts index ae8bb1c8..ffc29346 100644 --- a/src/renderers/contentful/renderField.ts +++ b/src/renderers/contentful/renderField.ts @@ -6,5 +6,12 @@ export default function renderField( type: string, localization: boolean = false, ): string { - return renderInterfaceProperty(field.id, type, field.required, localization, field.name) + return renderInterfaceProperty( + field.id, + type, + field.required, + localization, + field.localized, + field.name, + ) } diff --git a/src/renderers/contentful/renderLocalizedTypes.ts b/src/renderers/contentful/renderLocalizedTypes.ts index a2b17066..2141995a 100644 --- a/src/renderers/contentful/renderLocalizedTypes.ts +++ b/src/renderers/contentful/renderLocalizedTypes.ts @@ -3,7 +3,8 @@ export default function renderLocalizedTypes(localization: boolean) { if (!localization) return null return ` - export type LocalizedField = Partial> + export type DefaultLocalizedField = Record + export type LocalizedField = DefaultLocalizedField & Partial> // We have to use our own localized version of Asset because of a bug in contentful https://github.com/contentful/contentful.js/issues/208 export interface Asset { diff --git a/src/renderers/typescript/renderInterfaceProperty.ts b/src/renderers/typescript/renderInterfaceProperty.ts index 2f514049..a33646a5 100644 --- a/src/renderers/typescript/renderInterfaceProperty.ts +++ b/src/renderers/typescript/renderInterfaceProperty.ts @@ -3,6 +3,7 @@ export default function renderInterfaceProperty( type: string, required: boolean, localization: boolean, + localized: boolean, description?: string, ): string { return [ @@ -10,12 +11,16 @@ export default function renderInterfaceProperty( name, required ? "" : "?", ": ", - localization ? `LocalizedField<${type}>` : type, + localization ? renderLocalizedField(localized, type) : type, required ? "" : " | undefined", ";", ].join("") } +function renderLocalizedField(localized: boolean, type: string) { + return localized ? `LocalizedField<${type}>` : `DefaultLocalizedField<${type}>` +} + function descriptionComment(description: string | undefined) { if (description) { return `/** ${description} */\n` diff --git a/test/renderers/contentful/renderContentType.test.ts b/test/renderers/contentful/renderContentType.test.ts index af9df218..c6ebfa37 100644 --- a/test/renderers/contentful/renderContentType.test.ts +++ b/test/renderers/contentful/renderContentType.test.ts @@ -18,6 +18,16 @@ describe("renderContentType()", () => { localized: false, type: "Symbol", }, + { + id: "localizedSymbolField", + name: "Localized Symbol Field™", + required: true, + validations: [], + disabled: false, + omitted: false, + localized: true, + type: "Symbol", + }, { id: "arrayField", name: "Array field", @@ -60,6 +70,9 @@ describe("renderContentType()", () => { /** Symbol Field™ */ symbolField?: string | undefined; + /** Localized Symbol Field™ */ + localizedSymbolField: string; + /** Array field */ arrayField: (\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[]; } @@ -112,10 +125,13 @@ describe("renderContentType()", () => { expect(format(renderContentType(contentType, true))).toMatchInlineSnapshot(` "export interface IMyContentTypeFields { /** Symbol Field™ */ - symbolField?: LocalizedField | undefined; + symbolField?: DefaultLocalizedField | undefined; + + /** Localized Symbol Field™ */ + localizedSymbolField: LocalizedField; /** Array field */ - arrayField: LocalizedField<(\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[]>; + arrayField: DefaultLocalizedField<(\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[]>; } export interface IMyContentType extends Entry { diff --git a/test/renderers/render.test.ts b/test/renderers/render.test.ts index 384ef2d0..d601bab0 100644 --- a/test/renderers/render.test.ts +++ b/test/renderers/render.test.ts @@ -130,7 +130,7 @@ describe("render()", () => { export interface IMyContentTypeFields { /** Array field */ - arrayField: LocalizedField<(\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[]> + arrayField: DefaultLocalizedField<(\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[]> } export interface IMyContentType extends Entry { @@ -156,7 +156,8 @@ describe("render()", () => { export type CONTENTFUL_DEFAULT_LOCALE_CODE = \\"en-US\\" - export type LocalizedField = Partial> + export type DefaultLocalizedField = Record + export type LocalizedField = DefaultLocalizedField & Partial> // We have to use our own localized version of Asset because of a bug in contentful https://github.com/contentful/contentful.js/issues/208 export interface Asset { diff --git a/test/renderers/typescript/renderInterfaceProperty.test.ts b/test/renderers/typescript/renderInterfaceProperty.test.ts index b98b5d3c..6257bd77 100644 --- a/test/renderers/typescript/renderInterfaceProperty.test.ts +++ b/test/renderers/typescript/renderInterfaceProperty.test.ts @@ -2,19 +2,19 @@ import renderInterfaceProperty from "../../../src/renderers/typescript/renderInt describe("renderInterfaceProperty()", () => { it("works with unrequired properties", () => { - expect(renderInterfaceProperty("property", "type", false, false).trim()).toMatchInlineSnapshot( + expect(renderInterfaceProperty("property", "type", false, false, false).trim()).toMatchInlineSnapshot( `"property?: type | undefined;"`, ) }) it("works with required properties", () => { - expect(renderInterfaceProperty("property", "type", true, false).trim()).toMatchInlineSnapshot( + expect(renderInterfaceProperty("property", "type", true, false, false).trim()).toMatchInlineSnapshot( `"property: type;"`, ) }) it("adds descriptions", () => { - expect(renderInterfaceProperty("property", "type", false, false, "Description").trim()) + expect(renderInterfaceProperty("property", "type", false, false, false,"Description").trim()) .toMatchInlineSnapshot(` "/** Description */ property?: type | undefined;" @@ -22,8 +22,12 @@ describe("renderInterfaceProperty()", () => { }) it("supports localized fields", () => { - expect(renderInterfaceProperty("property", "type", false, true).trim()).toMatchInlineSnapshot( + expect(renderInterfaceProperty("property", "type", false, true, true).trim()).toMatchInlineSnapshot( `"property?: LocalizedField | undefined;"`, ) + + expect(renderInterfaceProperty("property", "type", false, true, false).trim()).toMatchInlineSnapshot( + `"property?: DefaultLocalizedField | undefined;"`, + ) }) })