@babelize/engine 0.3.5 → 0.3.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/config.d.ts CHANGED
@@ -3,72 +3,28 @@ import type { BabelizeConfig } from './types';
3
3
  declare const configSchema: z.ZodObject<{
4
4
  workspacePath: z.ZodString;
5
5
  sourceLocale: z.ZodDefault<z.ZodString>;
6
- targetLocales: z.ZodDefault<z.ZodArray<z.ZodString, "many">>;
7
- framework: z.ZodOptional<z.ZodEnum<["react", "nextjs", "vue", "static", "unknown", "django", "react-native"]>>;
6
+ targetLocales: z.ZodDefault<z.ZodArray<z.ZodString>>;
7
+ framework: z.ZodOptional<z.ZodEnum<{
8
+ react: "react";
9
+ nextjs: "nextjs";
10
+ vue: "vue";
11
+ static: "static";
12
+ django: "django";
13
+ "react-native": "react-native";
14
+ unknown: "unknown";
15
+ }>>;
8
16
  dryRun: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
9
17
  projectId: z.ZodOptional<z.ZodString>;
10
18
  sourceDir: z.ZodOptional<z.ZodString>;
11
19
  localesDir: z.ZodOptional<z.ZodString>;
12
20
  glossary: z.ZodOptional<z.ZodObject<{
13
- neverTranslate: z.ZodDefault<z.ZodArray<z.ZodString, "many">>;
21
+ neverTranslate: z.ZodDefault<z.ZodArray<z.ZodString>>;
14
22
  alwaysTranslateAs: z.ZodDefault<z.ZodArray<z.ZodObject<{
15
23
  term: z.ZodString;
16
24
  translations: z.ZodRecord<z.ZodString, z.ZodString>;
17
- }, "strip", z.ZodTypeAny, {
18
- translations: Record<string, string>;
19
- term: string;
20
- }, {
21
- translations: Record<string, string>;
22
- term: string;
23
- }>, "many">>;
24
- }, "strip", z.ZodTypeAny, {
25
- neverTranslate: string[];
26
- alwaysTranslateAs: {
27
- translations: Record<string, string>;
28
- term: string;
29
- }[];
30
- }, {
31
- neverTranslate?: string[] | undefined;
32
- alwaysTranslateAs?: {
33
- translations: Record<string, string>;
34
- term: string;
35
- }[] | undefined;
36
- }>>;
25
+ }, z.core.$strip>>>;
26
+ }, z.core.$strip>>;
37
27
  lockfilePath: z.ZodOptional<z.ZodString>;
38
- }, "strip", z.ZodTypeAny, {
39
- workspacePath: string;
40
- sourceLocale: string;
41
- targetLocales: string[];
42
- dryRun: boolean;
43
- framework?: "react" | "nextjs" | "vue" | "static" | "django" | "react-native" | "unknown" | undefined;
44
- projectId?: string | undefined;
45
- sourceDir?: string | undefined;
46
- localesDir?: string | undefined;
47
- glossary?: {
48
- neverTranslate: string[];
49
- alwaysTranslateAs: {
50
- translations: Record<string, string>;
51
- term: string;
52
- }[];
53
- } | undefined;
54
- lockfilePath?: string | undefined;
55
- }, {
56
- workspacePath: string;
57
- framework?: "react" | "nextjs" | "vue" | "static" | "django" | "react-native" | "unknown" | undefined;
58
- sourceLocale?: string | undefined;
59
- targetLocales?: string[] | undefined;
60
- dryRun?: boolean | undefined;
61
- projectId?: string | undefined;
62
- sourceDir?: string | undefined;
63
- localesDir?: string | undefined;
64
- glossary?: {
65
- neverTranslate?: string[] | undefined;
66
- alwaysTranslateAs?: {
67
- translations: Record<string, string>;
68
- term: string;
69
- }[] | undefined;
70
- } | undefined;
71
- lockfilePath?: string | undefined;
72
- }>;
28
+ }, z.core.$strip>;
73
29
  export declare function loadBabelizeConfig(input: unknown): BabelizeConfig;
74
30
  export { configSchema as babelizeConfigSchema };
@@ -238,6 +238,45 @@ function renderMessage(strategy, key, message, options) {
238
238
  return null;
239
239
  return { node: ast_helpers_1.t.jsxExpressionContainer((0, jsx_message_1.createMessageTCall)(key, message)) };
240
240
  }
