Skip to content
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
3 changes: 2 additions & 1 deletion src/renderers/contentful/fields/renderSymbol.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Field } from "contentful"

import { renderUnionValues } from "../../typescript/renderUnion"
import { escapeSingleQuotes } from "../../utils"

export default function renderSymbol(field: Field) {
const inValidation = field.validations.find(validation => !!validation.in)

if (inValidation) {
return renderUnionValues(inValidation.in!.map(value => `'${value}'`))
return renderUnionValues(inValidation.in!.map(value => `'${escapeSingleQuotes(value)}'`))
} else {
return "string"
}
Expand Down
5 changes: 4 additions & 1 deletion src/renderers/contentful/renderAllLocales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ import renderUnion from "../typescript/renderUnion"
import { Locale } from "contentful"

export default function renderAllLocales(locales: Locale[]): string {
return renderUnion("LOCALE_CODE", locales.map(locale => `'${locale.code}'`))
return renderUnion(
"LOCALE_CODE",
locales.map(locale => `'${locale.code}'`),
)
}
5 changes: 4 additions & 1 deletion src/renderers/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,8 @@ function renderAllContentTypes(contentTypes: ContentType[], localization: boolea
}

function renderAllContentTypeIds(contentTypes: ContentType[]): string {
return renderUnion("CONTENT_TYPE", contentTypes.map(contentType => `'${contentType.sys.id}'`))
return renderUnion(
"CONTENT_TYPE",
contentTypes.map(contentType => `'${contentType.sys.id}'`),
)
}
3 changes: 3 additions & 0 deletions src/renderers/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function escapeSingleQuotes(str: string = ""): string {
return str.replace(/'/g, "\\'")
}
10 changes: 5 additions & 5 deletions test/renderers/render.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("render()", () => {
type: "Symbol",
validations: [
{
in: ["one", "of", "the", "above"],
in: ["one's", "of", "the", "above"],
},
],
},
Expand Down Expand Up @@ -60,7 +60,7 @@ describe("render()", () => {

export interface IMyContentTypeFields {
/** Array field */
arrayField: (\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[]
arrayField: (\\"one's\\" | \\"of\\" | \\"the\\" | \\"above\\")[]
}

export interface IMyContentType extends Entry<IMyContentTypeFields> {
Expand Down Expand Up @@ -105,7 +105,7 @@ describe("render()", () => {
type: "Symbol",
validations: [
{
in: ["one", "of", "the", "above"],
in: ["one's", "of", "the", "above"],
},
],
},
Expand All @@ -130,7 +130,7 @@ describe("render()", () => {

export interface IMyContentTypeFields {
/** Array field */
arrayField: LocalizedField<(\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[]>
arrayField: LocalizedField<(\\"one's\\" | \\"of\\" | \\"the\\" | \\"above\\")[]>
}

export interface IMyContentType extends Entry<IMyContentTypeFields> {
Expand Down Expand Up @@ -193,7 +193,7 @@ describe("render()", () => {
declare namespace Codegen {
export interface IMyContentTypeFields {
/** Array field */
arrayField: (\\"one\\" | \\"of\\" | \\"the\\" | \\"above\\")[]
arrayField: (\\"one's\\" | \\"of\\" | \\"the\\" | \\"above\\")[]
}

export interface IMyContentType extends Entry<IMyContentTypeFields> {
Expand Down
25 changes: 25 additions & 0 deletions test/renderers/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { escapeSingleQuotes } from "../../src/renderers/utils"

describe("escapeSingleQuotes()", () => {
const testStrings = [
"one's",
"no quotes",
"the quote's pie",
"Tom's pie is better than quote's pie",
"Alot of quotes after '''''",
"''''' Alot of quotes before",
]

const escapedStrings = [
"one\\'s",
"no quotes",
"the quote\\'s pie",
"Tom\\'s pie is better than quote\\'s pie",
"Alot of quotes after \\'\\'\\'\\'\\'",
"\\'\\'\\'\\'\\' Alot of quotes before",
]

it("escapes all the single quotes", () => {
expect(testStrings.map(escapeSingleQuotes)).toStrictEqual(escapedStrings)
})
})