Skip to content

Commit

Permalink
fix(formatters): fix typing inconsistency and add tsd test for each p…
Browse files Browse the repository at this point in the history
…ackage (#1547)
  • Loading branch information
timofei-iatsenko authored Mar 22, 2023
1 parent 29cad1b commit 7a76e35
Show file tree
Hide file tree
Showing 17 changed files with 191 additions and 59 deletions.
5 changes: 5 additions & 0 deletions packages/format-csv/__typetests__/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { CatalogFormatter } from "@lingui/conf"
import { expectAssignable } from "tsd"
import { formatter } from "@lingui/format-csv"

expectAssignable<CatalogFormatter>(formatter())
9 changes: 9 additions & 0 deletions packages/format-csv/__typetests__/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"target": "ESNext",
"moduleResolution": "Node16",
"skipLibCheck": true,
"esModuleInterop": true
},
"paths": {}
}
1 change: 1 addition & 0 deletions packages/format-csv/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"papaparse": "^5.4.0"
},
"devDependencies": {
"tsd": "^0.28.0",
"unbuild": "^1.1.2"
}
}
8 changes: 8 additions & 0 deletions packages/format-json/__typetests__/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { CatalogFormatter } from "@lingui/conf"
import { expectAssignable } from "tsd"
import { formatter } from "@lingui/format-json"

expectAssignable<CatalogFormatter>(formatter())
expectAssignable<CatalogFormatter>(
formatter({ lineNumbers: true, origins: true })
)
9 changes: 9 additions & 0 deletions packages/format-json/__typetests__/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"target": "ESNext",
"moduleResolution": "Node16",
"skipLibCheck": true,
"esModuleInterop": true
},
"paths": {}
}
1 change: 1 addition & 0 deletions packages/format-json/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"ramda": "^0.28.0"
},
"devDependencies": {
"tsd": "^0.28.0",
"unbuild": "^1.1.2"
}
}
13 changes: 13 additions & 0 deletions packages/format-po-gettext/__typetests__/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { CatalogFormatter } from "@lingui/conf"
import { expectAssignable } from "tsd"
import { formatter } from "@lingui/format-po-gettext"

expectAssignable<CatalogFormatter>(formatter())
expectAssignable<CatalogFormatter>(
formatter({
lineNumbers: true,
origins: true,
printLinguiId: true,
disableSelectWarning: true,
})
)
9 changes: 9 additions & 0 deletions packages/format-po-gettext/__typetests__/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"target": "ESNext",
"moduleResolution": "Node16",
"skipLibCheck": true,
"esModuleInterop": true
},
"paths": {}
}
1 change: 1 addition & 0 deletions packages/format-po-gettext/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
},
"devDependencies": {
"@lingui/jest-mocks": "workspace:^",
"tsd": "^0.28.0",
"unbuild": "^1.1.2"
}
}
40 changes: 26 additions & 14 deletions packages/format-po-gettext/src/po-gettext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,24 @@ import { mockConsole } from "@lingui/jest-mocks"
import fs from "fs"
import path from "path"

import { CatalogType } from "@lingui/conf"
import { CatalogFormatter, CatalogType } from "@lingui/conf"
import { formatter as createFormat } from "./po-gettext"

const defaultParseCtx: Parameters<CatalogFormatter["parse"]>[1] = {
locale: "en",
sourceLocale: "en",
filename: "file.po",
}

const defaultSerializeCtx: Parameters<CatalogFormatter["serialize"]>[1] = {
locale: "en",
existing: null,
filename: "file.po",
sourceLocale: "en",
}