241
+ const DEPS_HOOKS = /^use(?:Callback|Memo|Effect|LayoutEffect|InsertionEffect)$/;
242
+ /**
243
+ * A component's translator used in a useCallback/useMemo/useEffect callback
244
+ * is one of its dependencies: left out of the array, the callback keeps the
245
+ * first render's t() and shows the old language after a switch (and
246
+ * react-hooks/exhaustive-deps flags it). Only callbacks holding a t() call
247
+ * this run injected are touched.
248
+ */
249
+ function addTranslatorToHookDeps(ast, names, keys) {
250
+ (0, ast_helpers_1.traverse)(ast, {
251
+ CallExpression(path) {
252
+ const callee = path.node.callee;
253
+ const hook = ast_helpers_1.t.isIdentifier(callee)
254
+ ? callee.name
255
+ : ast_helpers_1.t.isMemberExpression(callee) && ast_helpers_1.t.isIdentifier(callee.property) ? callee.property.name : null;
256
+ if (!hook || !DEPS_HOOKS.test(hook))
257
+ return;
258
+ const [fn, deps] = path.node.arguments;
259
+ if (!(ast_helpers_1.t.isArrowFunctionExpression(fn) || ast_helpers_1.t.isFunctionExpression(fn)) || !ast_helpers_1.t.isArrayExpression(deps))
260
+ return;
261
+ const listed = new Set(deps.elements.flatMap((e) => (ast_helpers_1.t.isIdentifier(e) ? [e.name] : [])));
262
+ const params = new Set(fn.params.flatMap((p) => (ast_helpers_1.t.isIdentifier(p) ? [p.name] : [])));
263
+ const used = new Set();
264
+ path.get('arguments.0').traverse({
265
+ CallExpression(inner) {
266
+ const c = inner.node.callee;
267
+ // t("key") and next-intl's t.rich("key") / t.markup("key").
268
+ const name = ast_helpers_1.t.isIdentifier(c) ? c.name : ast_helpers_1.t.isMemberExpression(c) && ast_helpers_1.t.isIdentifier(c.object) ? c.object.name : null;
269
+ const first = inner.node.arguments[0];
270
+ if (name && names.has(name) && ast_helpers_1.t.isStringLiteral(first) && keys.has(first.value))
271
+ used.add(name);
272
+ },
273
+ });
274
+ for (const name of used)
275
+ if (!listed.has(name) && !params.has(name))
276
+ deps.elements.push(ast_helpers_1.t.identifier(name));
277
+ },
278
+ });
279
+ }
241
280
  /**
242
281
  * A translated string literal keeps the whitespace around its text, which
243
282
  * the message itself leaves out: a mail body `Page: ${href}` followed by two
@@ -792,6 +831,8 @@ positions, options = {}) {
792
831
  };
793
832
  let moduleTranslatorUsed = false;
794
833
  let hookTranslatorUsed = false;
834
+ // Translators bound by a component hook (t, or an alias where t is taken).
835
+ const hookTranslatorNames = new Set();
795
836
  /** The t() expression for a replacement, recording which import it needs. */
796
837
  const pickTranslator = (p, source) => {
797
838
  if (source === 'module') {
@@ -799,7 +840,9 @@ positions, options = {}) {
799
840
  return 't';
800
841
  }
801
842
  hookTranslatorUsed = true;
802
- return nameFor(p);
843
+ const name = nameFor(p);
844
+ hookTranslatorNames.add(name);
845
+ return name;
803
846
  };
