i18next-cli 1.80.7 → 1.81.0

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/README.md CHANGED
@@ -374,7 +374,7 @@ npx i18next-cli lint
374
374
  **What it checks:**
375
375
 
376
376
  - **Hardcoded strings** *(error)* — user-facing text in JSX elements and attributes that isn't wrapped in `t()`/`<Trans>`.
377
- - **Interpolation parameters** *(error)* — mismatches between `{{placeholders}}` in a translation and the params passed to `t()` (missing or unused). Toggle with `lint.checkInterpolationParams` (default: `true`). With `extract.messageFormat: 'icu'` or `'mf2'` the arguments come from the ICU or MF2 parser (`{name}`, `{$name}`), so plurals like `one {{count} row}` are not read as `{{…}}` interpolation.
377
+ - **Interpolation parameters** *(error)* — mismatches between `{{placeholders}}` in a translation and the params passed to `t()` (missing or unused), also for selector calls like `t($ => $.user.greeting, { name })`. Toggle with `lint.checkInterpolationParams` (default: `true`). With `extract.messageFormat: 'icu'` or `'mf2'` the arguments come from the ICU or MF2 parser (`{name}`, `{$name}`), so plurals like `one {{count} row}` are not read as `{{…}}` interpolation.
378
378
  - **ICU / MF2 messages** *(error, only with `extract.messageFormat: 'icu'` or `'mf2'`)*: every value in the translation files of all locales is parsed. The linter reports, with file, key and line (JSON files):
379
379
  - syntax errors with the parser's message, e.g. `greeting: ICU syntax error: EXPECT_ARGUMENT_CLOSING_BRACE at column 7`
380
380
  - argument names that differ from the primary language (a renamed or missing `{name}`). `#` in a plural is not an argument
package/dist/cjs/cli.js CHANGED
@@ -39,7 +39,7 @@ const program = new commander.Command();
39
39
  program
40
40
  .name('i18next-cli')
41
41
  .description('A unified, high-performance i18next CLI.')
42
- .version('1.80.7') // This string is replaced with the actual version at build time by rollup
42
+ .version('1.81.0') // This string is replaced with the actual version at build time by rollup
43
43
  .addHelpText('after', '\nUsing an AI coding agent? Install the i18next-localization skill: npx skills add i18next/i18next-cli\n');
44
44
  // new: global config override option
45
45
  program.option('-c, --config <path>', 'Path to i18next-cli config file (overrides detection)');
@@ -11,6 +11,8 @@ var wrapOra = require('./utils/wrap-ora.js');
11
11
  var jsxAttributes = require('./utils/jsx-attributes.js');
12
12
  var astUtils = require('./extractor/parsers/ast-utils.js');
13
13
  var functionMatcher = require('./extractor/utils/function-matcher.js');
14
+ var astVisitors = require('./extractor/core/ast-visitors.js');
15
+ var nestedObject = require('./utils/nested-object.js');
14
16
  var messageFormat = require('./utils/message-format.js');
15
17
  var lintBaseline = require('./utils/lint-baseline.js');
16
18
  var funnelMsgTracker = require('./utils/funnel-msg-tracker.js');
