feat(SW-706): add Lokalise tooling and codemod

This commit is contained in:
Michael Zetterberg
2025-03-12 07:02:49 +01:00
parent 1c5b116ed8
commit e22fc1f3c8
26 changed files with 1478 additions and 130 deletions

View File

@@ -0,0 +1,134 @@
import { readFileSync } from "node:fs"
import { join } from "node:path"
import { describe, expect, test } from "@jest/globals"
import { IndentationText, Project } from "ts-morph"
import { processSourceFile } from "./lokalise"
describe("Lokalise codemod", () => {
test("serverComponent: intl.formatMessage with only id", () => {
const input = readFileSync(
join(__dirname, "./fixtures/serverComponentOnlyId.tsx"),
{
encoding: "utf-8",
}
)
const expected = readFileSync(
join(__dirname, "./expectations/serverComponentOnlyId.tsx"),
{
encoding: "utf-8",
}
)
const project = new Project({
manipulationSettings: {
indentationText: IndentationText.TwoSpaces,
},
})
const sourceFile = project.createSourceFile(
"./fixtures/serverComponent.tsx",
input
)
processSourceFile(sourceFile)
const result = sourceFile.getFullText()
expect(result).toBe(expected)
})
test("serverComponent: intl.formatMessage with ternary inside", () => {
const input = readFileSync(
join(__dirname, "./fixtures/serverComponentWithTernaryInside.tsx"),
{
encoding: "utf-8",
}
)
const expected = readFileSync(
join(__dirname, "./expectations/serverComponentWithTernaryInside.tsx"),
{
encoding: "utf-8",
}
)
const project = new Project({
manipulationSettings: {
indentationText: IndentationText.TwoSpaces,
},
})
const sourceFile = project.createSourceFile(
"./fixtures/serverComponentWithTernaryInside.tsx",
input
)
processSourceFile(sourceFile)
const result = sourceFile.getFullText()
expect(result).toBe(expected)
})
test("serverComponent: intl.formatMessage with variables", () => {
const input = readFileSync(
join(__dirname, "./fixtures/serverComponentWithVariables.tsx"),
{
encoding: "utf-8",
}
)
const expected = readFileSync(
join(__dirname, "./expectations/serverComponentWithVariables.tsx"),
{
encoding: "utf-8",
}
)
const project = new Project({
manipulationSettings: {
indentationText: IndentationText.TwoSpaces,
},
})
const sourceFile = project.createSourceFile(
"./fixtures/serverComponentWithVariables.tsx",
input
)
processSourceFile(sourceFile)
const result = sourceFile.getFullText()
expect(result).toBe(expected)
})
test("serverComponent: intl.formatMessage with variable for id", () => {
const input = readFileSync(
join(__dirname, "./fixtures/serverComponentVariableId.tsx"),
{
encoding: "utf-8",
}
)
const expected = readFileSync(
join(__dirname, "./expectations/serverComponentVariableId.tsx"),
{
encoding: "utf-8",
}
)
const project = new Project({
manipulationSettings: {
indentationText: IndentationText.TwoSpaces,
},
})
const sourceFile = project.createSourceFile(
"./fixtures/serverComponentVariableId.tsx",
input
)
processSourceFile(sourceFile)
const result = sourceFile.getFullText()
expect(result).toBe(expected)
})
})