804
847
  for (const { plan, keys } of moduleRewrites) {
805
848
  plan.literals.forEach((lit, i) => {
@@ -1111,6 +1154,8 @@ positions, options = {}) {
1111
1154
  importAdded = true;
1112
1155
  }
1113
1156
  }
1157
+ if (hookTranslatorNames.size > 0)
1158
+ addTranslatorToHookDeps(ast, hookTranslatorNames, new Set(injections.map((i) => i.key)));
1114
1159
  const modifiedContent = (0, ast_helpers_1.placeStatementsOnOwnLines)((0, ast_helpers_1.generateCode)(ast, content), [...insertedStatements]);
1115
1160
  return {
1116
1161
  file: filePath,
@@ -1,190 +1,38 @@
1
1
  import { z } from 'zod';
2
2
  export declare const lockfileEntrySchema: z.ZodObject<{
3
- /** SHA-256 hex digest of the source string */
4
3
  sourceHash: z.ZodString;
5
- /** ISO-8601 timestamp of last translation */
6
4
  translatedAt: z.ZodString;
7
- /** Translated values keyed by locale code */
8
5
  languages: z.ZodRecord<z.ZodString, z.ZodString>;
9
- /** Metadata per locale */
10
6
  metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
11
7
  fromMemory: z.ZodOptional<z.ZodBoolean>;
12
8
  sourceHash: z.ZodOptional<z.ZodString>;
13
- /** This key's translation for the locale is known to be in TM; skip backfill. */
14
9
  inTm: z.ZodOptional<z.ZodBoolean>;
15
- /** Pseudo-localized (`--provider pseudo`), not a translation: any real run re-translates it. */
16
10
  pseudo: z.ZodOptional<z.ZodBoolean>;
17
- /**
18
- * Fingerprint of the glossary rules that applied to this key's source
19
- * for the locale (see glossaryFingerprint). Absent: none applied.
20
- * A run whose rules give a different fingerprint re-translates it.
21
- */
22
11
  glossaryHash: z.ZodOptional<z.ZodString>;
23
- /**
24
- * The translation came from the project's own files (e.g. a .po
25
- * catalogue), not from Babelize: glossary edits don't overwrite it.
26
- */
27
12
  external: z.ZodOptional<z.ZodBoolean>;
28
- /** Edited in the dashboard (a translation override), not translated by Babelize. */
29
13
  manual: z.ZodOptional<z.ZodBoolean>;
30
- }, "strip", z.ZodTypeAny, {
31
- sourceHash?: string | undefined;
32
- fromMemory?: boolean | undefined;
33
- inTm?: boolean | undefined;
34
- pseudo?: boolean | undefined;
35
- glossaryHash?: string | undefined;
36
- external?: boolean | undefined;
37
- manual?: boolean | undefined;
38
- }, {
39
- sourceHash?: string | undefined;
40
- fromMemory?: boolean | undefined;
41
- inTm?: boolean | undefined;
42
- pseudo?: boolean | undefined;
43
- glossaryHash?: string | undefined;
44
- external?: boolean | undefined;
45
- manual?: boolean | undefined;
46
- }>>>;
47
- }, "strip", z.ZodTypeAny, {
48
- sourceHash: string;
49
- translatedAt: string;
50
- languages: Record<string, string>;
51
- metadata?: Record<string, {
52
- sourceHash?: string | undefined;
53
- fromMemory?: boolean | undefined;
54
- inTm?: boolean | undefined;
55
- pseudo?: boolean | undefined;
56
- glossaryHash?: string | undefined;
57
- external?: boolean | undefined;
58
- manual?: boolean | undefined;
59
- }> | undefined;
60
- }, {
61
- sourceHash: string;
62
- translatedAt: string;
63
- languages: Record<string, string>;
64
- metadata?: Record<string, {
65
- sourceHash?: string | undefined;
66
- fromMemory?: boolean | undefined;
67
- inTm?: boolean | undefined;
68
- pseudo?: boolean | undefined;
69
- glossaryHash?: string | undefined;
70
- external?: boolean | undefined;
71
- manual?: boolean | undefined;
72
- }> | undefined;
73
- }>;
14
+ }, z.core.$strip>>>;
15
+ }, z.core.$strip>;
74
16
  export type LockfileEntry = z.infer<typeof lockfileEntrySchema>;
