i18next-cli 1.10.1 → 1.10.2

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.
Files changed (56) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/README.md +3 -0
  3. package/dist/cjs/cli.js +1 -1
  4. package/dist/cjs/extractor/core/ast-visitors.js +1 -0
  5. package/dist/cjs/extractor/core/key-finder.js +1 -1
  6. package/dist/cjs/extractor/core/translation-manager.js +1 -1
  7. package/dist/cjs/extractor/parsers/call-expression-handler.js +1 -0
  8. package/dist/cjs/extractor/parsers/expression-resolver.js +1 -0
  9. package/dist/cjs/extractor/parsers/jsx-handler.js +1 -0
  10. package/dist/cjs/extractor/parsers/scope-manager.js +1 -0
  11. package/dist/esm/cli.js +1 -1
  12. package/dist/esm/extractor/core/ast-visitors.js +1 -0
  13. package/dist/esm/extractor/core/key-finder.js +1 -1
  14. package/dist/esm/extractor/core/translation-manager.js +1 -1
  15. package/dist/esm/extractor/parsers/call-expression-handler.js +1 -0
  16. package/dist/esm/extractor/parsers/expression-resolver.js +1 -0
  17. package/dist/esm/extractor/parsers/jsx-handler.js +1 -0
  18. package/dist/esm/extractor/parsers/scope-manager.js +1 -0
  19. package/package.json +1 -1
  20. package/src/cli.ts +1 -1
  21. package/src/extractor/core/ast-visitors.ts +170 -0
  22. package/src/extractor/core/extractor.ts +1 -1
  23. package/src/extractor/core/key-finder.ts +2 -2
  24. package/src/extractor/core/translation-manager.ts +88 -8
  25. package/src/extractor/index.ts +1 -1
  26. package/src/extractor/parsers/call-expression-handler.ts +506 -0
  27. package/src/extractor/parsers/expression-resolver.ts +178 -0
  28. package/src/extractor/parsers/jsx-handler.ts +358 -0
  29. package/src/extractor/parsers/scope-manager.ts +327 -0
  30. package/src/extractor.ts +1 -1
  31. package/src/types.ts +82 -0
  32. package/types/extractor/core/ast-visitors.d.ts +75 -0
  33. package/types/extractor/core/ast-visitors.d.ts.map +1 -0
  34. package/types/extractor/core/extractor.d.ts +1 -1
  35. package/types/extractor/core/extractor.d.ts.map +1 -1
  36. package/types/extractor/core/key-finder.d.ts.map +1 -1
  37. package/types/extractor/core/translation-manager.d.ts.map +1 -1
  38. package/types/extractor/index.d.ts +1 -1
  39. package/types/extractor/index.d.ts.map +1 -1
  40. package/types/extractor/parsers/call-expression-handler.d.ts +74 -0
  41. package/types/extractor/parsers/call-expression-handler.d.ts.map +1 -0
  42. package/types/extractor/parsers/expression-resolver.d.ts +62 -0
  43. package/types/extractor/parsers/expression-resolver.d.ts.map +1 -0
  44. package/types/extractor/parsers/jsx-handler.d.ts +44 -0
  45. package/types/extractor/parsers/jsx-handler.d.ts.map +1 -0
  46. package/types/extractor/parsers/scope-manager.d.ts +99 -0
  47. package/types/extractor/parsers/scope-manager.d.ts.map +1 -0
  48. package/types/extractor.d.ts +1 -1
  49. package/types/extractor.d.ts.map +1 -1
  50. package/types/types.d.ts +77 -0
  51. package/types/types.d.ts.map +1 -1
  52. package/dist/cjs/extractor/parsers/ast-visitors.js +0 -1
  53. package/dist/esm/extractor/parsers/ast-visitors.js +0 -1
  54. package/src/extractor/parsers/ast-visitors.ts +0 -1510
  55. package/types/extractor/parsers/ast-visitors.d.ts +0 -352
  56. package/types/extractor/parsers/ast-visitors.d.ts.map +0 -1