@@ -65,7 +67,10 @@ async function loadPrimaryTranslationValues(config) {
65
67
  try {
66
68
  const raw = await promises.readFile(filePath, 'utf-8');
67
69
  const json = JSON.parse(raw);
68
- for (const [k, v] of Object.entries(json)) {
70
+ // Nested resources too: `{ "a": { "b": "…" } }` → "a.b"
71
+ const keySeparator = config.extract?.keySeparator ?? '.';
72
+ for (const k of nestedObject.getNestedKeys(json, keySeparator)) {
73
+ const v = nestedObject.getNestedValue(json, k, keySeparator);
69
74
  if (typeof v === 'string')
70
75
  result.set(k, v);
71
76
  }
@@ -83,7 +88,7 @@ const i18nextOptionKeys = new Set([
83
88
  'defaultValue', 'count', 'context', 'ns', 'lng', 'lngs',
84
89
  'fallbackLng', 'returnDetails', 'returnObjects', 'joinArrays',
85
90
  'postProcess', 'interpolation', 'skipInterpolation', 'replace',
86
- 'ordinal', 'keySeparator', 'nsSeparator', 'tDescription',
91
+ 'ordinal', 'keySeparator', 'nsSeparator', 'tDescription', 'keyPrefix',
87
92
  ]);
88
93
  function isI18nextOptionKey(key) {
89
94
  if (i18nextOptionKeys.has(key))
@@ -178,6 +183,28 @@ function lintInterpolationParams(ast, code, config, translationValues, analyze)
178
183
  return code.substring(0, pos).split('\n').length;
179
184
  };
180
185
  const callSites = [];
186
+ // The keys the extractor resolves for each call (selector path, keyPrefix and namespace scope
187
+ // included), walked once and only for a file with a selector call: `t($ => $.a.b)` → ['a.b']
188
+ let keysByCall;
189
+ const keysOfCall = (call) => {
190
+ if (!keysByCall) {
191
+ const found = new Map();
192
+ let current;
193
+ const silent = { info() { }, warn() { }, error() { } };
194
+ const context = {
195
+ addKey: ({ key }) => { found.set(current, [...(found.get(current) ?? []), key]); },
196
+ config,
197
+ logger: silent,
198
+ getVarFromScope: () => undefined,
199
+ setVarInScope: () => { },
200
+ };
201
+ const visitors = new astVisitors.ASTVisitors(config, context, silent, { onBeforeVisitNode: (node) => { current = node; } });
202
+ visitors.setCurrentFile('', code);
203
+ visitors.visit(ast);
204
+ keysByCall = found;
205
+ }
206
+ return keysByCall.get(call) ?? [];
207
+ };
181
208
  // Traverse AST for CallExpressions matching t() or i18n.t()
182
209
  function walk(node, ancestors) {
183
210
  if (!node || typeof node !== 'object')
@@ -213,8 +240,9 @@ function lintInterpolationParams(ast, code, config, translationValues, analyze)
213
240
  const arg0 = arg0raw?.expression ?? arg0raw;
214
241
  const arg1 = arg1raw?.expression ?? arg1raw;
215
242
  const arg2 = arg2raw?.expression ?? arg2raw;
216
- // Only check interpolation params if the first argument is a string literal
217
- if (arg0?.type === 'StringLiteral') {
243
+ // Only check interpolation params if the first argument is a string literal or a selector
244
+ const isSelector = arg0?.type === 'ArrowFunctionExpression';
245
+ if (arg0?.type === 'StringLiteral' || isSelector) {
218
246
  const keyOrStr = arg0.value;
219
247
  // Detect 3-argument form: t('key', 'Default string {{foo}}', { foo })
220
248
  // In this form arg1 is the default string and arg2 is the params object.
@@ -257,10 +285,18 @@ function lintInterpolationParams(ast, code, config, translationValues, analyze)
257
285
  if (isThreeArgForm) {
258
286
  resolvedStr = translationArg.value;
259
287
  }
288
+ else if (isSelector) {
289
+ // The first key with a value, like i18next's fallback (a ternary or plural yields several)
290
+ const value = keysOfCall(node).map(k => translationValues?.get(k)).find(v => v !== undefined);
291
+ if (value === undefined)
292
+ return;
293
+ resolvedStr = value;
294
+ }
260
295
  else {
261
296
  resolvedStr = translationValues?.get(keyOrStr) ?? keyOrStr;
262
297
  }
263
- const searchText = arg0.raw ?? `"${arg0.value}"`;
298
+ // spans are file-relative character indices here (normalizeSpansToCharIndices)
299
+ const searchText = isSelector ? code.slice(arg0.span.start, arg0.span.end) : (arg0.raw ?? `"${arg0.value}"`);
264
300
  callSites.push({ translationStr: resolvedStr, searchText, paramKeys });
265
301
  }
266
302
  }
package/dist/esm/cli.js CHANGED
@@ -33,7 +33,7 @@ const program = new Command();
33
33
  program
34
34
  .name('i18next-cli')
35
35
  .description('A unified, high-performance i18next CLI.')
36
- .version('1.80.7') // This string is replaced with the actual version at build time by rollup
36
+ .version('1.81.0') // This string is replaced with the actual version at build time by rollup
37
37
  .addHelpText('after', '\nUsing an AI coding agent? Install the i18next-localization skill: npx skills add i18next/i18next-cli\n');
38
38
  // new: global config override option
39
39
  program.option('-c, --config <path>', 'Path to i18next-cli config file (overrides detection)');
@@ -9,6 +9,8 @@ import { createSpinnerLike } from './utils/wrap-ora.js';
9
9
  import { acceptedTags, translatableAttributes, ignoredTags, ignoredAttributeLowerSet } from './utils/jsx-attributes.js';
10
10
  import { normalizeSpansToCharIndices, collectIgnoredLineRanges, lineColumnFromOffset } from './extractor/parsers/ast-utils.js';
11
11
  import { matchesFunctionPattern } from './extractor/utils/function-matcher.js';
12
+ import { ASTVisitors } from './extractor/core/ast-visitors.js';
13
+ import { getNestedKeys, getNestedValue } from './utils/nested-object.js';
12
14
  import { loadMessageAnalyzer, lintMessageFormatResources, lintI18nextResources, interpolationNames } from './utils/message-format.js';
13
15
  import { lintBaselinePath, writeLintBaseline, readLintBaseline, applyLintBaseline } from './utils/lint-baseline.js';
14
16
  import { shouldShowFunnel, recordFunnelShown } from './utils/funnel-msg-tracker.js';
@@ -63,7 +65,10 @@ async function loadPrimaryTranslationValues(config) {
63
65
  try {
64
66
  const raw = await readFile(filePath, 'utf-8');
65
67
  const json = JSON.parse(raw);
66
- for (const [k, v] of Object.entries(json)) {
68
+ // Nested resources too: `{ "a": { "b": "…" } }` → "a.b"
69
+ const keySeparator = config.extract?.keySeparator ?? '.';
70
+ for (const k of getNestedKeys(json, keySeparator)) {
71
+ const v = getNestedValue(json, k, keySeparator);
67
72
  if (typeof v === 'string')
68
73
  result.set(k, v);
69
74
  }
@@ -81,7 +86,7 @@ const i18nextOptionKeys = new Set([
81
86
  'defaultValue', 'count', 'context', 'ns', 'lng', 'lngs',
82
87
  'fallbackLng', 'returnDetails', 'returnObjects', 'joinArrays',
83
88
  'postProcess', 'interpolation', 'skipInterpolation', 'replace',
84
- 'ordinal', 'keySeparator', 'nsSeparator', 'tDescription',
89
+ 'ordinal', 'keySeparator', 'nsSeparator', 'tDescription', 'keyPrefix',
85
90
  ]);
86
91
  function isI18nextOptionKey(key) {
87
92
  if (i18nextOptionKeys.has(key))
@@ -176,6 +181,28 @@ function lintInterpolationParams(ast, code, config, translationValues, analyze)
176
181
  return code.substring(0, pos).split('\n').length;
177
182
  };
178
183
  const callSites = [];
184
+ // The keys the extractor resolves for each call (selector path, keyPrefix and namespace scope
185
+ // included), walked once and only for a file with a selector call: `t($ => $.a.b)` → ['a.b']
186
+ let keysByCall;
187
+ const keysOfCall = (call) => {
188
+ if (!keysByCall) {
189
+ const found = new Map();
190
+ let current;
191
+ const silent = { info() { }, warn() { }, error() { } };
192
+ const context = {
193
+ addKey: ({ key }) => { found.set(current, [...(found.get(current) ?? []), key]); },
194
+ config,
195
+ logger: silent,
196
+ getVarFromScope: () => undefined,
197
+ setVarInScope: () => { },
198
+ };
199
+ const visitors = new ASTVisitors(config, context, silent, { onBeforeVisitNode: (node) => { current = node; } });
200
+ visitors.setCurrentFile('', code);
201
+ visitors.visit(ast);
202
+ keysByCall = found;
203
+ }
204
+ return keysByCall.get(call) ?? [];
205
+ };
179
206
  // Traverse AST for CallExpressions matching t() or i18n.t()
180
207
  function walk(node, ancestors) {
181
208
  if (!node || typeof node !== 'object')
@@ -211,8 +238,9 @@ function lintInterpolationParams(ast, code, config, translationValues, analyze)
211
238
  const arg0 = arg0raw?.expression ?? arg0raw;
212
239
  const arg1 = arg1raw?.expression ?? arg1raw;
213
240
  const arg2 = arg2raw?.expression ?? arg2raw;
214
- // Only check interpolation params if the first argument is a string literal
215
- if (arg0?.type === 'StringLiteral') {
241
+ // Only check interpolation params if the first argument is a string literal or a selector
242
+ const isSelector = arg0?.type === 'ArrowFunctionExpression';
243
+ if (arg0?.type === 'StringLiteral' || isSelector) {
216
244
  const keyOrStr = arg0.value;
217
245
  // Detect 3-argument form: t('key', 'Default string {{foo}}', { foo })
218
246
  // In this form arg1 is the default string and arg2 is the params object.
@@ -255,10 +283,18 @@ function lintInterpolationParams(ast, code, config, translationValues, analyze)
255
283
  if (isThreeArgForm) {
256
284
  resolvedStr = translationArg.value;
257
285
  }
286
+ else if (isSelector) {
287
+ // The first key with a value, like i18next's fallback (a ternary or plural yields several)
288
+ const value = keysOfCall(node).map(k => translationValues?.get(k)).find(v => v !== undefined);
289
+ if (value === undefined)
290
+ return;
291
+ resolvedStr = value;
292
+ }
258
293
  else {
259
294
  resolvedStr = translationValues?.get(keyOrStr) ?? keyOrStr;
260
295
  }
261
- const searchText = arg0.raw ?? `"${arg0.value}"`;
296
+ // spans are file-relative character indices here (normalizeSpansToCharIndices)
297
+ const searchText = isSelector ? code.slice(arg0.span.start, arg0.span.end) : (arg0.raw ?? `"${arg0.value}"`);
262
298
  callSites.push({ translationStr: resolvedStr, searchText, paramKeys });
263
299
  }
264
300
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "i18next-cli",
3
- "version": "1.80.7",
3
+ "version": "1.81.0",
4
4
  "description": "A unified, high-performance i18next CLI.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -1 +1 @@
1
- {"version":3,"file":"linter.d.ts","sourceRoot":"","sources":["../src/linter.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAU1C,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,EAAE,SAAS,EAA6B,MAAM,YAAY,CAAA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