Skip to content

Commit

Permalink
Merge pull request #1328 from thekip/fix/bump-messageformat-parser
Browse files Browse the repository at this point in the history
refactor(core): bump messageformat parser. Add TS types
  • Loading branch information
andrii-bodnar authored Jan 11, 2023
2 parents 261265f + f156c65 commit 5aa65d4
Show file tree
Hide file tree
Showing 26 changed files with 421 additions and 704 deletions.
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

1 comment on commit 5aa65d4

@vercel
Copy link

@vercel vercel bot commented on 5aa65d4 Jan 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

js-lingui – ./

lingui.js.org
js-lingui.vercel.app
js-lingui-lingui.vercel.app
js-lingui-git-main-lingui.vercel.app

Please sign in to comment.