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

refactor(core): bump messageformat parser. Add TS types #1328

Merged
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
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@babel/types": "^7.11.5",
"@lingui/babel-plugin-extract-messages": "^3.15.0",
"@lingui/conf": "^3.15.0",
"@lingui/core": "^3.15.0",
"babel-plugin-macros": "^3.0.1",
"bcp-47": "^1.0.7",
"chalk": "^4.1.0",
Expand All @@ -53,7 +54,6 @@
"glob": "^7.1.4",
"inquirer": "^7.3.3",
"make-plural": "^6.2.2",
"messageformat-parser": "^4.1.3",
"micromatch": "4.0.2",
"mkdirp": "^1.0.4",
"node-gettext": "^3.0.0",
Expand Down
36 changes: 11 additions & 25 deletions packages/cli/src/api/__snapshots__/compile.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,39 +1,25 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`compile should report failed message on error 1`] = `
Can't parse message. Please check correct syntax: "{value, plural, one {Book} other {Books"

Messageformat-parser trace: Expected "#", "{", "}", doubled apostrophe, escaped string, or plain char but end of input found.
`;
exports[`createCompiledCatalog nested message 1`] = `/*eslint-disable*/module.exports={messages:JSON.parse("{\\"nested\\":{\\"one\\":\\"Uno\\",\\"two\\":\\"Dos\\",\\"three\\":\\"Tres\\",\\"hello\\":[\\"Hola \\",[\\"name\\"]]}}")};`;

exports[`createCompiledCatalog nested message 1`] = `/*eslint-disable*/module.exports={messages:{"nested":{"one":"Uno","two":"Dos","three":"Tres","hello":["Hola ",["name"]]}}};`;
exports[`createCompiledCatalog options.compilerBabelOptions by default should return catalog without ASCII chars 1`] = `/*eslint-disable*/module.exports={messages:JSON.parse("{\\"Hello\\":\\"Alohà\\"}")};`;

exports[`createCompiledCatalog options.compilerBabelOptions by default should return catalog without ASCII chars 1`] = `/*eslint-disable*/module.exports={messages:{"Hello":"Alohà"}};`;
exports[`createCompiledCatalog options.compilerBabelOptions should return catalog without ASCII chars 1`] = `/*eslint-disable*/module.exports={messages:JSON.parse("{\\"Hello\\":\\"Aloh\\xE0\\"}")};`;

exports[`createCompiledCatalog options.compilerBabelOptions should return catalog without ASCII chars 1`] = `/*eslint-disable*/module.exports={messages:{"Hello":"Aloh\\xE0"}};`;
exports[`createCompiledCatalog options.namespace should compile with es 1`] = `/*eslint-disable*/export const messages=JSON.parse("{}");`;

exports[`createCompiledCatalog options.namespace should compile with es 1`] = `/*eslint-disable*/export const messages={};`;
exports[`createCompiledCatalog options.namespace should compile with global 1`] = `/*eslint-disable*/global.test={messages:JSON.parse("{}")};`;

exports[`createCompiledCatalog options.namespace should compile with global 1`] = `/*eslint-disable*/global.test={messages:{}};`;
exports[`createCompiledCatalog options.namespace should compile with ts 1`] = `/*eslint-disable*/export const messages=JSON.parse("{}");`;

exports[`createCompiledCatalog options.namespace should compile with ts 1`] = `/*eslint-disable*/export const messages={};`;

exports[`createCompiledCatalog options.namespace should compile with window 1`] = `/*eslint-disable*/window.test={messages:{}};`;
exports[`createCompiledCatalog options.namespace should compile with window 1`] = `/*eslint-disable*/window.test={messages:JSON.parse("{}")};`;

exports[`createCompiledCatalog options.namespace should error with invalid value 1`] = `Invalid namespace param: "global"`;

exports[`createCompiledCatalog options.pseudoLocale should return catalog with pseudolocalized messages 1`] = `/*eslint-disable*/module.exports={messages:{"Hello":"ÀĥōĴ"}};`;

exports[`createCompiledCatalog options.pseudoLocale should return compiled catalog when pseudoLocale doesn't match current locale 1`] = `/*eslint-disable*/module.exports={messages:{"Hello":"Ahoj"}};`;

exports[`createCompiledCatalog options.pure should return code catalog 1`] = `/*eslint-disable*/module.exports={messages:{"Hello":"Ahoj"}};`;
exports[`createCompiledCatalog options.pseudoLocale should return catalog with pseudolocalized messages 1`] = `/*eslint-disable*/module.exports={messages:JSON.parse("{\\"Hello\\":\\"ÀĥōĴ\\"}")};`;

