Skip to content

Commit

Permalink
fix(): keys with leading number was generating bad object keys (#1127)
Browse files Browse the repository at this point in the history
  • Loading branch information
semoal committed Sep 6, 2021
1 parent c50a06e commit 8bb2983
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
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

1 comment on commit 8bb2983

@vercel
Copy link

@vercel vercel bot commented on 8bb2983 Sep 6, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.