describe("po-gettext format", () => {
let format = createFormat()
const format = createFormat()

afterEach(() => {
jest.useRealTimers()
Expand Down Expand Up @@ -50,10 +63,7 @@ describe("po-gettext format", () => {
},
}

const pofile = format.serialize(catalog, {
locale: "en",
existing: null,
})
const pofile = format.serialize(catalog, defaultSerializeCtx)

expect(pofile).toMatchSnapshot()
})
Expand All @@ -63,7 +73,8 @@ describe("po-gettext format", () => {
.readFileSync(path.join(__dirname, "fixtures/messages_plural.po"))
.toString()

const catalog = format.parse(pofile)
const catalog = format.parse(pofile, defaultParseCtx)

expect(catalog).toMatchSnapshot()
})

Expand Down Expand Up @@ -94,7 +105,7 @@ describe("po-gettext format", () => {
}

mockConsole((console) => {
format.serialize(catalog, { existing: null, locale: "en" })
format.serialize(catalog, defaultSerializeCtx)

expect(console.warn).toHaveBeenCalledWith(
expect.stringContaining("Nested plurals"),
Expand All @@ -118,7 +129,7 @@ msgstr[1] "# dny"
msgstr[2] "# dní"
`

const parsed = format.parse(po)
const parsed = format.parse(po, defaultParseCtx)

expect(parsed).toEqual({
Y8Xw2Y: {
Expand All @@ -145,7 +156,7 @@ msgstr[2] "# dní"

it("should warn", () => {
mockConsole((console) => {
format.serialize(catalog, { locale: "en", existing: null })
format.serialize(catalog, defaultSerializeCtx)

expect(console.warn).toHaveBeenCalledWith(
expect.stringContaining("select"),
Expand All @@ -158,7 +169,7 @@ msgstr[2] "# dní"
const format = createFormat({ disableSelectWarning: true })

mockConsole((console) => {
format.serialize(catalog, { locale: "en", existing: null })
format.serialize(catalog, defaultSerializeCtx)

expect(console.warn).not.toHaveBeenCalled()
})
Expand All @@ -175,7 +186,7 @@ msgstr[2] "# dní"

it("should warn", () => {
mockConsole((console) => {
format.serialize(catalog, { locale: "en", existing: null })
format.serialize(catalog, defaultSerializeCtx)

expect(console.warn).toHaveBeenCalledWith(
expect.stringContaining("selectOrdinal"),
Expand All @@ -188,7 +199,7 @@ msgstr[2] "# dní"
const format = createFormat({ disableSelectWarning: true })

mockConsole((console) => {
format.serialize(catalog, { locale: "en", existing: null })
format.serialize(catalog, defaultSerializeCtx)

expect(console.warn).not.toHaveBeenCalled()
})
Expand All @@ -202,7 +213,8 @@ msgstr[2] "# dní"
)
.toString()

const catalog = format.parse(pofile)
const catalog = format.parse(pofile, defaultParseCtx)

expect(catalog).toMatchSnapshot()
})
})
17 changes: 8 additions & 9 deletions packages/format-po-gettext/src/po-gettext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,9 @@ const convertPluralsToICU = (
item.msgstr = ["{" + pluralizeOn + ", plural, " + pluralClauses + "}"]
}

export function formatter(options: PoGettextFormatterOptions = {}) {
export function formatter(
options: PoGettextFormatterOptions = {}
): CatalogFormatter {
options = {
origins: true,
lineNumbers: true,
Expand All @@ -251,7 +253,7 @@ export function formatter(options: PoGettextFormatterOptions = {}) {
catalogExtension: ".po",
templateExtension: ".pot",

parse(content): CatalogType {
parse(content, ctx): CatalogType {
const po = PO.parse(content)

// .po plurals are numbered 0-N and need to be mapped to ICU plural classes ("one", "few", "many"...). Different
Expand All @@ -263,14 +265,11 @@ export function formatter(options: PoGettextFormatterOptions = {}) {
convertPluralsToICU(item, pluralForms, po.headers.Language)
})

return formatter.parse(po.toString())
return formatter.parse(po.toString(), ctx) as CatalogType
},

serialize(
catalog: CatalogType,
ctx: { locale: string; existing: string }
): string {
const po = PO.parse(formatter.serialize(catalog, ctx))
serialize(catalog, ctx): string {
const po = PO.parse(formatter.serialize(catalog, ctx) as string)

po.items = po.items.map((item) => {
const isGeneratedId = !item.flags["explicit-id"]
Expand All @@ -283,5 +282,5 @@ export function formatter(options: PoGettextFormatterOptions = {}) {

return po.toString()
},
} satisfies CatalogFormatter
}
}
12 changes: 12 additions & 0 deletions packages/format-po/__typetests__/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { CatalogFormatter } from "@lingui/conf"
import { expectAssignable } from "tsd"
import { formatter } from "@lingui/format-po"

expectAssignable<CatalogFormatter>(formatter())
expectAssignable<CatalogFormatter>(
formatter({
lineNumbers: true,
origins: true,
printLinguiId: true,
})
)
9 changes: 9 additions & 0 deletions packages/format-po/__typetests__/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"target": "ESNext",
"moduleResolution": "Node16",
"skipLibCheck": true,
"esModuleInterop": true
},
"paths": {}
}
1 change: 1 addition & 0 deletions packages/format-po/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
},
"devDependencies": {
"@lingui/jest-mocks": "workspace:^",
"tsd": "^0.28.0",
"unbuild": "^1.1.2"
}
}
53 changes: 24 additions & 29 deletions packages/format-po/src/po.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,20 @@ import fs from "fs"
import path from "path"

import { formatter as createFormatter } from "./po"
import { CatalogType } from "@lingui/conf"
import { CatalogFormatter, CatalogType } from "@lingui/conf"

const defaultParseCtx: Parameters<CatalogFormatter["parse"]>[1] = {
locale: "en",
sourceLocale: "en",
filename: "file.po",
}

const defaultSerializeCtx: Parameters<CatalogFormatter["serialize"]>[1] = {
locale: "en",
existing: null,
filename: "file.po",
sourceLocale: "en",
}

describe("pofile format", () => {
jest.useFakeTimers().setSystemTime(new Date("2018-08-27T10:00Z").getTime())
Expand Down Expand Up @@ -65,10 +78,7 @@ describe("pofile format", () => {
},
}

const pofile = format.serialize(catalog, {
locale: "en",
existing: null,
})
const pofile = format.serialize(catalog, defaultSerializeCtx)
expect(pofile).toMatchSnapshot()
})

Expand All @@ -79,7 +89,7 @@ describe("pofile format", () => {
.readFileSync(path.join(__dirname, "fixtures/messages.po"))
.toString()

const actual = format.parse(pofile)
const actual = format.parse(pofile, defaultParseCtx)
expect(actual).toMatchSnapshot()
})

Expand All @@ -95,12 +105,9 @@ describe("pofile format", () => {
},
}

const serialized = format.serialize(catalog, {
locale: "en",
existing: null,
})
const serialized = format.serialize(catalog, defaultSerializeCtx) as string

const actual = format.parse(serialized)
const actual = format.parse(serialized, defaultParseCtx)
expect(actual).toMatchObject(catalog)
})

Expand All @@ -116,10 +123,7 @@ describe("pofile format", () => {
},
}

const serialized = format.serialize(catalog, {
locale: "en",
existing: null,
})
const serialized = format.serialize(catalog, defaultSerializeCtx)

expect(serialized).toMatchSnapshot()
})
Expand All @@ -137,10 +141,7 @@ describe("pofile format", () => {
},
}

const serialized = format.serialize(catalog, {
locale: "en",
existing: null,
})
const serialized = format.serialize(catalog, defaultSerializeCtx)

expect(serialized).toMatchSnapshot()
})
Expand All @@ -164,7 +165,7 @@ describe("pofile format", () => {
msgstr "Second description joins translator comments"
`

const actual = format.parse(po)
const actual = format.parse(po, defaultParseCtx)
expect(actual).toMatchSnapshot()
})

Expand All @@ -188,7 +189,7 @@ describe("pofile format", () => {
},
}

const actual = format.serialize(catalog, { locale: "en", existing: null })
const actual = format.serialize(catalog, defaultSerializeCtx)
const pofileOriginPrefix = "#:"
expect(actual).toEqual(expect.not.stringContaining(pofileOriginPrefix))
})
Expand All @@ -212,10 +213,7 @@ describe("pofile format", () => {
],
},
}
const actual = format.serialize(catalog, {
locale: "en",
existing: null,
})
const actual = format.serialize(catalog, defaultSerializeCtx)

expect(actual).toMatchInlineSnapshot(`
msgid ""
Expand Down Expand Up @@ -262,10 +260,7 @@ describe("pofile format", () => {
},
}

const actual = format.serialize(catalog, {
locale: "en",
existing: null,
})
const actual = format.serialize(catalog, defaultSerializeCtx)

expect(actual).toMatchInlineSnapshot(`
msgid ""
Expand Down
Loading

0 comments on commit 7a76e35

Please sign in to comment.