Skip to content

fix(): keys with leading number was generating bad object keys #1127

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

Merged
merged 1 commit into from
Sep 6, 2021
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
4 changes: 2 additions & 2 deletions packages/cli/src/api/__snapshots__/compile.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ exports[`createCompiledCatalog options.pseudoLocale should return catalog with p

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.strict should return message key as a fallback translation 1`] = `/*eslint-disable*/module.exports={messages:{"Hello":"Ahoj","Missing":"Missing"}};`;
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't return message key as a fallback in strict mode 1`] = `/*eslint-disable*/module.exports={messages:{"Hello":"Ahoj","Missing":""}};`;
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:"____"}]]}};`;
1 change: 1 addition & 0 deletions packages/cli/src/api/compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ describe("createCompiledCatalog", () => {
{
Hello: "Ahoj",
Missing: "",
Select: "{id, select, Gen {Genesis} 1John {1 John} other {____}}"
},
{
strict,
Expand Down
5 changes: 4 additions & 1 deletion packages/cli/src/api/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import * as R from "ramda"

import pseudoLocalize from "./pseudoLocalize"


const INVALID_OBJECT_KEY_REGEX = /^(\d+[a-zA-Z]|[a-zA-Z]+\d)(\d|[a-zA-Z])*/
export type CompiledCatalogNamespace = "cjs" | "es" | "ts" | string

type CompiledCatalogType = {
Expand Down Expand Up @@ -159,7 +161,8 @@ function processTokens(tokens) {
const inlineTokens = processTokens(item.tokens)
formatProps.push(
t.objectProperty(
t.identifier(item.key),
// if starts with number must be wrapped with quotes
INVALID_OBJECT_KEY_REGEX.test(item.key) ? t.stringLiteral(item.key) : t.identifier(item.key),
isString(inlineTokens)
? t.stringLiteral(inlineTokens)
: inlineTokens
Expand Down