Skip to content

Commit 8bb2983

Browse files
authored
fix(): keys with leading number was generating bad object keys (#1127)
1 parent c50a06e commit 8bb2983

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

packages/cli/src/api/__snapshots__/compile.test.ts.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ exports[`createCompiledCatalog options.pseudoLocale should return catalog with p
2424
2525
exports[`createCompiledCatalog options.pseudoLocale should return compiled catalog when pseudoLocale doesn't match current locale 1`] = `/*eslint-disable*/module.exports={messages:{"Hello":"Ahoj"}};`;
2626
27-
exports[`createCompiledCatalog options.strict should return message key as a fallback translation 1`] = `/*eslint-disable*/module.exports={messages:{"Hello":"Ahoj","Missing":"Missing"}};`;
27+
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:"____"}]]}};`;
2828
29-
exports[`createCompiledCatalog options.strict should't return message key as a fallback in strict mode 1`] = `/*eslint-disable*/module.exports={messages:{"Hello":"Ahoj","Missing":""}};`;
29+
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:"____"}]]}};`;

packages/cli/src/api/compile.test.ts

+1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ describe("createCompiledCatalog", () => {
113113
{
114114
Hello: "Ahoj",
115115
Missing: "",
116+
Select: "{id, select, Gen {Genesis} 1John {1 John} other {____}}"
116117
},
117118
{
118119
strict,

packages/cli/src/api/compile.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import * as R from "ramda"
55

66
import pseudoLocalize from "./pseudoLocalize"
77

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

1012
type CompiledCatalogType = {
@@ -159,7 +161,8 @@ function processTokens(tokens) {
159161
const inlineTokens = processTokens(item.tokens)
160162
formatProps.push(
161163
t.objectProperty(
162-
t.identifier(item.key),
164+
// if starts with number must be wrapped with quotes
165+
INVALID_OBJECT_KEY_REGEX.test(item.key) ? t.stringLiteral(item.key) : t.identifier(item.key),
163166
isString(inlineTokens)
164167
? t.stringLiteral(inlineTokens)
165168
: inlineTokens

0 commit comments

Comments
 (0)