exports[`createCompiledCatalog options.pure should return pure catalog 1`] = `
Object {
Hello: Ahoj,
}
`;
exports[`createCompiledCatalog options.pseudoLocale should return compiled catalog when pseudoLocale doesn't match current locale 1`] = `/*eslint-disable*/module.exports={messages:JSON.parse("{\\"Hello\\":\\"Ahoj\\"}")};`;

exports[`createCompiledCatalog options.strict should return message key as a fallback translation 1`] = `/*eslint-disable*/module.exports={messages:{"Hello":"Ahoj","Missing":"Missing","Select":[["id","select",{Gen:"Genesis","1John":"1 John",other:"____"}]]}};`;
exports[`createCompiledCatalog options.strict should return message key as a fallback translation 1`] = `/*eslint-disable*/module.exports={messages:JSON.parse("{\\"Hello\\":\\"Ahoj\\",\\"Missing\\":\\"Missing\\",\\"Select\\":[[\\"id\\",\\"select\\",{\\"Gen\\":\\"Genesis\\",\\"1John\\":\\"1 John\\",\\"other\\":\\"____\\"}]]}")};`;

exports[`createCompiledCatalog options.strict should't return message key as a fallback in strict mode 1`] = `/*eslint-disable*/module.exports={messages:{"Hello":"Ahoj","Missing":"","Select":[["id","select",{Gen:"Genesis","1John":"1 John",other:"____"}]]}};`;
exports[`createCompiledCatalog options.strict should't return message key as a fallback in strict mode 1`] = `/*eslint-disable*/module.exports={messages:JSON.parse("{\\"Hello\\":\\"Ahoj\\",\\"Missing\\":\\"\\",\\"Select\\":[[\\"id\\",\\"select\\",{\\"Gen\\":\\"Genesis\\",\\"1John\\":\\"1 John\\",\\"other\\":\\"____\\"}]]}")};`;
89 changes: 21 additions & 68 deletions packages/cli/src/api/catalog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1023,76 +1023,29 @@ describe("order", function () {
})

describe("writeCompiled", function () {
it("saves ES modules to .mjs files", function () {
const localeDir = copyFixture(fixture("locales", "initial/"))
const catalog = new Catalog(
{
name: "messages",
path: path.join(localeDir, "{locale}", "messages"),
include: [],
exclude: [],
},
mockConfig()
)

const namespace = "es"
const compiledCatalog = createCompiledCatalog("en", {}, { namespace })
const localeDir = copyFixture(fixture("locales", "initial/"))
const catalog = new Catalog(
{
name: "messages",
path: path.join(localeDir, "{locale}", "messages"),
include: [],
exclude: []
},
mockConfig()
)

it.each([
{namespace: "es", extension: /\.mjs$/},
{namespace: "ts", extension: /\.ts$/},
{namespace: undefined, extension: /\.js$/},
{namespace: 'cjs', extension: /\.js$/},
{namespace: 'window.test', extension: /\.js$/},
{namespace: 'global.test', extension: /\.js$/}
])('Should save namespace $namespace in $extension extension', ({namespace, extension}) => {
const compiledCatalog = createCompiledCatalog("en", {}, {namespace})
// Test that the file extension of the compiled catalog is `.mjs`
expect(catalog.writeCompiled("en", compiledCatalog, namespace)).toMatch(
/\.mjs$/
)
})

it("saves TS modules to .ts files", function () {
const localeDir = copyFixture(fixture("locales", "initial/"))
const catalog = new Catalog(
{
name: "messages",
path: path.join(localeDir, "{locale}", "messages"),
include: [],
exclude: [],
},
mockConfig()
)

const namespace = "ts"
const compiledCatalog = createCompiledCatalog("en", {}, { namespace })
expect(catalog.writeCompiled("en", compiledCatalog, namespace)).toMatch(
/\.ts$/
)
})

it("saves anything else than ES modules to .js files", function () {
const localeDir = copyFixture(fixture("locales", "initial/"))
const catalog = new Catalog(
{
name: "messages",
path: path.join(localeDir, "{locale}", "messages"),
include: [],
exclude: [],
},
mockConfig()
)

let compiledCatalog = createCompiledCatalog("en", {}, {})
// Test that the file extension of the compiled catalog is `.js`
expect(catalog.writeCompiled("en", compiledCatalog)).toMatch(/\.js$/)

compiledCatalog = createCompiledCatalog("en", {}, { namespace: "cjs" })
expect(catalog.writeCompiled("en", compiledCatalog)).toMatch(/\.js$/)

compiledCatalog = createCompiledCatalog(
"en",
{},
{ namespace: "window.test" }
)
expect(catalog.writeCompiled("en", compiledCatalog)).toMatch(/\.js$/)

compiledCatalog = createCompiledCatalog(
"en",
{},
{ namespace: "global.test" }
extension
)
expect(catalog.writeCompiled("en", compiledCatalog)).toMatch(/\.js$/)
})
})
Loading