Skip to content

Commit 750ab4c

Browse files
authored
delete unused import (#113)
* fix(renderContentfulImport): Only import Document when needed * test(renderContentfulImports, renderers): Add tests Co-authored-by: kouhei <[email protected]>
1 parent 2f62228 commit 750ab4c

File tree

4 files changed

+92
-20
lines changed

4 files changed

+92
-20
lines changed
Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
export default function renderContentfulImports(localization: boolean = false): string {
2-
if (localization) {
3-
return `
4-
// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT.
5-
6-
import { Entry } from 'contentful'
7-
import { Document } from '@contentful/rich-text-types'
8-
`
9-
}
10-
1+
export default function renderContentfulImports(
2+
localization: boolean = false,
3+
hasRichText: boolean = true,
4+
): string {
115
return `
126
// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT.
137
14-
import { Asset, Entry } from 'contentful'
15-
import { Document } from '@contentful/rich-text-types'
8+
import { ${localization ? "" : "Asset, "}Entry } from 'contentful'
9+
${hasRichText ? "import { Document } from '@contentful/rich-text-types'" : ""}
1610
`
1711
}

src/renderers/render.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import { ContentType, Locale } from "contentful"
22

33
import { format, resolveConfig } from "prettier"
44

5+
import renderAllLocales from "./contentful/renderAllLocales"
56
import renderContentfulImports from "./contentful/renderContentfulImports"
67
import renderContentType from "./contentful/renderContentType"
7-
import renderUnion from "./typescript/renderUnion"
8-
import renderAllLocales from "./contentful/renderAllLocales"
8+
import renderContentTypeId from "./contentful/renderContentTypeId"
99
import renderDefaultLocale from "./contentful/renderDefaultLocale"
10-
import renderNamespace from "./contentful/renderNamespace"
1110
import renderLocalizedTypes from "./contentful/renderLocalizedTypes"
12-
import renderContentTypeId from "./contentful/renderContentTypeId"
11+
import renderNamespace from "./contentful/renderNamespace"
12+
import renderUnion from "./typescript/renderUnion"
1313

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

3636
const source = [
37-
renderContentfulImports(localization),
37+
renderContentfulImports(localization, hasRichText(contentTypes)),
3838
renderNamespace(typingsSource, namespace),
3939
].join("\n\n")
4040

@@ -59,3 +59,9 @@ function renderEntryType(contentTypes: ContentType[]) {
5959
contentTypes.map(contentType => renderContentTypeId(contentType.sys.id)),
6060
)
6161
}
62+
63+
function hasRichText(contentTypes: ContentType[]): boolean {
64+
return contentTypes.some(sortedContentType =>
65+
sortedContentType.fields.some(f => !f.omitted && f.type === "RichText"),
66+
)
67+
}

test/renderers/contentful/renderContentfulImports.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,12 @@ describe("renderContentfulImports()", () => {
1919
import { Document } from \\"@contentful/rich-text-types\\";"
2020
`)
2121
})
22+
23+
it("renders the top of the codegen file without import 'Document' statement", () => {
24+
expect(format(renderContentfulImports(true, false))).toMatchInlineSnapshot(`
25+
"// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT.
26+
27+
import { Entry } from \\"contentful\\";"
28+
`)
29+
})
2230
})

test/renderers/render.test.ts

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ describe("render()", () => {
5656
"// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT.
5757
5858
import { Asset, Entry } from \\"contentful\\"
59-
import { Document } from \\"@contentful/rich-text-types\\"
6059
6160
export interface IMyContentTypeFields {
6261
/** Array field */
@@ -91,6 +90,73 @@ describe("render()", () => {
9190
`)
9291
})
9392

93+
it("renders a given content type (with RichText)", async () => {
94+
const contentTypes: ContentType[] = [
95+
{
96+
sys: {
97+
id: "myContentType",
98+
} as Sys,
99+
fields: [
100+
{
101+
id: "richTextField",
102+
name: "richText field",
103+
required: true,
104+
validations: [{}],
105+
items: {
106+
type: "Symbol",
107+
validations: [],
108+
},
109+
disabled: false,
110+
omitted: false,
111+
localized: false,
112+
type: "RichText",
113+
},
114+
],
115+
description: "",
116+
displayField: "",
117+
name: "",
118+
toPlainObject: () => ({} as ContentType),
119+
},
120+
]
121+
expect(await render(contentTypes, locales)).toMatchInlineSnapshot(`
122+
"// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT.
123+
124+
import { Asset, Entry } from \\"contentful\\"
125+
import { Document } from \\"@contentful/rich-text-types\\"
126+
127+
export interface IMyContentTypeFields {
128+
/** richText field */
129+
richTextField: Document
130+
}
131+
132+
export interface IMyContentType extends Entry<IMyContentTypeFields> {
133+
sys: {
134+
id: string
135+
type: string
136+
createdAt: string
137+
updatedAt: string
138+
locale: string
139+
contentType: {
140+
sys: {
141+
id: \\"myContentType\\"
142+
linkType: \\"ContentType\\"
143+
type: \\"Link\\"
144+
}
145+
}
146+
}
147+
}
148+
149+
export type CONTENT_TYPE = \\"myContentType\\"
150+
151+
export type IEntry = IMyContentType
152+
153+
export type LOCALE_CODE = \\"en-US\\" | \\"pt-BR\\"
154+
155+
export type CONTENTFUL_DEFAULT_LOCALE_CODE = \\"en-US\\"
156+
"
157+
`)
158+
})
159+
94160
it("renders a given localized content type", async () => {
95161
const contentTypes: ContentType[] = [
96162
{
@@ -128,7 +194,6 @@ describe("render()", () => {
128194
"// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT.
129195
130196
import { Entry } from \\"contentful\\"
131-
import { Document } from \\"@contentful/rich-text-types\\"
132197
133198
export interface IMyContentTypeFields {
134199
/** Array field */
@@ -192,7 +257,6 @@ describe("render()", () => {
192257
"// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT MODIFY IT.
193258
194259
import { Asset, Entry } from \\"contentful\\"
195-
import { Document } from \\"@contentful/rich-text-types\\"
196260
197261
declare namespace Codegen {
198262
export interface IMyContentTypeFields {

0 commit comments

Comments
 (0)