i18next-cli 1.74.1 → 1.74.3
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/cjs/cli.js +1 -1
- package/dist/cjs/extractor/core/ast-visitors.js +7 -1
- package/dist/cjs/extractor/core/extractor.js +38 -0
- package/dist/cjs/extractor/core/key-finder.js +7 -3
- package/dist/cjs/extractor/parsers/ast-utils.js +15 -16
- package/dist/cjs/extractor/parsers/expression-resolver.js +272 -91
- package/dist/esm/cli.js +1 -1
- package/dist/esm/extractor/core/ast-visitors.js +7 -1
- package/dist/esm/extractor/core/extractor.js +38 -0
- package/dist/esm/extractor/core/key-finder.js +7 -3
- package/dist/esm/extractor/parsers/ast-utils.js +15 -16
- package/dist/esm/extractor/parsers/expression-resolver.js +272 -91
- package/package.json +17 -17
- package/types/extractor/core/ast-visitors.d.ts.map +1 -1
- package/types/extractor/core/extractor.d.ts.map +1 -1
- package/types/extractor/core/key-finder.d.ts.map +1 -1
- package/types/extractor/parsers/ast-utils.d.ts.map +1 -1
- package/types/extractor/parsers/expression-resolver.d.ts +45 -4
- package/types/extractor/parsers/expression-resolver.d.ts.map +1 -1
package/dist/cjs/cli.js
CHANGED
|
@@ -37,7 +37,7 @@ const program = new commander.Command();
|
|
|
37
37
|
program
|
|
38
38
|
.name('i18next-cli')
|
|
39
39
|
.description('A unified, high-performance i18next CLI.')
|
|
40
|
-
.version('1.74.
|
|
40
|
+
.version('1.74.3') // This string is replaced with the actual version at build time by rollup
|
|
41
41
|
.addHelpText('after', '\nUsing an AI coding agent? Install the i18next-localization skill: npx skills add i18next/i18next-cli\n');
|
|
42
42
|
// new: global config override option
|
|
43
43
|
program.option('-c, --config <path>', 'Path to i18next-cli config file (overrides detection)');
|
|
@@ -98,7 +98,13 @@ class ASTVisitors {
|
|
|
98
98
|
*/
|
|
99
99
|
preScanForConstants(node) {
|
|
100
100
|
this.expressionResolver.resetFileSymbols();
|
|
101
|
-
this.
|
|
101
|
+
this.expressionResolver.recording = true;
|
|
102
|
+
try {
|
|
103
|
+
this._walkForConstants(node);
|
|
104
|
+
}
|
|
105
|
+
finally {
|
|
106
|
+
this.expressionResolver.recording = false;
|
|
107
|
+
}
|
|
102
108
|
}
|
|
103
109
|
/**
|
|
104
110
|
* Recursive walker used exclusively by `preScanForConstants`.
|
|
@@ -132,6 +132,39 @@ function isExtExplicitlyInInputPatterns(ext, input) {
|
|
|
132
132
|
return new RegExp(`[{,*]${escaped}[},*]|[.]${escaped}(?:[^a-z]|$)`).test(p);
|
|
133
133
|
});
|
|
134
134
|
}
|
|
135
|
+
const extractionSiteRegexes = new WeakMap();
|
|
136
|
+
/**
|
|
137
|
+
* Cheap textual test for anything the extraction walk can turn into a key: a call
|
|
138
|
+
* to a configured function (`t(`, `i18n.t(`, `t?.(`, `t<Ns>(`, `new TranslatedError(`, a
|
|
139
|
+
* `t(` comment), or a mention of what binds a translation function under any name
|
|
140
|
+
* (useTranslation-style hooks, getFixedT, TFunction params) or of a Trans component.
|
|
141
|
+
* A false positive only costs a parse; a false negative would drop keys.
|
|
142
|
+
*
|
|
143
|
+
* @internal
|
|
144
|
+
*/
|
|
145
|
+
function mayContainExtractionSite(code, config) {
|
|
146
|
+
let re = extractionSiteRegexes.get(config);
|
|
147
|
+
if (!re) {
|
|
148
|
+
const calls = new Set(['t']);
|
|
149
|
+
const names = new Set(['getFixedT', 'TFunction']);
|
|
150
|
+
for (const fn of config.extract.functions || ['t', '*.t']) {
|
|
151
|
+
// 'tProps.*' matches any `tProps.<x>(` call, so the prefix is what to look for
|
|
152
|
+
if (fn.endsWith('.*'))
|
|
153
|
+
names.add(fn.slice(0, -2).split('.').pop());
|
|
154
|
+
else
|
|
155
|
+
calls.add(fn.split('.').pop());
|
|
156
|
+
}
|
|
157
|
+
for (const hook of config.extract.useTranslationNames || ['useTranslation', 'getT', 'useT']) {
|
|
158
|
+
names.add(typeof hook === 'string' ? hook : hook.name);
|
|
159
|
+
}
|
|
160
|
+
for (const component of config.extract.transComponents || ['Trans'])
|
|
161
|
+
names.add(component.split('.').pop());
|
|
162
|
+
const alt = (set) => [...set].map(s => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('|');
|
|
163
|
+
re = new RegExp(`(?<!\\w)(?:(?:${alt(calls)})\\s*(?:\\?\\.\\s*)?[(<]|(?:${alt(names)})(?![\\w$]))`);
|
|
164
|
+
extractionSiteRegexes.set(config, re);
|
|
165
|
+
}
|
|
166
|
+
return re.test(code);
|
|
167
|
+
}
|
|
135
168
|
/**
|
|
136
169
|
* Processes an individual source file for translation key extraction.
|
|
137
170
|
*
|
|
@@ -197,6 +230,11 @@ async function processFile(file, plugins, astVisitors, pluginContext, config, lo
|
|
|
197
230
|
}
|
|
198
231
|
return;
|
|
199
232
|
}
|
|
233
|
+
// A file that mentions no translation function, hook or component cannot yield
|
|
234
|
+
// a key, so skip the parse and walk (#297). The pre-scan has already harvested
|
|
235
|
+
// its constants and types for the other files. onVisitNode plugins see every node.
|
|
236
|
+
if (!plugins.some(p => p.onVisitNode) && !mayContainExtractionSite(code, config))
|
|
237
|
+
return;
|
|
200
238
|
// When a plugin transformed a non-native file, always use tsx so SWC can
|
|
201
239
|
// handle TypeScript syntax that the plugin may have extracted from the file.
|
|
202
240
|
const effectiveExt = (!isNativeExt && wasTransformedByPlugin) ? '.tsx' : fileExt;
|
|
@@ -94,9 +94,10 @@ async function findKeys(config, logger$1 = new logger.ConsoleLogger(), fileError
|
|
|
94
94
|
for (const file of sourceFiles) {
|
|
95
95
|
await extractor.preScanFile(file, astVisitors$1, otherConfig, logger$1, fileErrors);
|
|
96
96
|
}
|
|
97
|
-
// 6b.
|
|
97
|
+
// 6b. Re-resolve declarations that refer to ones in files scanned after them, and
|
|
98
|
+
// mirror declarations onto the local names they were imported as, so that
|
|
98
99
|
// `import { ResourceStatus as Status }` resolves like the unaliased import.
|
|
99
|
-
sharedExpressionResolver.
|
|
100
|
+
sharedExpressionResolver.finishPreScan();
|
|
100
101
|
// 7. Extraction pass: all shared tables are now fully populated, so every
|
|
101
102
|
// identifier reference can be resolved regardless of file order.
|
|
102
103
|
for (const file of sourceFiles) {
|
|
@@ -140,13 +141,16 @@ async function processSourceFiles(config) {
|
|
|
140
141
|
const userIgnore = Array.isArray(config.extract.ignore)
|
|
141
142
|
? config.extract.ignore
|
|
142
143
|
: config.extract.ignore ? [config.extract.ignore] : [];
|
|
143
|
-
|
|
144
|
+
const files = await glob.glob(config.extract.input, {
|
|
144
145
|
// Combine default ignore patterns with user-configured ones
|
|
145
146
|
ignore: [...defaultIgnore, ...userIgnore],
|
|
146
147
|
cwd: process.cwd(),
|
|
147
148
|
// A directory named e.g. `abc.tsx` matches the input globs
|
|
148
149
|
nodir: true,
|
|
149
150
|
});
|
|
151
|
+
// glob's order is not stable across runs, and when two call sites give one key
|
|
152
|
+
// different defaults the file visited first wins, so sort for reproducible output (#296)
|
|
153
|
+
return files.sort();
|
|
150
154
|
}
|
|
151
155
|
|
|
152
156
|
exports.findKeys = findKeys;
|
|
@@ -368,22 +368,21 @@ function isSimpleTemplateLiteral(literal) {
|
|
|
368
368
|
* @private
|
|
369
369
|
*/
|
|
370
370
|
function getObjectPropValue(object, propName, identifierResolver, expressionResolver) {
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
return
|
|
385
|
-
|
|
386
|
-
return undefined;
|
|
371
|
+
// Shorthand `{ ns }` resolves like `{ ns: ns }`
|
|
372
|
+
const val = getObjectPropValueExpression(object, propName);
|
|
373
|
+
if (!val)
|
|
374
|
+
return undefined;
|
|
375
|
+
if (val.type === 'StringLiteral')
|
|
376
|
+
return val.value;
|
|
377
|
+
if (val.type === 'Identifier' && identifierResolver)
|
|
378
|
+
return identifierResolver(val.value);
|
|
379
|
+
if (val.type === 'TemplateLiteral' && isSimpleTemplateLiteral(val))
|
|
380
|
+
return val.quasis[0].cooked;
|
|
381
|
+
if (val.type === 'BooleanLiteral')
|
|
382
|
+
return val.value;
|
|
383
|
+
if (val.type === 'NumericLiteral')
|
|
384
|
+
return val.value;
|
|
385
|
+
return expressionResolver?.(val) ?? ''; // Indicate presence for other types
|
|
387
386
|
}
|
|
388
387
|
|
|
389
388
|
exports.buildByteToCharMap = buildByteToCharMap;
|