Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delete unused import #113

Merged
merged 2 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions src/renderers/contentful/renderContentfulImports.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
export default function renderContentfulImports(localization: boolean = false): string {
if (localization) {
return `
// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT.

import { Entry } from 'contentful'
import { Document } from '@contentful/rich-text-types'
`
}

export default function renderContentfulImports(
localization: boolean = false,
hasRichText: boolean = true,
): string {
return `
// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT.

import { Asset, Entry } from 'contentful'
import { Document } from '@contentful/rich-text-types'
import { ${localization ? "" : "Asset, "}Entry } from 'contentful'
${hasRichText ? "import { Document } from '@contentful/rich-text-types'" : ""}
`
}
16 changes: 11 additions & 5 deletions src/renderers/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { ContentType, Locale } from "contentful"

import { format, resolveConfig } from "prettier"

import renderAllLocales from "./contentful/renderAllLocales"
import renderContentfulImports from "./contentful/renderContentfulImports"
import renderContentType from "./contentful/renderContentType"
import renderUnion from "./typescript/renderUnion"
import renderAllLocales from "./contentful/renderAllLocales"
import renderContentTypeId from "./contentful/renderContentTypeId"
import renderDefaultLocale from "./contentful/renderDefaultLocale"
import renderNamespace from "./contentful/renderNamespace"
import renderLocalizedTypes from "./contentful/renderLocalizedTypes"
import renderContentTypeId from "./contentful/renderContentTypeId"
import renderNamespace from "./contentful/renderNamespace"
import renderUnion from "./typescript/renderUnion"

interface Options {
localization?: boolean
Expand All @@ -34,7 +34,7 @@ export default async function render(
].join("\n\n")

const source = [
renderContentfulImports(localization),
renderContentfulImports(localization, hasRichText(contentTypes)),
renderNamespace(typingsSource, namespace),
].join("\n\n")

Expand All @@ -59,3 +59,9 @@ function renderEntryType(contentTypes: ContentType[]) {
contentTypes.map(contentType => renderContentTypeId(contentType.sys.id)),
)
}

function hasRichText(contentTypes: ContentType[]): boolean {
return contentTypes.some(sortedContentType =>
sortedContentType.fields.some(f => !f.omitted && f.type === "RichText"),
)
}
8 changes: 8 additions & 0 deletions test/renderers/contentful/renderContentfulImports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,12 @@ describe("renderContentfulImports()", () => {
import { Document } from \\"@contentful/rich-text-types\\";"
`)
})

it("renders the top of the codegen file without import 'Document' statement", () => {
expect(format(renderContentfulImports(true, false))).toMatchInlineSnapshot(`
"// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT.

import { Entry } from \\"contentful\\";"
`)
})
})
70 changes: 67 additions & 3 deletions test/renderers/render.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ describe("render()", () => {
"// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT.

import { Asset, Entry } from \\"contentful\\"
import { Document } from \\"@contentful/rich-text-types\\"

export interface IMyContentTypeFields {
/** Array field */
Expand Down Expand Up @@ -91,6 +90,73 @@ describe("render()", () => {
`)
})

it("renders a given content type (with RichText)", async () => {
const contentTypes: ContentType[] = [
{
sys: {
id: "myContentType",
} as Sys,
fields: [
{
id: "richTextField",
name: "richText field",
required: true,
validations: [{}],
items: {
type: "Symbol",
validations: [],
},
disabled: false,
omitted: false,
localized: false,
type: "RichText",
},
],
description: "",
displayField: "",
name: "",
toPlainObject: () => ({} as ContentType),
},
]
expect(await render(contentTypes, locales)).toMatchInlineSnapshot(`
"// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT.

import { Asset, Entry } from \\"contentful\\"
import { Document } from \\"@contentful/rich-text-types\\"

export interface IMyContentTypeFields {
/** richText field */
richTextField: Document
}

export interface IMyContentType extends Entry<IMyContentTypeFields> {
sys: {
id: string
type: string
createdAt: string
updatedAt: string
locale: string
contentType: {
sys: {
id: \\"myContentType\\"
linkType: \\"ContentType\\"
type: \\"Link\\"
}
}
}
}

export type CONTENT_TYPE = \\"myContentType\\"

export type IEntry = IMyContentType

export type LOCALE_CODE = \\"en-US\\" | \\"pt-BR\\"

export type CONTENTFUL_DEFAULT_LOCALE_CODE = \\"en-US\\"
"
`)
})

it("renders a given localized content type", async () => {
const contentTypes: ContentType[] = [
{
Expand Down Expand Up @@ -128,7 +194,6 @@ describe("render()", () => {
"// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT.

import { Entry } from \\"contentful\\"
import { Document } from \\"@contentful/rich-text-types\\"

export interface IMyContentTypeFields {
/** Array field */
Expand Down Expand Up @@ -192,7 +257,6 @@ describe("render()", () => {
"// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT.

import { Asset, Entry } from \\"contentful\\"
import { Document } from \\"@contentful/rich-text-types\\"

declare namespace Codegen {
export interface IMyContentTypeFields {
Expand Down