@@ -1,352 +0,0 @@
1
- import type { Module, Node, Expression } from '@swc/core';
2
- import type { PluginContext, I18nextToolkitConfig, Logger, ScopeInfo } from '../../types';
3
- export interface ASTVisitorHooks {
4
- onBeforeVisitNode?: (node: Node) => void;
5
- onAfterVisitNode?: (node: Node) => void;
6
- resolvePossibleContextStringValues?: (expression: Expression, returnEmptyStrings?: boolean) => string[];
7
- resolvePossibleKeyStringValues?: (expression: Expression, returnEmptyStrings?: boolean) => string[];
8
- }
9
- /**
10
- * AST visitor class that traverses JavaScript/TypeScript syntax trees to extract translation keys.
11
- *
12
- * This class implements a manual recursive walker that:
13
- * - Maintains scope information for tracking useTranslation and getFixedT calls
14
- * - Extracts keys from t() function calls with various argument patterns
15
- * - Handles JSX Trans components with complex children serialization
16
- * - Supports both string literals and selector API for type-safe keys
17
- * - Processes pluralization and context variants
18
- * - Manages namespace resolution from multiple sources
19
- *
20
- * The visitor respects configuration options for separators, function names,
21
- * component names, and other extraction settings.
22
- *
23
- * @example
24
- * ```typescript
25
- * const visitors = new ASTVisitors(config, pluginContext, logger)
26
- * visitors.visit(parsedAST)
27
- *
28
- * // The pluginContext will now contain all extracted keys
29
- * ```
30
- */
31
- export declare class ASTVisitors {
32
- private readonly pluginContext;
33
- private readonly config;
34
- private readonly logger;
35
- private scopeStack;
36
- private hooks;
37
- objectKeys: Set<string>;
38
- private scope;
39
- /**
40
- * Creates a new AST visitor instance.
41
- *
42
- * @param config - Toolkit configuration with extraction settings
43
- * @param pluginContext - Context for adding discovered translation keys
44
- * @param logger - Logger for warnings and debug information
45
- */
46
- constructor(config: Omit<I18nextToolkitConfig, 'plugins'>, pluginContext: PluginContext, logger: Logger, hooks?: ASTVisitorHooks);
47
- /**
48
- * Main entry point for AST traversal.
49
- * Creates a root scope and begins the recursive walk through the syntax tree.
50
- *
51
- * @param node - The root module node to traverse
52
- */
53
- visit(node: Module): void;
54
- /**
55
- * Recursively walks through AST nodes, handling scoping and visiting logic.
56
- *
57
- * This is the core traversal method that:
58
- * 1. Manages function scopes (enter/exit)
59
- * 2. Dispatches to specific handlers based on node type
60
- * 3. Recursively processes child nodes
61
- * 4. Maintains proper scope cleanup
62
- *
63
- * @param node - The current AST node to process
64
- *
65
- * @private
66
- */
67
- private walk;
68
- /**
69
- * Enters a new variable scope by pushing a new scope map onto the stack.
70
- * Used when entering functions to isolate variable declarations.
71
- *
72
- * @private
73
- */
74
- private enterScope;
75
- /**
76
- * Exits the current variable scope by popping the top scope map.
77
- * Used when leaving functions to clean up variable tracking.
78
- *
79
- * @private
80
- */
81
- private exitScope;
82
- /**
83
- * Stores variable information in the current scope.
84
- * Used to track translation functions and their configuration.
85
- *
86
- * @param name - Variable name to store
87
- * @param info - Scope information about the variable
88
- *
89
- * @private
90
- */
91
- private setVarInScope;
92
- /**
93
- * Retrieves variable information from the scope chain.
94
- * Searches from innermost to outermost scope.
95
- *
96
- * @param name - Variable name to look up
97
- * @returns Scope information if found, undefined otherwise
98
- *
99
- * @private
100
- */
101
- getVarFromScope(name: string): ScopeInfo | undefined;
102
- /**
103
- * Handles variable declarations that might define translation functions.
104
- *
105
- * Processes two patterns:
106
- * 1. `const { t } = useTranslation(...)` - React i18next pattern
107
- * 2. `const t = i18next.getFixedT(...)` - Core i18next pattern
108
- *
109
- * Extracts namespace and key prefix information for later use.
110
- *
111
- * @param node - Variable declarator node to process
112
- *
113
- * @private
114
- */
115
- private handleVariableDeclarator;
116
- /**
117
- * Handles useTranslation calls for comment scope resolution.
118
- * This is a separate method to store scope info in the legacy scope map
119
- * that the comment parser can access.
120
- *
121
- * @param node - Variable declarator with useTranslation call
122
- * @param callExpr - The CallExpression node representing the useTranslation invocation
123
- * @param hookConfig - Configuration describing argument positions for namespace and keyPrefix
124
- *
125
- * @private
126
- */
127
- private handleUseTranslationForComments;
128
- /**
129
- * Processes useTranslation hook declarations to extract scope information.
130
- *
131
- * Handles various destructuring patterns:
132
- * - `const [t] = useTranslation('ns')` - Array destructuring
133
- * - `const { t } = useTranslation('ns')` - Object destructuring
134
- * - `const { t: myT } = useTranslation('ns')` - Aliased destructuring
135
- *
136
- * Extracts namespace from the first argument and keyPrefix from options.
137
- *
138
- * @param node - Variable declarator with useTranslation call
139
- * @param callExpr - The CallExpression node representing the useTranslation invocation
140
- * @param hookConfig - Configuration describing argument positions for namespace and keyPrefix
141
- *
142
- * @private
143
- */
144
- private handleUseTranslationDeclarator;
145
- /**
146
- * Processes getFixedT function declarations to extract scope information.
147
- *
148
- * Handles the pattern: `const t = i18next.getFixedT(lng, ns, keyPrefix)`
149
- * - Ignores the first argument (language)
150
- * - Extracts namespace from the second argument
151
- * - Extracts key prefix from the third argument
152
- *
153
- * @param node - Variable declarator with getFixedT call
154
- * @param callExpr - The CallExpression node representing the getFixedT invocation
155
- *
156
- * @private
157
- */
158
- private handleGetFixedTDeclarator;
159
- /**
160
- * Processes function call expressions to extract translation keys.
161
- *
162
- * This is the core extraction method that handles:
163
- * - Standard t() calls with string literals
164
- * - Selector API calls with arrow functions: `t($ => $.path.to.key)`
165
- * - Namespace resolution from multiple sources
166
- * - Default value extraction from various argument patterns
167
- * - Pluralization and context handling
168
- * - Key prefix application from scope
169
- *
170
- * @param node - Call expression node to process
171
- *
172
- * @private
173
- */
174
- private handleCallExpression;
175
- /**
176
- * Processed a call expression to extract keys from the specified argument.
177
- *
178
- * @param node - The call expression node
179
- * @param argIndex - The index of the argument to process
180
- * @returns An object containing the keys to process and a flag indicating if the selector API is used
181
- */
182
- private handleCallExpressionArgument;
183
- /**
184
- * Generates plural form keys based on the primary language's plural rules.
185
- *
186
- * Uses Intl.PluralRules to determine the correct plural categories
187
- * for the configured primary language and generates suffixed keys
188
- * for each category (e.g., 'item_one', 'item_other').
189
- *
190
- * @param key - Base key name for pluralization
191
- * @param ns - Namespace for the keys
192
- * @param options - object expression options
193
- * @param isOrdinal - isOrdinal flag
194
- *
195
- * @private
196
- */
197
- private handlePluralKeys;
198
- /**
199
- * Processes JSX elements to extract translation keys from Trans components.
200
- *
201
- * Identifies configured Trans components and delegates to the JSX parser
202
- * for complex children serialization and attribute extraction.
203
- *
204
- * @param node - JSX element node to process
205
- *
206
- * @private
207
- */
208
- private handleJSXElement;
209
- /**
210
- * Generates plural keys for Trans components, with support for tOptions plural defaults.
211
- *
212
- * @param key - Base key name for pluralization
213
- * @param defaultValue - Default value for the keys
214
- * @param ns - Namespace for the keys
215
- * @param isOrdinal - Whether to generate ordinal plural forms
216
- * @param optionsNode - Optional tOptions object expression for plural-specific defaults
217
- *
218
- * @private
219
- */
220
- private generatePluralKeysForTrans;
221
- /**
222
- * Extracts element name from JSX opening tag.
223
- *
224
- * Handles both simple identifiers and member expressions:
225
- * - `<Trans>` → 'Trans'
226
- * - `<React.Trans>` → 'React.Trans'
227
- *
228
- * @param node - JSX element node
229
- * @returns Element name or undefined if not extractable
230
- *
231
- * @private
232
- */
233
- private getElementName;
234
- /**
235
- * Extracts translation key from selector API arrow function.
236
- *
237
- * Processes selector expressions like:
238
- * - `$ => $.path.to.key` → 'path.to.key'
239
- * - `$ => $.app['title'].main` → 'app.title.main'
240
- * - `$ => { return $.nested.key; }` → 'nested.key'
241
- *
242
- * Handles both dot notation and bracket notation, respecting
243
- * the configured key separator or flat key structure.
244
- *
245
- * @param node - Arrow function expression from selector call
246
- * @returns Extracted key path or null if not statically analyzable
247
- *
248
- * @private
249
- */
250
- private extractKeyFromSelector;
251
- /**
252
- * Resolves an expression to one or more possible context string values that can be
253
- * determined statically from the AST. This is a wrapper around the plugin hook
254
- * `extractContextFromExpression` and {@link resolvePossibleStringValuesFromExpression}.
255
- *
256
- * @param expression - The SWC AST expression node to resolve
257
- * @returns An array of possible context string values that the expression may produce.
258
- *
259
- * @private
260
- */
261
- private resolvePossibleContextStringValues;
262
- /**
263
- * Resolves an expression to one or more possible key string values that can be
264
- * determined statically from the AST. This is a wrapper around the plugin hook
265
- * `extractKeysFromExpression` and {@link resolvePossibleStringValuesFromExpression}.
266
- *
267
- * @param expression - The SWC AST expression node to resolve
268
- * @returns An array of possible key string values that the expression may produce.
269
- *
270
- * @private
271
- */
272
- private resolvePossibleKeyStringValues;
273
- /**
274
- * Resolves an expression to one or more possible string values that can be
275
- * determined statically from the AST.
276
- *
277
- * Supports:
278
- * - StringLiteral -> single value (filtered to exclude empty strings for context)
279
- * - NumericLiteral -> single value
280
- * - BooleanLiteral -> single value
281
- * - ConditionalExpression (ternary) -> union of consequent and alternate resolved values
282
- * - TemplateLiteral -> union of all possible string values
283
- * - The identifier `undefined` -> empty array
284
- *
285
- * For any other expression types (identifiers, function calls, member expressions,
286
- * etc.) the value cannot be determined statically and an empty array is returned.
287
- *
288
- * @private
289
- * @param expression - The SWC AST expression node to resolve
290
- * @param returnEmptyStrings - Whether to include empty strings in the result
291
- * @returns An array of possible string values that the expression may produce.
292
- */
293
- private resolvePossibleStringValuesFromExpression;
294
- private resolvePossibleStringValuesFromType;
295
- /**
296
- * Resolves a template literal string to one or more possible strings that can be
297
- * determined statically from the AST.
298
- *
299
- * @private
300
- * @param templateString - The SWC AST template literal string to resolve
301
- * @returns An array of possible string values that the template may produce.
302
- */
303
- private resolvePossibleStringValuesFromTemplateString;
304
- /**
305
- * Resolves a template literal type to one or more possible strings that can be
306
- * determined statically from the AST.
307
- *
308
- * @private
309
- * @param templateLiteralType - The SWC AST template literal type to resolve
310
- * @returns An array of possible string values that the template may produce.
311
- */
312
- private resolvePossibleStringValuesFromTemplateLiteralType;
313
- /**
314
- * Finds the configuration for a given useTranslation function name.
315
- * Applies default argument positions if none are specified.
316
- *
317
- * @param name - The identifier name to look up in the configured useTranslationNames
318
- * @returns The resolved UseTranslationHookConfig when a match is found, otherwise undefined
319
- */
320
- private getUseTranslationConfig;
321
- /**
322
- * Serializes a callee node (Identifier or MemberExpression) into a string.
323
- *
324
- * Produces a dotted name for simple callees that can be used for scope lookups
325
- * or configuration matching.
326
- *
327
- * Supported inputs:
328
- * - Identifier: returns the identifier name (e.g., `t` -> "t")
329
- * - MemberExpression with Identifier parts: returns a dotted path of identifiers
330
- * (e.g., `i18n.t` -> "i18n.t", `i18n.getFixedT` -> "i18n.getFixedT")
331
- *
332
- * Behavior notes:
333
- * - Computed properties are not supported and cause this function to return null
334
- * (e.g., `i18n['t']` -> null).
335
- * - The base of a MemberExpression must be a simple Identifier. More complex bases
336
- * (other expressions, `this`, etc.) will result in null.
337
- * - This function does not attempt to resolve or evaluate expressions — it only
338
- * serializes static identifier/member chains.
339
- *
340
- * Examples:
341
- * - Identifier callee: { type: 'Identifier', value: 't' } -> "t"
342
- * - Member callee: { type: 'MemberExpression', object: { type: 'Identifier', value: 'i18n' }, property: { type: 'Identifier', value: 't' } } -> "i18n.t"
343
- *
344
- * @param callee - The CallExpression callee node to serialize
345
- * @returns A dotted string name for supported callees, or null when the callee
346
- * is a computed/unsupported expression.
347
- *
348
- * @private
349
- */
350
- private getFunctionName;
351
- }
352
- //# sourceMappingURL=ast-visitors.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ast-visitors.d.ts","sourceRoot":"","sources":["../../../src/extractor/parsers/ast-visitors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,IAAI,EAA6F,UAAU,EAAkD,MAAM,WAAW,CAAA;AACpM,OAAO,KAAK,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,EAAgB,SAAS,EAAE,MAAM,aAAa,CAAA;AAUvG,MAAM,WAAW,eAAe;IAC9B,iBAAiB,CAAC,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,IAAI,CAAA;IACxC,gBAAgB,CAAC,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,IAAI,CAAA;IACvC,kCAAkC,CAAC,EAAE,CAAC,UAAU,EAAE,UAAU,EAAE,kBAAkB,CAAC,EAAE,OAAO,KAAK,MAAM,EAAE,CAAA;IACvG,8BAA8B,CAAC,EAAE,CAAC,UAAU,EAAE,UAAU,EAAE,kBAAkB,CAAC,EAAE,OAAO,KAAK,MAAM,EAAE,CAAA;CACpG;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,qBAAa,WAAW;IACtB,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAe;IAC7C,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAuC;IAC9D,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAQ;IAC/B,OAAO,CAAC,UAAU,CAAoC;IACtD,OAAO,CAAC,KAAK,CAAiB;IAEvB,UAAU,cAAoB;IAErC,OAAO,CAAC,KAAK,CAAqE;IAElF;;;;;;OAMG;gBAED,MAAM,EAAE,IAAI,CAAC,oBAAoB,EAAE,SAAS,CAAC,EAC7C,aAAa,EAAE,aAAa,EAC5B,MAAM,EAAE,MAAM,EACd,KAAK,CAAC,EAA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