75
17
  export declare const lockfileDataSchema: z.ZodObject<{
76
18
  version: z.ZodDefault<z.ZodString>;
77
19
  projectId: z.ZodString;
78
20
  updatedAt: z.ZodString;
79
21
  keys: z.ZodRecord<z.ZodString, z.ZodObject<{
80
- /** SHA-256 hex digest of the source string */
81
22
  sourceHash: z.ZodString;
82
- /** ISO-8601 timestamp of last translation */
83
23
  translatedAt: z.ZodString;
84
- /** Translated values keyed by locale code */
85
24
  languages: z.ZodRecord<z.ZodString, z.ZodString>;
86
- /** Metadata per locale */
87
25
  metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
88
26
  fromMemory: z.ZodOptional<z.ZodBoolean>;
89
27
  sourceHash: z.ZodOptional<z.ZodString>;
90
- /** This key's translation for the locale is known to be in TM; skip backfill. */
91
28
  inTm: z.ZodOptional<z.ZodBoolean>;
92
- /** Pseudo-localized (`--provider pseudo`), not a translation: any real run re-translates it. */
93
29
  pseudo: z.ZodOptional<z.ZodBoolean>;
94
- /**
95
- * Fingerprint of the glossary rules that applied to this key's source
96
- * for the locale (see glossaryFingerprint). Absent: none applied.
97
- * A run whose rules give a different fingerprint re-translates it.
98
- */
99
30
  glossaryHash: z.ZodOptional<z.ZodString>;
100
- /**
101
- * The translation came from the project's own files (e.g. a .po
102
- * catalogue), not from Babelize: glossary edits don't overwrite it.
103
- */
104
31
  external: z.ZodOptional<z.ZodBoolean>;
105
- /** Edited in the dashboard (a translation override), not translated by Babelize. */
106
32
  manual: z.ZodOptional<z.ZodBoolean>;
107
- }, "strip", z.ZodTypeAny, {
108
- sourceHash?: string | undefined;
109
- fromMemory?: boolean | undefined;
110
- inTm?: boolean | undefined;
111
- pseudo?: boolean | undefined;
112
- glossaryHash?: string | undefined;
113
- external?: boolean | undefined;
114
- manual?: boolean | undefined;
115
- }, {
116
- sourceHash?: string | undefined;
117
- fromMemory?: boolean | undefined;
118
- inTm?: boolean | undefined;
119
- pseudo?: boolean | undefined;
120
- glossaryHash?: string | undefined;
121
- external?: boolean | undefined;
122
- manual?: boolean | undefined;
123
- }>>>;
124
- }, "strip", z.ZodTypeAny, {
125
- sourceHash: string;
126
- translatedAt: string;
127
- languages: Record<string, string>;
128
- metadata?: Record<string, {
129
- sourceHash?: string | undefined;
130
- fromMemory?: boolean | undefined;
131
- inTm?: boolean | undefined;
132
- pseudo?: boolean | undefined;
133
- glossaryHash?: string | undefined;
134
- external?: boolean | undefined;
135
- manual?: boolean | undefined;
136
- }> | undefined;
137
- }, {
138
- sourceHash: string;
139
- translatedAt: string;
140
- languages: Record<string, string>;
141
- metadata?: Record<string, {
142
- sourceHash?: string | undefined;
143
- fromMemory?: boolean | undefined;
144
- inTm?: boolean | undefined;
145
- pseudo?: boolean | undefined;
146
- glossaryHash?: string | undefined;
147
- external?: boolean | undefined;
148
- manual?: boolean | undefined;
149
- }> | undefined;
150
- }>>;
151
- }, "strip", z.ZodTypeAny, {
152
- keys: Record<string, {
153
- sourceHash: string;
154
- translatedAt: string;
155
- languages: Record<string, string>;
156
- metadata?: Record<string, {
157
- sourceHash?: string | undefined;
158
- fromMemory?: boolean | undefined;
159
- inTm?: boolean | undefined;
160
- pseudo?: boolean | undefined;
161
- glossaryHash?: string | undefined;
162
- external?: boolean | undefined;
163
- manual?: boolean | undefined;
164
- }> | undefined;
165
- }>;
166
- projectId: string;
167
- version: string;
168
- updatedAt: string;
169
- }, {
170
- keys: Record<string, {
171
- sourceHash: string;
172
- translatedAt: string;
173
- languages: Record<string, string>;
174
- metadata?: Record<string, {
175
- sourceHash?: string | undefined;
176
- fromMemory?: boolean | undefined;
177
- inTm?: boolean | undefined;
178
- pseudo?: boolean | undefined;
179
- glossaryHash?: string | undefined;
180
- external?: boolean | undefined;
181
- manual?: boolean | undefined;
182
- }> | undefined;
183
- }>;
184
- projectId: string;
185
- updatedAt: string;
186
- version?: string | undefined;
187
- }>;
33
+ }, z.core.$strip>>>;
34
+ }, z.core.$strip>>;
35
+ }, z.core.$strip>;
188
36
  export type LockfileData = z.infer<typeof lockfileDataSchema>;
189
37
  export interface PartitionedKey {
190
38
  key: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@babelize/engine",
3
- "version": "0.3.5",
3
+ "version": "0.3.7",
4
4
  "description": "Babelize translation pipeline (extract, keygen, translate, inject). Infrastructure is injected through provider/store interfaces.",
5
5
  "keywords": [
6
6
  "babelize",
@@ -32,7 +32,7 @@
32
32
  "@babel/parser": "^7.28.5",
33
33
  "@babel/traverse": "^7.28.5",
34
34
  "@babel/types": "^7.29.8",
35
- "@babelize/types": "^0.1.3",
35
+ "@babelize/types": "^0.1.4",
36
36
  "@vue/compiler-dom": "^3.5",
37
37
  "@vue/compiler-sfc": "^3.5",
38
38
  "diff": "^8.0.4",
@@ -42,7 +42,7 @@
42
42
  "magic-string": "^0.30",
43
43
  "node-html-parser": "^8.0.4",
44
44
  "recast": "^0.23",
45
- "zod": "^3.22.4"
45
+ "zod": "^4.6.6"
46
46
  },
47
47
  "devDependencies": {
48
48
  "@eslint/js": "^9.39.2",