i18next-cli 1.24.3 → 1.24.5

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/CHANGELOG.md CHANGED
@@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [1.24.5](https://github.com/i18next/i18next-cli/compare/v1.24.4...v1.24.5) - 2025-11-13
9
+
10
+ - improved Trans component parsing further [#102](https://github.com/i18next/i18next-cli/issues/102)
11
+
12
+ ## [1.24.4](https://github.com/i18next/i18next-cli/compare/v1.24.3...v1.24.4) - 2025-11-13
13
+
14
+ - **Location Metadata:** Fixed critical bug in location tracking where line and column numbers were consistently pointing to the end of file instead of actual usage positions. The extractor now uses text-based search instead of relying on SWC span offsets, which were accumulating across multiple files in the same process. This ensures accurate source location metadata for plugins that track where translation keys are used in the codebase. [#111](https://github.com/i18next/i18next-cli/issues/111)
15
+
8
16
  ## [1.24.3](https://github.com/i18next/i18next-cli/compare/v1.24.2...v1.24.3) - 2025-11-12
9
17
 
10
18
  - improved Trans component parsing further [#102](https://github.com/i18next/i18next-cli/issues/102)
package/dist/cjs/cli.js CHANGED
@@ -1,2 +1,2 @@
1
1
  #!/usr/bin/env node
2
- "use strict";var e=require("commander"),o=require("chokidar"),t=require("glob"),n=require("minimatch"),i=require("chalk"),r=require("./config.js"),a=require("./heuristic-config.js"),c=require("./extractor/core/extractor.js");require("node:path"),require("node:fs/promises"),require("jiti");var s=require("./types-generator.js"),l=require("./syncer.js"),u=require("./migrator.js"),d=require("./init.js"),g=require("./linter.js"),f=require("./status.js"),p=require("./locize.js"),m=require("./rename-key.js");const y=new e.Command;y.name("i18next-cli").description("A unified, high-performance i18next CLI.").version("1.24.3"),y.option("-c, --config <path>","Path to i18next-cli config file (overrides detection)"),y.command("extract").description("Extract translation keys from source files and update resource files.").option("-w, --watch","Watch for file changes and re-run the extractor.").option("--ci","Exit with a non-zero status code if any files are updated.").option("--dry-run","Run the extractor without writing any files to disk.").option("--sync-primary","Sync primary language values with default values from code.").action(async e=>{try{const t=y.opts().config,i=await r.ensureConfig(t),a=async()=>{const o=await c.runExtractor(i,{isWatchMode:!!e.watch,isDryRun:!!e.dryRun,syncPrimaryWithDefaults:!!e.syncPrimary});return e.ci&&!o?(console.log("✅ No files were updated."),process.exit(0)):e.ci&&o&&(console.error("❌ Some files were updated. This should not happen in CI mode."),process.exit(1)),o};if(await a(),e.watch){console.log("\nWatching for changes...");const e=await x(i.extract.input),t=h(i.extract.ignore),r=w(i.extract.output),c=[...t,...r].filter(Boolean),s=e.filter(e=>!c.some(o=>n.minimatch(e,o,{dot:!0})));o.watch(s,{ignored:/node_modules/,persistent:!0}).on("change",e=>{console.log(`\nFile changed: ${e}`),a()})}}catch(e){console.error("Error running extractor:",e),process.exit(1)}}),y.command("status [locale]").description("Display translation status. Provide a locale for a detailed key-by-key view.").option("-n, --namespace <ns>","Filter the status report by a specific namespace").action(async(e,o)=>{const t=y.opts().config;let n=await r.loadConfig(t);if(!n){console.log(i.blue("No config file found. Attempting to detect project structure..."));const e=await a.detectConfig();e||(console.error(i.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${i.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(i.green("Project structure detected successfully!")),n=e}await f.runStatus(n,{detail:e,namespace:o.namespace})}),y.command("types").description("Generate TypeScript definitions from translation resource files.").option("-w, --watch","Watch for file changes and re-run the type generator.").action(async e=>{const t=y.opts().config,i=await r.ensureConfig(t),a=()=>s.runTypesGenerator(i);if(await a(),e.watch){console.log("\nWatching for changes...");const e=await x(i.types?.input||[]),t=[...h(i.extract?.ignore)].filter(Boolean),r=e.filter(e=>!t.some(o=>n.minimatch(e,o,{dot:!0})));o.watch(r,{persistent:!0}).on("change",e=>{console.log(`\nFile changed: ${e}`),a()})}}),y.command("sync").description("Synchronize secondary language files with the primary language file.").action(async()=>{const e=y.opts().config,o=await r.ensureConfig(e);await l.runSyncer(o)}),y.command("migrate-config [configPath]").description("Migrate a legacy i18next-parser.config.js to the new format.").action(async e=>{await u.runMigrator(e)}),y.command("init").description("Create a new i18next.config.ts/js file with an interactive setup wizard.").action(d.runInit),y.command("lint").description("Find potential issues like hardcoded strings in your codebase.").option("-w, --watch","Watch for file changes and re-run the linter.").action(async e=>{const t=y.opts().config,c=async()=>{let e=await r.loadConfig(t);if(!e){console.log(i.blue("No config file found. Attempting to detect project structure..."));const o=await a.detectConfig();o||(console.error(i.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${i.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(i.green("Project structure detected successfully!")),e=o}await g.runLinterCli(e)};if(await c(),e.watch){console.log("\nWatching for changes...");const e=await r.loadConfig(t);if(e?.extract?.input){const t=await x(e.extract.input),i=[...h(e.extract.ignore),...w(e.extract.output)].filter(Boolean),r=t.filter(e=>!i.some(o=>n.minimatch(e,o,{dot:!0})));o.watch(r,{ignored:/node_modules/,persistent:!0}).on("change",e=>{console.log(`\nFile changed: ${e}`),c()})}}}),y.command("locize-sync").description("Synchronize local translations with your locize project.").option("--update-values","Update values of existing translations on locize.").option("--src-lng-only","Check for changes in source language only.").option("--compare-mtime","Compare modification times when syncing.").option("--dry-run","Run the command without making any changes.").action(async e=>{const o=y.opts().config,t=await r.ensureConfig(o);await p.runLocizeSync(t,e)}),y.command("locize-download").description("Download all translations from your locize project.").action(async e=>{const o=y.opts().config,t=await r.ensureConfig(o);await p.runLocizeDownload(t,e)}),y.command("locize-migrate").description("Migrate local translation files to a new locize project.").action(async e=>{const o=y.opts().config,t=await r.ensureConfig(o);await p.runLocizeMigrate(t,e)}),y.command("rename-key <oldKey> <newKey>").description("Rename a translation key across all source files and translation files.").option("--dry-run","Preview changes without modifying files").action(async(e,o,t)=>{try{const n=y.opts().config,a=await r.ensureConfig(n),c=await m.runRenameKey(a,e,o,t);c.success||(c.conflicts&&(console.error(i.red("\n❌ Conflicts detected:")),c.conflicts.forEach(e=>console.error(` - ${e}`))),c.error&&console.error(i.red(`\n❌ ${c.error}`)),process.exit(1));0===c.sourceFiles.reduce((e,o)=>e+o.changes,0)&&console.log(i.yellow(`\n⚠️ No usages found for "${e}"`))}catch(e){console.error(i.red("Error renaming key:"),e),process.exit(1)}}),y.parse(process.argv);const h=e=>Array.isArray(e)?e:e?[e]:[],w=e=>e&&"string"==typeof e?[e.replace(/\{\{[^}]+\}\}/g,"*")]:[],x=async(e=[])=>{const o=h(e),n=await Promise.all(o.map(e=>t.glob(e||"",{nodir:!0})));return Array.from(new Set(n.flat()))};
2
+ "use strict";var e=require("commander"),o=require("chokidar"),t=require("glob"),n=require("minimatch"),i=require("chalk"),r=require("./config.js"),a=require("./heuristic-config.js"),c=require("./extractor/core/extractor.js");require("node:path"),require("node:fs/promises"),require("jiti");var s=require("./types-generator.js"),l=require("./syncer.js"),u=require("./migrator.js"),d=require("./init.js"),g=require("./linter.js"),f=require("./status.js"),p=require("./locize.js"),m=require("./rename-key.js");const y=new e.Command;y.name("i18next-cli").description("A unified, high-performance i18next CLI.").version("1.24.5"),y.option("-c, --config <path>","Path to i18next-cli config file (overrides detection)"),y.command("extract").description("Extract translation keys from source files and update resource files.").option("-w, --watch","Watch for file changes and re-run the extractor.").option("--ci","Exit with a non-zero status code if any files are updated.").option("--dry-run","Run the extractor without writing any files to disk.").option("--sync-primary","Sync primary language values with default values from code.").action(async e=>{try{const t=y.opts().config,i=await r.ensureConfig(t),a=async()=>{const o=await c.runExtractor(i,{isWatchMode:!!e.watch,isDryRun:!!e.dryRun,syncPrimaryWithDefaults:!!e.syncPrimary});return e.ci&&!o?(console.log("✅ No files were updated."),process.exit(0)):e.ci&&o&&(console.error("❌ Some files were updated. This should not happen in CI mode."),process.exit(1)),o};if(await a(),e.watch){console.log("\nWatching for changes...");const e=await x(i.extract.input),t=h(i.extract.ignore),r=w(i.extract.output),c=[...t,...r].filter(Boolean),s=e.filter(e=>!c.some(o=>n.minimatch(e,o,{dot:!0})));o.watch(s,{ignored:/node_modules/,persistent:!0}).on("change",e=>{console.log(`\nFile changed: ${e}`),a()})}}catch(e){console.error("Error running extractor:",e),process.exit(1)}}),y.command("status [locale]").description("Display translation status. Provide a locale for a detailed key-by-key view.").option("-n, --namespace <ns>","Filter the status report by a specific namespace").action(async(e,o)=>{const t=y.opts().config;let n=await r.loadConfig(t);if(!n){console.log(i.blue("No config file found. Attempting to detect project structure..."));const e=await a.detectConfig();e||(console.error(i.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${i.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(i.green("Project structure detected successfully!")),n=e}await f.runStatus(n,{detail:e,namespace:o.namespace})}),y.command("types").description("Generate TypeScript definitions from translation resource files.").option("-w, --watch","Watch for file changes and re-run the type generator.").action(async e=>{const t=y.opts().config,i=await r.ensureConfig(t),a=()=>s.runTypesGenerator(i);if(await a(),e.watch){console.log("\nWatching for changes...");const e=await x(i.types?.input||[]),t=[...h(i.extract?.ignore)].filter(Boolean),r=e.filter(e=>!t.some(o=>n.minimatch(e,o,{dot:!0})));o.watch(r,{persistent:!0}).on("change",e=>{console.log(`\nFile changed: ${e}`),a()})}}),y.command("sync").description("Synchronize secondary language files with the primary language file.").action(async()=>{const e=y.opts().config,o=await r.ensureConfig(e);await l.runSyncer(o)}),y.command("migrate-config [configPath]").description("Migrate a legacy i18next-parser.config.js to the new format.").action(async e=>{await u.runMigrator(e)}),y.command("init").description("Create a new i18next.config.ts/js file with an interactive setup wizard.").action(d.runInit),y.command("lint").description("Find potential issues like hardcoded strings in your codebase.").option("-w, --watch","Watch for file changes and re-run the linter.").action(async e=>{const t=y.opts().config,c=async()=>{let e=await r.loadConfig(t);if(!e){console.log(i.blue("No config file found. Attempting to detect project structure..."));const o=await a.detectConfig();o||(console.error(i.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${i.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(i.green("Project structure detected successfully!")),e=o}await g.runLinterCli(e)};if(await c(),e.watch){console.log("\nWatching for changes...");const e=await r.loadConfig(t);if(e?.extract?.input){const t=await x(e.extract.input),i=[...h(e.extract.ignore),...w(e.extract.output)].filter(Boolean),r=t.filter(e=>!i.some(o=>n.minimatch(e,o,{dot:!0})));o.watch(r,{ignored:/node_modules/,persistent:!0}).on("change",e=>{console.log(`\nFile changed: ${e}`),c()})}}}),y.command("locize-sync").description("Synchronize local translations with your locize project.").option("--update-values","Update values of existing translations on locize.").option("--src-lng-only","Check for changes in source language only.").option("--compare-mtime","Compare modification times when syncing.").option("--dry-run","Run the command without making any changes.").action(async e=>{const o=y.opts().config,t=await r.ensureConfig(o);await p.runLocizeSync(t,e)}),y.command("locize-download").description("Download all translations from your locize project.").action(async e=>{const o=y.opts().config,t=await r.ensureConfig(o);await p.runLocizeDownload(t,e)}),y.command("locize-migrate").description("Migrate local translation files to a new locize project.").action(async e=>{const o=y.opts().config,t=await r.ensureConfig(o);await p.runLocizeMigrate(t,e)}),y.command("rename-key <oldKey> <newKey>").description("Rename a translation key across all source files and translation files.").option("--dry-run","Preview changes without modifying files").action(async(e,o,t)=>{try{const n=y.opts().config,a=await r.ensureConfig(n),c=await m.runRenameKey(a,e,o,t);c.success||(c.conflicts&&(console.error(i.red("\n❌ Conflicts detected:")),c.conflicts.forEach(e=>console.error(` - ${e}`))),c.error&&console.error(i.red(`\n❌ ${c.error}`)),process.exit(1));0===c.sourceFiles.reduce((e,o)=>e+o.changes,0)&&console.log(i.yellow(`\n⚠️ No usages found for "${e}"`))}catch(e){console.error(i.red("Error renaming key:"),e),process.exit(1)}}),y.parse(process.argv);const h=e=>Array.isArray(e)?e:e?[e]:[],w=e=>e&&"string"==typeof e?[e.replace(/\{\{[^}]+\}\}/g,"*")]:[],x=async(e=[])=>{const o=h(e),n=await Promise.all(o.map(e=>t.glob(e||"",{nodir:!0})));return Array.from(new Set(n.flat()))};
@@ -1 +1 @@
1
- "use strict";var e=require("../parsers/scope-manager.js"),r=require("../parsers/expression-resolver.js"),s=require("../parsers/call-expression-handler.js"),t=require("../parsers/jsx-handler.js");exports.ASTVisitors=class{pluginContext;config;logger;hooks;get objectKeys(){return this.callExpressionHandler.objectKeys}scopeManager;expressionResolver;callExpressionHandler;jsxHandler;currentFile="";currentCode="";constructor(o,a,i,n,l){this.pluginContext=a,this.config=o,this.logger=i,this.hooks={onBeforeVisitNode:n?.onBeforeVisitNode,onAfterVisitNode:n?.onAfterVisitNode,resolvePossibleKeyStringValues:n?.resolvePossibleKeyStringValues,resolvePossibleContextStringValues:n?.resolvePossibleContextStringValues},this.scopeManager=new e.ScopeManager(o),this.expressionResolver=l??new r.ExpressionResolver(this.hooks),this.callExpressionHandler=new s.CallExpressionHandler(o,a,i,this.expressionResolver,()=>this.getCurrentFile(),()=>this.getCurrentCode()),this.jsxHandler=new t.JSXHandler(o,a,this.expressionResolver,()=>this.getCurrentFile(),()=>this.getCurrentCode())}visit(e){this.scopeManager.reset(),this.expressionResolver.resetFileSymbols(),this.scopeManager.enterScope(),this.walk(e),this.scopeManager.exitScope()}walk(e){if(!e)return;let r=!1;switch("Function"!==e.type&&"ArrowFunctionExpression"!==e.type&&"FunctionExpression"!==e.type||(this.scopeManager.enterScope(),r=!0),this.hooks.onBeforeVisitNode?.(e),e.type){case"VariableDeclarator":this.scopeManager.handleVariableDeclarator(e),this.expressionResolver.captureVariableDeclarator(e);break;case"TSEnumDeclaration":case"TsEnumDeclaration":case"TsEnumDecl":this.expressionResolver.captureEnumDeclaration(e);break;case"CallExpression":this.callExpressionHandler.handleCallExpression(e,this.scopeManager.getVarFromScope.bind(this.scopeManager));break;case"JSXElement":this.jsxHandler.handleJSXElement(e,this.scopeManager.getVarFromScope.bind(this.scopeManager))}this.hooks.onAfterVisitNode?.(e);for(const r in e){if("span"===r)continue;const s=e[r];if(Array.isArray(s)){for(const e of s)if(e&&"object"==typeof e)if("VariableDeclarator"!==e.type){if(e&&e.id&&Array.isArray(e.members)&&this.expressionResolver.captureEnumDeclaration(e),"VariableDeclaration"===e.type&&Array.isArray(e.declarations))for(const r of e.declarations)r&&"object"==typeof r&&"VariableDeclarator"===r.type&&(this.scopeManager.handleVariableDeclarator(r),this.expressionResolver.captureVariableDeclarator(r))}else this.scopeManager.handleVariableDeclarator(e),this.expressionResolver.captureVariableDeclarator(e);for(const e of s)e&&"object"==typeof e&&this.walk(e)}else s&&"object"==typeof s&&this.walk(s)}r&&this.scopeManager.exitScope()}getVarFromScope(e){return this.scopeManager.getVarFromScope(e)}setCurrentFile(e,r){this.currentFile=e,this.currentCode=r}getCurrentFile(){return this.currentFile}getCurrentCode(){return this.currentCode}};
1
+ "use strict";var e=require("../parsers/scope-manager.js"),r=require("../parsers/expression-resolver.js"),s=require("../parsers/call-expression-handler.js"),t=require("../parsers/jsx-handler.js");exports.ASTVisitors=class{pluginContext;config;logger;hooks;get objectKeys(){return this.callExpressionHandler.objectKeys}scopeManager;expressionResolver;callExpressionHandler;jsxHandler;currentFile="";currentCode="";constructor(o,a,i,n,l){this.pluginContext=a,this.config=o,this.logger=i,this.hooks={onBeforeVisitNode:n?.onBeforeVisitNode,onAfterVisitNode:n?.onAfterVisitNode,resolvePossibleKeyStringValues:n?.resolvePossibleKeyStringValues,resolvePossibleContextStringValues:n?.resolvePossibleContextStringValues},this.scopeManager=new e.ScopeManager(o),this.expressionResolver=l??new r.ExpressionResolver(this.hooks),this.callExpressionHandler=new s.CallExpressionHandler(o,a,i,this.expressionResolver,()=>this.getCurrentFile(),()=>this.getCurrentCode()),this.jsxHandler=new t.JSXHandler(o,a,this.expressionResolver,()=>this.getCurrentFile(),()=>this.getCurrentCode())}visit(e){this.scopeManager.reset(),this.expressionResolver.resetFileSymbols(),this.scopeManager.enterScope(),this.walk(e),this.scopeManager.exitScope()}walk(e){if(!e)return;let r=!1;switch("Function"!==e.type&&"ArrowFunctionExpression"!==e.type&&"FunctionExpression"!==e.type||(this.scopeManager.enterScope(),r=!0),this.hooks.onBeforeVisitNode?.(e),e.type){case"VariableDeclarator":this.scopeManager.handleVariableDeclarator(e),this.expressionResolver.captureVariableDeclarator(e);break;case"TSEnumDeclaration":case"TsEnumDeclaration":case"TsEnumDecl":this.expressionResolver.captureEnumDeclaration(e);break;case"CallExpression":this.callExpressionHandler.handleCallExpression(e,this.scopeManager.getVarFromScope.bind(this.scopeManager));break;case"JSXElement":this.jsxHandler.handleJSXElement(e,this.scopeManager.getVarFromScope.bind(this.scopeManager))}this.hooks.onAfterVisitNode?.(e);for(const r in e){if("span"===r)continue;const s=e[r];if(Array.isArray(s)){for(const e of s)if(e&&"object"==typeof e)if("VariableDeclarator"!==e.type){if(e&&e.id&&Array.isArray(e.members)&&this.expressionResolver.captureEnumDeclaration(e),"VariableDeclaration"===e.type&&Array.isArray(e.declarations))for(const r of e.declarations)r&&"object"==typeof r&&"VariableDeclarator"===r.type&&(this.scopeManager.handleVariableDeclarator(r),this.expressionResolver.captureVariableDeclarator(r))}else this.scopeManager.handleVariableDeclarator(e),this.expressionResolver.captureVariableDeclarator(e);for(const e of s)e&&"object"==typeof e&&this.walk(e)}else s&&"object"==typeof s&&this.walk(s)}r&&this.scopeManager.exitScope()}getVarFromScope(e){return this.scopeManager.getVarFromScope(e)}setCurrentFile(e,r){this.currentFile=e,this.currentCode=r,this.callExpressionHandler.resetSearchIndex(),this.jsxHandler.resetSearchIndex()}getCurrentFile(){return this.currentFile}getCurrentCode(){return this.currentCode}};
@@ -1 +1 @@
1
- "use strict";var e=require("./ast-utils.js");exports.CallExpressionHandler=class{pluginContext;config;logger;expressionResolver;objectKeys=new Set;getCurrentFile;getCurrentCode;constructor(e,t,r,n,o,i){this.config=e,this.pluginContext=t,this.logger=r,this.expressionResolver=n,this.getCurrentFile=o,this.getCurrentCode=i}getLocationFromSpan(e){if(!e||"number"!=typeof e.start)return;const t=this.getCurrentCode(),r=e.start,n=t.substring(0,r).split("\n");return{line:n.length,column:n[n.length-1].length}}handleCallExpression(t,r){const n=this.getFunctionName(t.callee);if(!n)return;const o=r(n),i=this.config.extract.functions||["t","*.t"];let s=void 0!==o;if(!s)for(const e of i)if(e.startsWith("*.")){if(n.endsWith(e.substring(1))){s=!0;break}}else if(e===n){s=!0;break}if(!s||0===t.arguments.length)return;const{keysToProcess:l,isSelectorAPI:a}=this.handleCallExpressionArgument(t,0);if(0===l.length)return;let u=!1;const p=this.config.extract.pluralSeparator??"_";for(let e=0;e<l.length;e++)l[e].endsWith(`${p}ordinal`)&&(u=!0,l[e]=l[e].slice(0,-8));let c,f;if(t.arguments.length>1){const r=t.arguments[1].expression;"ObjectExpression"===r.type?f=r:"StringLiteral"===r.type?c=r.value:"TemplateLiteral"===r.type&&e.isSimpleTemplateLiteral(r)&&(c=r.quasis[0].cooked)}if(t.arguments.length>2){const e=t.arguments[2].expression;"ObjectExpression"===e.type&&(f=e)}const y=f?e.getObjectPropValue(f,"defaultValue"):void 0,g="string"==typeof y?y:c,h=e=>{if(!e||!Array.isArray(e.properties))return!1;for(const t of e.properties)if(t&&"KeyValueProperty"===t.type&&t.key){const e="Identifier"===t.key.type&&t.key.value||"StringLiteral"===t.key.type&&t.key.value;if("string"==typeof e&&e.startsWith("defaultValue"))return!0}return!1},d="string"==typeof g||h(f),x=h(f),k=Boolean(x||"string"==typeof g&&!("string"==typeof($=g)&&/{{\s*count\s*}}/.test($)));var $;for(let r=0;r<l.length;r++){let n,i=l[r];if(f){const t=e.getObjectPropValue(f,"ns");"string"==typeof t&&(n=t)}const s=this.config.extract.nsSeparator??":";if(!n&&s&&i.includes(s)){const e=i.split(s);if(n=e.shift(),i=e.join(s),!i||""===i.trim()){this.logger.warn(`Skipping key that became empty after namespace removal: '${n}${s}'`);continue}}!n&&o?.defaultNs&&(n=o.defaultNs),n||(n=this.config.extract.defaultNS);let p=i;if(o?.keyPrefix){const e=this.config.extract.keySeparator??".";if(p=!1!==e?o.keyPrefix.endsWith(e)?`${o.keyPrefix}${i}`:`${o.keyPrefix}${e}${i}`:`${o.keyPrefix}${i}`,!1!==e){if(p.split(e).some(e=>""===e.trim())){this.logger.warn(`Skipping key with empty segments: '${p}' (keyPrefix: '${o.keyPrefix}', key: '${i}')`);continue}}}const c=r===l.length-1&&g||i;if(f){const t=e.getObjectPropValueExpression(f,"context"),r=[];if("StringLiteral"===t?.type||"NumericLiteral"===t?.type||"BooleanLiteral"===t?.type){const e=`${t.value}`,o=this.config.extract.contextSeparator??"_";""!==e&&r.push({key:`${p}${o}${e}`,ns:n,defaultValue:c,explicitDefault:d})}else if(t){const e=this.expressionResolver.resolvePossibleContextStringValues(t),o=this.config.extract.contextSeparator??"_";e.length>0&&e.forEach(e=>{r.push({key:`${p}${o}${e}`,ns:n,defaultValue:c,explicitDefault:d})}),r.push({key:p,ns:n,defaultValue:c,explicitDefault:d,keyAcceptingContext:p})}const o=e=>{if(e){if("KeyValueProperty"===e.type&&e.key){if("Identifier"===e.key.type)return e.key.value;if("StringLiteral"===e.key.type)return e.key.value}return"KeyValueProperty"===e.type&&e.value&&"Identifier"===e.value.type?e.key&&"Identifier"===e.key.type?e.key.value:void 0:"ShorthandProperty"!==e.type&&"Identifier"!==e.type||!e.value?e.key&&"string"==typeof e.key?e.key:void 0:e.value}},i=(()=>{if(!f||!Array.isArray(f.properties))return!1;for(const e of f.properties){if("count"===o(e))return!0}return!1})(),s=(()=>{if(!f||!Array.isArray(f.properties))return!1;for(const e of f.properties){if("ordinal"===o(e))return!("KeyValueProperty"!==e.type||!e.value||"BooleanLiteral"!==e.value.type)&&Boolean(e.value.value)}return!1})();if(i||u){try{const e=u?"ordinal":"cardinal",t=this.config.extract?.primaryLanguage||(Array.isArray(this.config.locales)?this.config.locales[0]:void 0)||"en";let o=!1;try{const r=new Intl.PluralRules(t,{type:e}).resolvedOptions().pluralCategories;1===r.length&&"other"===r[0]&&(o=!0)}catch{}if(!o){const t=new Set;for(const r of this.config.locales)try{new Intl.PluralRules(r,{type:e}).resolvedOptions().pluralCategories.forEach(e=>t.add(e))}catch{new Intl.PluralRules("en",{type:e}).resolvedOptions().pluralCategories.forEach(e=>t.add(e))}const r=Array.from(t).sort();1===r.length&&"other"===r[0]&&(o=!0)}if(o){if(r.length>0)for(const e of r)this.pluginContext.addKey({key:e.key,ns:e.ns,defaultValue:e.defaultValue,hasCount:!0,isOrdinal:u});else this.pluginContext.addKey({key:p,ns:n,defaultValue:c,hasCount:!0,isOrdinal:u});continue}}catch(e){}this.config.extract.disablePlurals?r.length>0?r.forEach(this.pluginContext.addKey):this.pluginContext.addKey({key:p,ns:n,defaultValue:c,explicitDefault:d}):this.handlePluralKeys(p,n,f,s||u,g,k);continue}if(r.length>0){r.forEach(this.pluginContext.addKey);continue}!0===e.getObjectPropValue(f,"returnObjects")&&this.objectKeys.add(p)}a&&this.objectKeys.add(p);{const e=t.span?this.getLocationFromSpan(t.span):void 0;this.pluginContext.addKey({key:p,ns:n,defaultValue:c,explicitDefault:d,locations:e?[{file:this.getCurrentFile(),line:e.line,column:e.column}]:void 0})}}}handleCallExpressionArgument(e,t){const r=e.arguments[t].expression,n=[];let o=!1;if("ArrowFunctionExpression"===r.type){const e=this.extractKeyFromSelector(r);e&&(n.push(e),o=!0)}else if("ArrayExpression"===r.type)for(const e of r.elements)e?.expression&&n.push(...this.expressionResolver.resolvePossibleKeyStringValues(e.expression));else n.push(...this.expressionResolver.resolvePossibleKeyStringValues(r));return{keysToProcess:n.filter(e=>!!e),isSelectorAPI:o}}extractKeyFromSelector(e){let t=e.body;if("BlockStatement"===t.type){const e=t.stmts.find(e=>"ReturnStatement"===e.type);if("ReturnStatement"!==e?.type||!e.argument)return null;t=e.argument}let r=t;const n=[];for(;"MemberExpression"===r.type;){const e=r.property;if("Identifier"===e.type)n.unshift(e.value);else{if("Computed"!==e.type||"StringLiteral"!==e.expression.type)return null;n.unshift(e.expression.value)}r=r.object}if(n.length>0){const e=this.config.extract.keySeparator,t="string"==typeof e?e:".";return n.join(t)}return null}handlePluralKeys(t,r,n,o,i,s){try{const l=o?"ordinal":"cardinal",a=new Set;for(const e of this.config.locales)try{const t=new Intl.PluralRules(e,{type:l});t.resolvedOptions().pluralCategories.forEach(e=>a.add(e))}catch(e){const t=new Intl.PluralRules("en",{type:l});t.resolvedOptions().pluralCategories.forEach(e=>a.add(e))}const u=Array.from(a).sort(),p=this.config.extract.pluralSeparator??"_",c=e.getObjectPropValue(n,"defaultValue"),f=e.getObjectPropValue(n,`defaultValue${p}other`),y=e.getObjectPropValue(n,`defaultValue${p}ordinal${p}other`),g=e.getObjectPropValueExpression(n,"context"),h=[];if(g){const e=this.expressionResolver.resolvePossibleContextStringValues(g);if(e.length>0)if("StringLiteral"===g.type)for(const r of e)r.length>0&&h.push({key:t,context:r});else{for(const r of e)r.length>0&&h.push({key:t,context:r});!1!==this.config.extract?.generateBasePluralForms&&h.push({key:t})}else h.push({key:t})}else h.push({key:t});const d=this.config.extract?.primaryLanguage||(Array.isArray(this.config.locales)?this.config.locales[0]:void 0)||"en";let x=!1;try{const e=new Intl.PluralRules(d,{type:l}).resolvedOptions().pluralCategories;1===e.length&&"other"===e[0]&&(x=!0)}catch{x=!1}if(x||1===u.length&&"other"===u[0]){for(const{key:t,context:l}of h){const a=e.getObjectPropValue(n,`defaultValue${p}other`);let u;u="string"==typeof a?a:"string"==typeof c?c:"string"==typeof i?i:l?`${t}_${l}`:t;const f=this.config.extract.contextSeparator??"_",y=l?`${t}${f}${l}`:t;this.pluginContext.addKey({key:y,ns:r,defaultValue:u,hasCount:!0,isOrdinal:o,explicitDefault:Boolean(s||"string"==typeof a)})}return}for(const{key:l,context:a}of h)for(const g of u){const u=o?`defaultValue${p}ordinal${p}${g}`:`defaultValue${p}${g}`,h=e.getObjectPropValue(n,u);let d,x;if(d="string"==typeof h?h:"one"===g&&"string"==typeof c?c:"one"===g&&"string"==typeof i?i:o&&"string"==typeof y?y:o||"string"!=typeof f?"string"==typeof c?c:"string"==typeof i?i:l:f,a){const e=this.config.extract.contextSeparator??"_";x=o?`${l}${e}${a}${p}ordinal${p}${g}`:`${l}${e}${a}${p}${g}`}else x=o?`${l}${p}ordinal${p}${g}`:`${l}${p}${g}`;this.pluginContext.addKey({key:x,ns:r,defaultValue:d,hasCount:!0,isOrdinal:o,explicitDefault:Boolean(s||"string"==typeof h||"string"==typeof f),keyAcceptingContext:void 0!==a?t:void 0})}}catch(o){this.logger.warn(`Could not determine plural rules for language "${this.config.extract?.primaryLanguage}". Falling back to simple key extraction.`);const s=i||e.getObjectPropValue(n,"defaultValue");this.pluginContext.addKey({key:t,ns:r,defaultValue:"string"==typeof s?s:t})}}getFunctionName(e){if("Identifier"===e.type)return e.value;if("MemberExpression"===e.type){const t=[];let r=e;for(;"MemberExpression"===r.type;){if("Identifier"!==r.property.type)return null;t.unshift(r.property.value),r=r.object}if("ThisExpression"===r.type)t.unshift("this");else{if("Identifier"!==r.type)return null;t.unshift(r.value)}return t.join(".")}return null}};
1
+ "use strict";var e=require("./ast-utils.js");exports.CallExpressionHandler=class{pluginContext;config;logger;expressionResolver;objectKeys=new Set;getCurrentFile;getCurrentCode;lastSearchIndex=0;constructor(e,t,r,n,s,i){this.config=e,this.pluginContext=t,this.logger=r,this.expressionResolver=n,this.getCurrentFile=s,this.getCurrentCode=i}resetSearchIndex(){this.lastSearchIndex=0}getLocationFromNode(e){const t=this.getCurrentCode();let r;if("CallExpression"===e.type&&e.arguments.length>0){const t=e.arguments[0].expression;"StringLiteral"===t.type?r=t.raw??`'${t.value}'`:"TemplateLiteral"===t.type&&(r="`")}if(!r)return;const n=t.indexOf(r,this.lastSearchIndex);if(-1===n)return;this.lastSearchIndex=n+r.length;const s=t.substring(0,n).split("\n");return{line:s.length,column:s[s.length-1].length}}handleCallExpression(t,r){const n=this.getFunctionName(t.callee);if(!n)return;const s=r(n),i=this.config.extract.functions||["t","*.t"];let o=void 0!==s;if(!o)for(const e of i)if(e.startsWith("*.")){if(n.endsWith(e.substring(1))){o=!0;break}}else if(e===n){o=!0;break}if(!o||0===t.arguments.length)return;const{keysToProcess:l,isSelectorAPI:a}=this.handleCallExpressionArgument(t,0);if(0===l.length)return;let u=!1;const c=this.config.extract.pluralSeparator??"_";for(let e=0;e<l.length;e++)l[e].endsWith(`${c}ordinal`)&&(u=!0,l[e]=l[e].slice(0,-8));let p,f;if(t.arguments.length>1){const r=t.arguments[1].expression;"ObjectExpression"===r.type?f=r:"StringLiteral"===r.type?p=r.value:"TemplateLiteral"===r.type&&e.isSimpleTemplateLiteral(r)&&(p=r.quasis[0].cooked)}if(t.arguments.length>2){const e=t.arguments[2].expression;"ObjectExpression"===e.type&&(f=e)}const y=f?e.getObjectPropValue(f,"defaultValue"):void 0,g="string"==typeof y?y:p,h=e=>{if(!e||!Array.isArray(e.properties))return!1;for(const t of e.properties)if(t&&"KeyValueProperty"===t.type&&t.key){const e="Identifier"===t.key.type&&t.key.value||"StringLiteral"===t.key.type&&t.key.value;if("string"==typeof e&&e.startsWith("defaultValue"))return!0}return!1},d="string"==typeof g||h(f),x=h(f),k=Boolean(x||"string"==typeof g&&!("string"==typeof($=g)&&/{{\s*count\s*}}/.test($)));var $;for(let r=0;r<l.length;r++){let n,i=l[r];if(f){const t=e.getObjectPropValue(f,"ns");"string"==typeof t&&(n=t)}const o=this.config.extract.nsSeparator??":";if(!n&&o&&i.includes(o)){const e=i.split(o);if(n=e.shift(),i=e.join(o),!i||""===i.trim()){this.logger.warn(`Skipping key that became empty after namespace removal: '${n}${o}'`);continue}}!n&&s?.defaultNs&&(n=s.defaultNs),n||(n=this.config.extract.defaultNS);let c=i;if(s?.keyPrefix){const e=this.config.extract.keySeparator??".";if(c=!1!==e?s.keyPrefix.endsWith(e)?`${s.keyPrefix}${i}`:`${s.keyPrefix}${e}${i}`:`${s.keyPrefix}${i}`,!1!==e){if(c.split(e).some(e=>""===e.trim())){this.logger.warn(`Skipping key with empty segments: '${c}' (keyPrefix: '${s.keyPrefix}', key: '${i}')`);continue}}}const p=r===l.length-1&&g||i;if(f){const t=e.getObjectPropValueExpression(f,"context"),r=[];if("StringLiteral"===t?.type||"NumericLiteral"===t?.type||"BooleanLiteral"===t?.type){const e=`${t.value}`,s=this.config.extract.contextSeparator??"_";""!==e&&r.push({key:`${c}${s}${e}`,ns:n,defaultValue:p,explicitDefault:d})}else if(t){const e=this.expressionResolver.resolvePossibleContextStringValues(t),s=this.config.extract.contextSeparator??"_";e.length>0&&e.forEach(e=>{r.push({key:`${c}${s}${e}`,ns:n,defaultValue:p,explicitDefault:d})}),r.push({key:c,ns:n,defaultValue:p,explicitDefault:d,keyAcceptingContext:c})}const s=e=>{if(e){if("KeyValueProperty"===e.type&&e.key){if("Identifier"===e.key.type)return e.key.value;if("StringLiteral"===e.key.type)return e.key.value}return"KeyValueProperty"===e.type&&e.value&&"Identifier"===e.value.type?e.key&&"Identifier"===e.key.type?e.key.value:void 0:"ShorthandProperty"!==e.type&&"Identifier"!==e.type||!e.value?e.key&&"string"==typeof e.key?e.key:void 0:e.value}},i=(()=>{if(!f||!Array.isArray(f.properties))return!1;for(const e of f.properties){if("count"===s(e))return!0}return!1})(),o=(()=>{if(!f||!Array.isArray(f.properties))return!1;for(const e of f.properties){if("ordinal"===s(e))return!("KeyValueProperty"!==e.type||!e.value||"BooleanLiteral"!==e.value.type)&&Boolean(e.value.value)}return!1})();if(i||u){try{const e=u?"ordinal":"cardinal",t=this.config.extract?.primaryLanguage||(Array.isArray(this.config.locales)?this.config.locales[0]:void 0)||"en";let s=!1;try{const r=new Intl.PluralRules(t,{type:e}).resolvedOptions().pluralCategories;1===r.length&&"other"===r[0]&&(s=!0)}catch{}if(!s){const t=new Set;for(const r of this.config.locales)try{new Intl.PluralRules(r,{type:e}).resolvedOptions().pluralCategories.forEach(e=>t.add(e))}catch{new Intl.PluralRules("en",{type:e}).resolvedOptions().pluralCategories.forEach(e=>t.add(e))}const r=Array.from(t).sort();1===r.length&&"other"===r[0]&&(s=!0)}if(s){if(r.length>0)for(const e of r)this.pluginContext.addKey({key:e.key,ns:e.ns,defaultValue:e.defaultValue,hasCount:!0,isOrdinal:u});else this.pluginContext.addKey({key:c,ns:n,defaultValue:p,hasCount:!0,isOrdinal:u});continue}}catch(e){}this.config.extract.disablePlurals?r.length>0?r.forEach(this.pluginContext.addKey):this.pluginContext.addKey({key:c,ns:n,defaultValue:p,explicitDefault:d}):this.handlePluralKeys(c,n,f,o||u,g,k);continue}if(r.length>0){r.forEach(this.pluginContext.addKey);continue}!0===e.getObjectPropValue(f,"returnObjects")&&this.objectKeys.add(c)}a&&this.objectKeys.add(c);{const e=this.getLocationFromNode(t);this.pluginContext.addKey({key:c,ns:n,defaultValue:p,explicitDefault:d,locations:e?[{file:this.getCurrentFile(),line:e.line,column:e.column}]:void 0})}}}handleCallExpressionArgument(e,t){const r=e.arguments[t].expression,n=[];let s=!1;if("ArrowFunctionExpression"===r.type){const e=this.extractKeyFromSelector(r);e&&(n.push(e),s=!0)}else if("ArrayExpression"===r.type)for(const e of r.elements)e?.expression&&n.push(...this.expressionResolver.resolvePossibleKeyStringValues(e.expression));else n.push(...this.expressionResolver.resolvePossibleKeyStringValues(r));return{keysToProcess:n.filter(e=>!!e),isSelectorAPI:s}}extractKeyFromSelector(e){let t=e.body;if("BlockStatement"===t.type){const e=t.stmts.find(e=>"ReturnStatement"===e.type);if("ReturnStatement"!==e?.type||!e.argument)return null;t=e.argument}let r=t;const n=[];for(;"MemberExpression"===r.type;){const e=r.property;if("Identifier"===e.type)n.unshift(e.value);else{if("Computed"!==e.type||"StringLiteral"!==e.expression.type)return null;n.unshift(e.expression.value)}r=r.object}if(n.length>0){const e=this.config.extract.keySeparator,t="string"==typeof e?e:".";return n.join(t)}return null}handlePluralKeys(t,r,n,s,i,o){try{const l=s?"ordinal":"cardinal",a=new Set;for(const e of this.config.locales)try{const t=new Intl.PluralRules(e,{type:l});t.resolvedOptions().pluralCategories.forEach(e=>a.add(e))}catch(e){const t=new Intl.PluralRules("en",{type:l});t.resolvedOptions().pluralCategories.forEach(e=>a.add(e))}const u=Array.from(a).sort(),c=this.config.extract.pluralSeparator??"_",p=e.getObjectPropValue(n,"defaultValue"),f=e.getObjectPropValue(n,`defaultValue${c}other`),y=e.getObjectPropValue(n,`defaultValue${c}ordinal${c}other`),g=e.getObjectPropValueExpression(n,"context"),h=[];if(g){const e=this.expressionResolver.resolvePossibleContextStringValues(g);if(e.length>0)if("StringLiteral"===g.type)for(const r of e)r.length>0&&h.push({key:t,context:r});else{for(const r of e)r.length>0&&h.push({key:t,context:r});!1!==this.config.extract?.generateBasePluralForms&&h.push({key:t})}else h.push({key:t})}else h.push({key:t});const d=this.config.extract?.primaryLanguage||(Array.isArray(this.config.locales)?this.config.locales[0]:void 0)||"en";let x=!1;try{const e=new Intl.PluralRules(d,{type:l}).resolvedOptions().pluralCategories;1===e.length&&"other"===e[0]&&(x=!0)}catch{x=!1}if(x||1===u.length&&"other"===u[0]){for(const{key:t,context:l}of h){const a=e.getObjectPropValue(n,`defaultValue${c}other`);let u;u="string"==typeof a?a:"string"==typeof p?p:"string"==typeof i?i:l?`${t}_${l}`:t;const f=this.config.extract.contextSeparator??"_",y=l?`${t}${f}${l}`:t;this.pluginContext.addKey({key:y,ns:r,defaultValue:u,hasCount:!0,isOrdinal:s,explicitDefault:Boolean(o||"string"==typeof a)})}return}for(const{key:l,context:a}of h)for(const g of u){const u=s?`defaultValue${c}ordinal${c}${g}`:`defaultValue${c}${g}`,h=e.getObjectPropValue(n,u);let d,x;if(d="string"==typeof h?h:"one"===g&&"string"==typeof p?p:"one"===g&&"string"==typeof i?i:s&&"string"==typeof y?y:s||"string"!=typeof f?"string"==typeof p?p:"string"==typeof i?i:l:f,a){const e=this.config.extract.contextSeparator??"_";x=s?`${l}${e}${a}${c}ordinal${c}${g}`:`${l}${e}${a}${c}${g}`}else x=s?`${l}${c}ordinal${c}${g}`:`${l}${c}${g}`;this.pluginContext.addKey({key:x,ns:r,defaultValue:d,hasCount:!0,isOrdinal:s,explicitDefault:Boolean(o||"string"==typeof h||"string"==typeof f),keyAcceptingContext:void 0!==a?t:void 0})}}catch(s){this.logger.warn(`Could not determine plural rules for language "${this.config.extract?.primaryLanguage}". Falling back to simple key extraction.`);const o=i||e.getObjectPropValue(n,"defaultValue");this.pluginContext.addKey({key:t,ns:r,defaultValue:"string"==typeof o?o:t})}}getFunctionName(e){if("Identifier"===e.type)return e.value;if("MemberExpression"===e.type){const t=[];let r=e;for(;"MemberExpression"===r.type;){if("Identifier"!==r.property.type)return null;t.unshift(r.property.value),r=r.object}if("ThisExpression"===r.type)t.unshift("this");else{if("Identifier"!==r.type)return null;t.unshift(r.value)}return t.join(".")}return null}};
@@ -1 +1 @@
1
- "use strict";var e=require("./jsx-parser.js"),t=require("./ast-utils.js");exports.JSXHandler=class{config;pluginContext;expressionResolver;getCurrentFile;getCurrentCode;constructor(e,t,n,o,s){this.config=e,this.pluginContext=t,this.expressionResolver=n,this.getCurrentFile=o,this.getCurrentCode=s}getLocationFromSpan(e){if(!e||"number"!=typeof e.start)return;const t=this.getCurrentCode(),n=e.start,o=t.substring(0,n).split("\n");return{line:o.length,column:o[o.length-1].length}}handleJSXElement(t,n){const o=this.getElementName(t);if(o&&(this.config.extract.transComponents||["Trans"]).includes(o)){const o=e.extractFromTransComponent(t,this.config),s=[];if(o){if(o.keyExpression){const e=this.expressionResolver.resolvePossibleKeyStringValues(o.keyExpression);s.push(...e)}else s.push(o.serializedChildren);let e;const{contextExpression:i,optionsNode:a,defaultValue:l,hasCount:r,isOrdinal:u,serializedChildren:c}=o,f=t.span?this.getLocationFromSpan(t.span):void 0,p=f?[{file:this.getCurrentFile(),line:f.line,column:f.column}]:void 0;if(o.ns){const{ns:t}=o;e=s.map(e=>({key:e,ns:t,defaultValue:l||c,hasCount:r,isOrdinal:u,locations:p}))}else{e=s.map(e=>{const t=this.config.extract.nsSeparator??":";let n;if(t&&e.includes(t)){let o;[n,...o]=e.split(t),e=o.join(t)}return{key:e,ns:n,defaultValue:l||c,hasCount:r,isOrdinal:u,explicitDefault:o.explicitDefault,locations:p}});const i=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"t"===e.name.value);if("JSXAttribute"===i?.type&&"JSXExpressionContainer"===i.value?.type&&"Identifier"===i.value.expression.type){const t=n(i.value.expression.value);t?.defaultNs&&e.forEach(e=>{e.ns||(e.ns=t.defaultNs)})}}if(e.forEach(e=>{e.ns||(e.ns=this.config.extract.defaultNS)}),i&&r)if(this.config.extract.disablePlurals){const t=this.expressionResolver.resolvePossibleContextStringValues(i),n=this.config.extract.contextSeparator??"_";if(t.length>0)if("StringLiteral"===i.type)for(const o of t)for(const t of e){const e=`${t.key}${n}${o}`;this.pluginContext.addKey({key:e,ns:t.ns,defaultValue:t.defaultValue,locations:t.locations})}else{e.forEach(e=>{this.pluginContext.addKey({key:e.key,ns:e.ns,defaultValue:e.defaultValue,locations:e.locations,keyAcceptingContext:e.key})});for(const o of t)for(const t of e){const e=`${t.key}${n}${o}`;this.pluginContext.addKey({key:e,ns:t.ns,defaultValue:t.defaultValue,locations:t.locations})}}else e.forEach(e=>{this.pluginContext.addKey({key:e.key,ns:e.ns,defaultValue:e.defaultValue,locations:e.locations,keyAcceptingContext:e.key})})}else{const n=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),o=!!n,s=this.expressionResolver.resolvePossibleContextStringValues(i),l=this.config.extract.contextSeparator??"_";if(s.length>0){e.forEach(e=>this.generatePluralKeysForTrans(e.key,e.defaultValue,e.ns,o,a,void 0,e.locations,e.key));for(const t of s)for(const n of e){const e=`${n.key}${l}${t}`;this.generatePluralKeysForTrans(e,n.defaultValue,n.ns,o,a,n.explicitDefault,n.locations,n.key)}}else e.forEach(e=>this.generatePluralKeysForTrans(e.key,e.defaultValue,e.ns,o,a,e.explicitDefault,e.locations))}else if(i){const t=this.expressionResolver.resolvePossibleContextStringValues(i),n=this.config.extract.contextSeparator??"_";if(t.length>0){for(const o of t)for(const{key:t,ns:s,defaultValue:i,locations:a}of e)this.pluginContext.addKey({key:`${t}${n}${o}`,ns:s,defaultValue:i,locations:a});"StringLiteral"!==i.type&&e.forEach(e=>{this.pluginContext.addKey({key:e.key,ns:e.ns,defaultValue:e.defaultValue,locations:e.locations,keyAcceptingContext:e.key})})}else e.forEach(e=>{this.pluginContext.addKey({key:e.key,ns:e.ns,defaultValue:e.defaultValue,locations:e.locations,keyAcceptingContext:e.key})})}else if(r)if(this.config.extract.disablePlurals)e.forEach(e=>{this.pluginContext.addKey({key:e.key,ns:e.ns,defaultValue:e.defaultValue,locations:e.locations})});else{const n=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),o=!!n;e.forEach(e=>this.generatePluralKeysForTrans(e.key,e.defaultValue,e.ns,o,a,e.explicitDefault,e.locations))}else e.forEach(e=>{this.pluginContext.addKey({key:e.key,ns:e.ns,defaultValue:e.defaultValue,locations:e.locations})})}}}generatePluralKeysForTrans(e,n,o,s,i,a,l,r){try{const u=s?"ordinal":"cardinal",c=new Intl.PluralRules(this.config.extract?.primaryLanguage,{type:u}).resolvedOptions().pluralCategories,f=this.config.extract.pluralSeparator??"_";let p,d;if(i&&(p=t.getObjectPropValue(i,`defaultValue${f}other`),d=t.getObjectPropValue(i,`defaultValue${f}ordinal${f}other`)),1===c.length&&"other"===c[0]){const u=i?t.getObjectPropValue(i,`defaultValue${f}other`):void 0,c="string"==typeof u?u:"string"==typeof n?n:e;return void this.pluginContext.addKey({key:e,ns:o,defaultValue:c,hasCount:!0,isOrdinal:s,explicitDefault:Boolean(a||"string"==typeof u||"string"==typeof p),locations:l,keyAcceptingContext:r})}for(const u of c){const c=s?`defaultValue${f}ordinal${f}${u}`:`defaultValue${f}${u}`,y=i?t.getObjectPropValue(i,c):void 0;let g;g="string"==typeof y?y:"one"===u&&"string"==typeof n?n:s&&"string"==typeof d?d:s||"string"!=typeof p?"string"==typeof n?n:e:p;const h=s?`${e}${f}ordinal${f}${u}`:`${e}${f}${u}`;this.pluginContext.addKey({key:h,ns:o,defaultValue:g,hasCount:!0,isOrdinal:s,explicitDefault:Boolean(a||"string"==typeof y||"string"==typeof p),locations:l,keyAcceptingContext:r})}}catch(t){this.pluginContext.addKey({key:e,ns:o,defaultValue:n,locations:l})}}getElementName(e){if("Identifier"===e.opening.name.type)return e.opening.name.value;if("JSXMemberExpression"===e.opening.name.type){let t=e.opening.name;const n=[];for(;"JSXMemberExpression"===t.type;)"Identifier"===t.property.type&&n.unshift(t.property.value),t=t.object;return"Identifier"===t.type&&n.unshift(t.value),n.join(".")}}};
1
+ "use strict";var e=require("./jsx-parser.js"),t=require("./ast-utils.js");exports.JSXHandler=class{config;pluginContext;expressionResolver;getCurrentFile;getCurrentCode;lastSearchIndex=0;constructor(e,t,n,o,s){this.config=e,this.pluginContext=t,this.expressionResolver=n,this.getCurrentFile=o,this.getCurrentCode=s}resetSearchIndex(){this.lastSearchIndex=0}getLocationFromNode(e){const t=this.getCurrentCode();let n;if("JSXElement"===e.type&&e.opening){const t=e.opening.name?.value;t&&(n=`<${t}`)}if(!n)return;const o=t.indexOf(n,this.lastSearchIndex);if(-1===o)return;this.lastSearchIndex=o+n.length;const s=t.substring(0,o).split("\n");return{line:s.length,column:s[s.length-1].length}}handleJSXElement(t,n){const o=this.getElementName(t);if(o&&(this.config.extract.transComponents||["Trans"]).includes(o)){const o=e.extractFromTransComponent(t,this.config),s=[];if(o){if(o.keyExpression){const e=this.expressionResolver.resolvePossibleKeyStringValues(o.keyExpression);s.push(...e)}else s.push(o.serializedChildren);let e;const{contextExpression:i,optionsNode:a,defaultValue:l,hasCount:r,isOrdinal:u,serializedChildren:c}=o,f=this.getLocationFromNode(t),d=f?[{file:this.getCurrentFile(),line:f.line,column:f.column}]:void 0;if(o.ns){const{ns:t}=o;e=s.map(e=>({key:e,ns:t,defaultValue:l||c,hasCount:r,isOrdinal:u,locations:d}))}else{e=s.map(e=>{const t=this.config.extract.nsSeparator??":";let n;if(t&&e.includes(t)){let o;[n,...o]=e.split(t),e=o.join(t)}return{key:e,ns:n,defaultValue:l||c,hasCount:r,isOrdinal:u,explicitDefault:o.explicitDefault,locations:d}});const i=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"t"===e.name.value);if("JSXAttribute"===i?.type&&"JSXExpressionContainer"===i.value?.type&&"Identifier"===i.value.expression.type){const t=n(i.value.expression.value);t?.defaultNs&&e.forEach(e=>{e.ns||(e.ns=t.defaultNs)})}}if(e.forEach(e=>{e.ns||(e.ns=this.config.extract.defaultNS)}),i&&r)if(this.config.extract.disablePlurals){const t=this.expressionResolver.resolvePossibleContextStringValues(i),n=this.config.extract.contextSeparator??"_";if(t.length>0)if("StringLiteral"===i.type)for(const o of t)for(const t of e){const e=`${t.key}${n}${o}`;this.pluginContext.addKey({key:e,ns:t.ns,defaultValue:t.defaultValue,locations:t.locations})}else{e.forEach(e=>{this.pluginContext.addKey({key:e.key,ns:e.ns,defaultValue:e.defaultValue,locations:e.locations,keyAcceptingContext:e.key})});for(const o of t)for(const t of e){const e=`${t.key}${n}${o}`;this.pluginContext.addKey({key:e,ns:t.ns,defaultValue:t.defaultValue,locations:t.locations})}}else e.forEach(e=>{this.pluginContext.addKey({key:e.key,ns:e.ns,defaultValue:e.defaultValue,locations:e.locations,keyAcceptingContext:e.key})})}else{const n=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),o=!!n,s=this.expressionResolver.resolvePossibleContextStringValues(i),l=this.config.extract.contextSeparator??"_";if(s.length>0){e.forEach(e=>this.generatePluralKeysForTrans(e.key,e.defaultValue,e.ns,o,a,void 0,e.locations,e.key));for(const t of s)for(const n of e){const e=`${n.key}${l}${t}`;this.generatePluralKeysForTrans(e,n.defaultValue,n.ns,o,a,n.explicitDefault,n.locations,n.key)}}else e.forEach(e=>this.generatePluralKeysForTrans(e.key,e.defaultValue,e.ns,o,a,e.explicitDefault,e.locations))}else if(i){const t=this.expressionResolver.resolvePossibleContextStringValues(i),n=this.config.extract.contextSeparator??"_";if(t.length>0){for(const o of t)for(const{key:t,ns:s,defaultValue:i,locations:a}of e)this.pluginContext.addKey({key:`${t}${n}${o}`,ns:s,defaultValue:i,locations:a});"StringLiteral"!==i.type&&e.forEach(e=>{this.pluginContext.addKey({key:e.key,ns:e.ns,defaultValue:e.defaultValue,locations:e.locations,keyAcceptingContext:e.key})})}else e.forEach(e=>{this.pluginContext.addKey({key:e.key,ns:e.ns,defaultValue:e.defaultValue,locations:e.locations,keyAcceptingContext:e.key})})}else if(r)if(this.config.extract.disablePlurals)e.forEach(e=>{this.pluginContext.addKey({key:e.key,ns:e.ns,defaultValue:e.defaultValue,locations:e.locations})});else{const n=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),o=!!n;e.forEach(e=>this.generatePluralKeysForTrans(e.key,e.defaultValue,e.ns,o,a,e.explicitDefault,e.locations))}else e.forEach(e=>{this.pluginContext.addKey({key:e.key,ns:e.ns,defaultValue:e.defaultValue,locations:e.locations})})}}}generatePluralKeysForTrans(e,n,o,s,i,a,l,r){try{const u=s?"ordinal":"cardinal",c=new Intl.PluralRules(this.config.extract?.primaryLanguage,{type:u}).resolvedOptions().pluralCategories,f=this.config.extract.pluralSeparator??"_";let d,p;if(i&&(d=t.getObjectPropValue(i,`defaultValue${f}other`),p=t.getObjectPropValue(i,`defaultValue${f}ordinal${f}other`)),1===c.length&&"other"===c[0]){const u=i?t.getObjectPropValue(i,`defaultValue${f}other`):void 0,c="string"==typeof u?u:"string"==typeof n?n:e;return void this.pluginContext.addKey({key:e,ns:o,defaultValue:c,hasCount:!0,isOrdinal:s,explicitDefault:Boolean(a||"string"==typeof u||"string"==typeof d),locations:l,keyAcceptingContext:r})}for(const u of c){const c=s?`defaultValue${f}ordinal${f}${u}`:`defaultValue${f}${u}`,y=i?t.getObjectPropValue(i,c):void 0;let g;g="string"==typeof y?y:"one"===u&&"string"==typeof n?n:s&&"string"==typeof p?p:s||"string"!=typeof d?"string"==typeof n?n:e:d;const h=s?`${e}${f}ordinal${f}${u}`:`${e}${f}${u}`;this.pluginContext.addKey({key:h,ns:o,defaultValue:g,hasCount:!0,isOrdinal:s,explicitDefault:Boolean(a||"string"==typeof y||"string"==typeof d),locations:l,keyAcceptingContext:r})}}catch(t){this.pluginContext.addKey({key:e,ns:o,defaultValue:n,locations:l})}}getElementName(e){if("Identifier"===e.opening.name.type)return e.opening.name.value;if("JSXMemberExpression"===e.opening.name.type){let t=e.opening.name;const n=[];for(;"JSXMemberExpression"===t.type;)"Identifier"===t.property.type&&n.unshift(t.property.value),t=t.object;return"Identifier"===t.type&&n.unshift(t.value),n.join(".")}}};
@@ -1 +1 @@
1
- "use strict";var e=require("./ast-utils.js");function t(t){if(t)return"StringLiteral"===t.type?t.value:"TemplateLiteral"===t.type&&e.isSimpleTemplateLiteral(t)?t.quasis[0].cooked:void 0}function n(e){return"StringLiteral"===e.value?.type?e.value.value:"JSXExpressionContainer"===e.value?.type?t(e.value.expression):void 0}exports.extractFromTransComponent=function(i,s){const r=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"i18nKey"===e.name.value),o=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"defaults"===e.name.value),p=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"count"===e.name.value),a=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"values"===e.name.value);let l;p||"JSXAttribute"!==a?.type||"JSXExpressionContainer"!==a.value?.type||"ObjectExpression"!==a.value.expression.type||(l=e.getObjectPropValueExpression(a.value.expression,"count"));const u=!!p||!!l,c=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"tOptions"===e.name.value),y="JSXAttribute"===c?.type&&"JSXExpressionContainer"===c.value?.type&&"ObjectExpression"===c.value.expression.type?c.value.expression:void 0,f=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),g=!!f,d=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"context"===e.name.value);let v="JSXAttribute"===d?.type&&"JSXExpressionContainer"===d.value?.type?d.value.expression:"JSXAttribute"===d?.type&&"StringLiteral"===d.value?.type?d.value:void 0;const S=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ns"===e.name.value);let x;x="JSXAttribute"===S?.type?n(S):void 0,y&&(void 0===x&&(x=e.getObjectPropValue(y,"ns")),void 0===v&&(v=e.getObjectPropValueExpression(y,"context")));const m=function(e,n){if(!e||0===e.length)return"";const i=new Set(n.extract.transKeepBasicHtmlNodesFor??["br","strong","i","p"]),s=e=>e&&"JSXText"===e.type&&/^\s*$/.test(e.value)&&e.value.includes("\n");function r(e,n,o=!1,p=!1){if(!e||!e.length)return;const a=p&&e.filter(e=>e&&"JSXElement"===e.type&&"p"===e.opening?.name?.value).length>1;let l=0,u=e.length-1;for(;l<=u&&s(e[l]);)l++;for(;u>=l&&s(e[u]);)u--;const c=l<=u?e.slice(l,u+1):[],y=c.some(e=>e&&("JSXElement"===e.type||"JSXFragment"===e.type));for(let e=0;e<c.length;e++){const p=c[e];if(p)if("JSXText"!==p.type){if("JSXExpressionContainer"===p.type){if(o&&!y&&p.expression){const e=p.expression.type;if("ObjectExpression"===e){const e=p.expression.properties&&p.expression.properties[0];if(e&&"KeyValueProperty"===e.type)continue}const n=t(p.expression);if(void 0!==n){if(!(/^\s*$/.test(n)&&!n.includes("\n")))continue}else if("Identifier"===e||"MemberExpression"===e||"CallExpression"===e)continue}const i=t(p.expression);if(void 0!==i){const t=/^\s*$/.test(i)&&!i.includes("\n"),r=c[e-1],o=c[e+1];if(t){const t=c[e+2];if(o&&"JSXText"===o.type&&s(o)&&t&&("JSXElement"===t.type||"JSXFragment"===t.type)){const t=c[e-1],n=c[e-2];if(!t||"JSXText"!==t.type&&n&&"JSXExpressionContainer"===n.type)continue}if(r&&("JSXElement"===r.type||"JSXFragment"===r.type)&&o&&"JSXText"===o.type&&s(o))continue;const i=!o||"JSXText"===o.type&&!s(o);if(r&&"JSXText"===r.type&&i){const e=n[n.length-1];if(e&&"JSXText"===e.type){e.value=String(e.value)+p.expression.value;continue}}if(r&&("JSXElement"===r.type||"JSXFragment"===r.type)&&o&&"JSXText"===o.type&&s(o))continue}}n.push(p);continue}if("JSXElement"===p.type){const e=p.opening&&p.opening.name&&"Identifier"===p.opening.name.type?p.opening.name.value:void 0;if(e&&i.has(e)){const i=p.opening&&Array.isArray(p.opening.attributes)&&p.opening.attributes.length>0,s=p.children||[],o=1===s.length&&("JSXText"===s[0]?.type||"JSXExpressionContainer"===s[0]?.type&&void 0!==t(s[0].expression)),l=!s.length,u=o;i&&!o?(n.push(p),r(p.children||[],n,!0)):l?n.push(p):u||("p"===e&&a?(n.push(p),r(p.children||[],n,!0,!1)):r(p.children||[],n,!1,!1));continue}n.push(p),r(p.children||[],n,!0);continue}"JSXFragment"!==p.type||r(p.children||[],n,o)}else{if(o&&!y)continue;if(o&&s(p))continue;if(s(p)){const t=c[e-1],i=c[e+1];if(t&&("JSXElement"===t.type||"JSXFragment"===t.type)&&i&&("JSXElement"===i.type||"JSXFragment"===i.type))continue;const s=n[n.length-1],r=c[e-1];if(s){if(r&&"JSXExpressionContainer"===r.type)continue;if("JSXText"===s.type&&r&&"JSXText"===r.type){s.value=String(s.value)+p.value;continue}}}if(o&&y&&0===e)continue;n.push(p)}}}const o=[];r(e,o,!1,!0);const p=e=>String(e).replace(/^\s*\n\s*/g,"").replace(/\s*\n\s*$/g,"");function a(e,n,r=!1){if(!e||0===e.length)return"";let l="",u=0;for(let c=0;c<e.length;c++){const y=e[c];if(y){if("JSXText"===y.type){if(s(y))continue;const t=e[c+1],n=e[c-1];if(n&&"JSXElement"===n.type){const e="Identifier"===n.opening?.name?.type?n.opening.name.value:void 0,t=e&&i.has(e),s=0===(n.children||[]).length;if(t&&s&&/^\s*\n\s*/.test(y.value)){const e=y.value.replace(/^\s*\n\s*/,"");if(e){l+=e;continue}continue}}if(/\n\s*$/.test(y.value)&&t&&"JSXElement"===t.type){const n=y.value.replace(/\n\s*$/,"");if(n.trim()){const r="Identifier"===t.opening?.name?.type?t.opening.name.value:void 0,o=r&&i.has(r),p=(t.children||[]).length>0,a=/\s\n/.test(y.value),u=e[c+2],f=u&&"JSXText"===u.type&&!s(u)&&/[a-zA-Z0-9]/.test(u.value),g=/^\s/.test(n),d=n.trim(),v=g?" "+d:d;l+=a||o&&p||!o&&f?v+" ":v;continue}}l+=y.value;continue}if("JSXExpressionContainer"===y.type){const e=y.expression;if(!e)continue;const n=t(e);if(void 0!==n)l+=n;else if("Identifier"===e.type)l+=`{{${e.value}}}`;else if("ObjectExpression"===e.type){const t=e.properties[0];t&&"KeyValueProperty"===t.type&&t.key&&"Identifier"===t.key.type?l+=`{{${t.key.value}}}`:t&&"Identifier"===t.type?l+=`{{${t.value}}}`:l+="{{value}}"}else"MemberExpression"===e.type&&e.property&&"Identifier"===e.property.type?l+=`{{${e.property.value}}}`:"CallExpression"===e.type&&"Identifier"===e.callee?.type?l+=`{{${e.callee.value}}}`:l+="{{value}}";continue}if("JSXElement"===y.type){let f;y.opening&&y.opening.name&&"Identifier"===y.opening.name.type&&(f=y.opening.name.value);const g=r?u:void 0;if(r&&"JSXElement"===y.type&&u++,f&&i.has(f)){const r=y.opening&&Array.isArray(y.opening.attributes)&&y.opening.attributes.length>0,u=y.children||[],d=u.length>0,v=1===u.length&&("JSXText"===u[0]?.type||"JSXExpressionContainer"===u[0]?.type&&void 0!==t(u[0].expression));if(!d||v){const t=v?a(u,void 0):"";if(""!==String(t).trim())l+=`<${f}>${t}</${f}>`;else{const t=e[c-1];t&&"JSXText"===t.type&&/\n\s*$/.test(t.value)&&(l=l.replace(/\s+$/,"")),l+=`<${f} />`}}else if(r&&!v){const e=u;if(e.some(e=>e&&("JSXText"===e.type||"JSXExpressionContainer"===e.type)&&-1!==o.indexOf(e))){const t=o.indexOf(y),n=a(e,void 0);l+=`<${t}>${p(n)}</${t}>`}else{const t=new Map;let s=0;for(const n of e)if(n&&"JSXElement"===n.type){const e=n.opening&&n.opening.name&&"Identifier"===n.opening.name.type?n.opening.name.value:void 0;if(e&&i.has(e)){const e=n.opening&&Array.isArray(n.opening.attributes)&&n.opening.attributes.length>0,i=n.children||[],r=1===i.length&&"JSXText"===i[0]?.type;(e||i.length&&!r)&&t.set(n,s++)}else t.set(n,s++)}const r=n&&n.has(y)?n.get(y):o.indexOf(y),u=a(e,t.size?t:void 0);l+=`<${r}>${p(u)}</${r}>`}}else{const e=o.indexOf(y);if(-1!==e){const n=void 0!==g?g:e;if((()=>{let e=!1;for(const t of u)if(t)if("JSXElement"!==t.type){if("JSXExpressionContainer"===t.type&&-1!==o.indexOf(t))return e;if("JSXText"===t.type&&-1!==o.indexOf(t)){if(s(t))continue;if(!e)return!0;if(u.slice(u.indexOf(t)+1).some(e=>e&&"JSXElement"===e.type))return!0}}else e=!0;return!1})()){const e=a(u,void 0,!1);l+=`<${n}>${p(e)}</${n}>`;continue}const r=new Map;let c=n;for(const e of u)if(e&&"JSXElement"===e.type){const n=e.opening&&e.opening.name&&"Identifier"===e.opening.name.type?e.opening.name.value:void 0;if(n&&i.has(n)){const n=e.opening&&Array.isArray(e.opening.attributes)&&e.opening.attributes.length>0,i=e.children||[],s=1===i.length&&("JSXText"===i[0]?.type||"JSXExpressionContainer"===i[0]?.type&&void 0!==t(i[0].expression));!n&&(!i.length||s)||r.set(e,c++)}else r.set(e,c++)}const y=a(u,r.size>0?r:void 0,!1);l+=`<${n}>${p(y)}</${n}>`}else{const e=a(u,void 0,!1);l+=`<${f}>${p(e)}</${f}>`}}}else{const e=y.children||[];if(e.some(e=>e&&("JSXText"===e.type||"JSXExpressionContainer"===e.type)&&-1!==o.indexOf(e))){const t=o.indexOf(y),n=a(e,void 0);l+=`<${t}>${p(n)}</${t}>`}else{const t=new Map;let s=0;for(const n of e)if(n&&"JSXElement"===n.type){const e=n.opening&&n.opening.name&&"Identifier"===n.opening.name.type?n.opening.name.value:void 0;if(e&&i.has(e)){const e=n.opening&&Array.isArray(n.opening.attributes)&&n.opening.attributes.length>0,i=n.children||[],r=1===i.length&&"JSXText"===i[0]?.type;!e&&(!i.length||r)||t.set(n,s++)}else t.set(n,s++)}const r=n&&n.has(y)?n.get(y):o.indexOf(y),u=a(e,t.size?t:void 0);l+=`<${r}>${p(u)}</${r}>`}}continue}"JSXFragment"!==y.type||(l+=a(y.children||[]))}}return l}const l=a(e,void 0,!0),u=String(l).replace(/<br \/>\s*\n\s*/g,"<br />").replace(/\s+/g," ");return u.replace(/\s+\./g,".").trim()}(i.children,s);let J;const X="JSXAttribute"===o?.type?n(o):void 0;if(void 0!==X)J=X;else{const e=s.extract.defaultValue;J="string"==typeof e?e:""}let h,E;if("JSXAttribute"===r?.type){if("StringLiteral"===r.value?.type){if(h=r.value,E=h.value,!E||""===E.trim())return null;if(x&&"StringLiteral"===h.type){const e=s.extract.nsSeparator??":",t=h.value;if(e&&t.startsWith(`${x}${e}`)){if(E=t.slice(`${x}${e}`.length),!E||""===E.trim())return null;h={...h,value:E}}}}else"JSXExpressionContainer"===r.value?.type&&"JSXEmptyExpression"!==r.value.expression.type&&(h=r.value.expression);if(!h)return null}return o||!E||m.trim()?!o&&m.trim()&&(J=m):J=E,{keyExpression:h,serializedChildren:m,ns:x,defaultValue:J,hasCount:u,isOrdinal:g,contextExpression:v,optionsNode:y,explicitDefault:void 0!==X||(e=>{if(!e||!Array.isArray(e.properties))return!1;for(const t of e.properties)if(t&&"KeyValueProperty"===t.type&&t.key){const e="Identifier"===t.key.type&&t.key.value||"StringLiteral"===t.key.type&&t.key.value;if("string"==typeof e&&e.startsWith("defaultValue"))return!0}return!1})(y)}};
1
+ "use strict";var e=require("./ast-utils.js");function t(t){if(t)return"StringLiteral"===t.type?t.value:"TemplateLiteral"===t.type&&e.isSimpleTemplateLiteral(t)?t.quasis[0].cooked:void 0}function n(e){return"StringLiteral"===e.value?.type?e.value.value:"JSXExpressionContainer"===e.value?.type?t(e.value.expression):void 0}exports.extractFromTransComponent=function(i,s){const r=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"i18nKey"===e.name.value),o=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"defaults"===e.name.value),p=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"count"===e.name.value),a=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"values"===e.name.value);let l;p||"JSXAttribute"!==a?.type||"JSXExpressionContainer"!==a.value?.type||"ObjectExpression"!==a.value.expression.type||(l=e.getObjectPropValueExpression(a.value.expression,"count"));const u=!!p||!!l,c=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"tOptions"===e.name.value),f="JSXAttribute"===c?.type&&"JSXExpressionContainer"===c.value?.type&&"ObjectExpression"===c.value.expression.type?c.value.expression:void 0,y=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),g=!!y,v=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"context"===e.name.value);let d="JSXAttribute"===v?.type&&"JSXExpressionContainer"===v.value?.type?v.value.expression:"JSXAttribute"===v?.type&&"StringLiteral"===v.value?.type?v.value:void 0;const S=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ns"===e.name.value);let x;x="JSXAttribute"===S?.type?n(S):void 0,f&&(void 0===x&&(x=e.getObjectPropValue(f,"ns")),void 0===d&&(d=e.getObjectPropValueExpression(f,"context")));const m=function(e,n){if(!e||0===e.length)return"";const i=new Set(n.extract.transKeepBasicHtmlNodesFor??["br","strong","i","p"]),s=e=>e&&"JSXText"===e.type&&/^\s*$/.test(e.value)&&e.value.includes("\n");function r(e,n,o=!1,p=!1){if(!e||!e.length)return;const a=p&&e.filter(e=>e&&"JSXElement"===e.type&&"p"===e.opening?.name?.value).length>1;let l=0,u=e.length-1;for(;l<=u&&s(e[l]);)l++;for(;u>=l&&s(e[u]);)u--;const c=l<=u?e.slice(l,u+1):[],f=c.some(e=>e&&("JSXElement"===e.type||"JSXFragment"===e.type));for(let e=0;e<c.length;e++){const p=c[e];if(p)if("JSXText"!==p.type){if("JSXExpressionContainer"===p.type){if(o&&!f&&p.expression){const e=p.expression.type;if("ObjectExpression"===e){const e=p.expression.properties&&p.expression.properties[0];if(e&&"KeyValueProperty"===e.type)continue}const n=t(p.expression);if(void 0!==n){if(!(/^\s*$/.test(n)&&!n.includes("\n")))continue}else if("Identifier"===e||"MemberExpression"===e||"CallExpression"===e)continue}const i=t(p.expression);if(void 0!==i){const t=/^\s*$/.test(i)&&!i.includes("\n"),r=c[e-1],o=c[e+1];if(t){const t=c[e+2];if(o&&"JSXText"===o.type&&s(o)&&t&&("JSXElement"===t.type||"JSXFragment"===t.type)){const t=c[e-1],n=c[e-2];if(!t||"JSXText"!==t.type&&n&&"JSXExpressionContainer"===n.type)continue}if(r&&("JSXElement"===r.type||"JSXFragment"===r.type)&&o&&"JSXText"===o.type&&s(o))continue;const i=!o||"JSXText"===o.type&&!s(o);if(r&&"JSXText"===r.type&&i){const e=n[n.length-1];if(e&&"JSXText"===e.type){e.value=String(e.value)+p.expression.value;continue}}if(r&&("JSXElement"===r.type||"JSXFragment"===r.type)&&o&&"JSXText"===o.type&&s(o))continue}}n.push(p);continue}if("JSXElement"===p.type){const e=p.opening&&p.opening.name&&"Identifier"===p.opening.name.type?p.opening.name.value:void 0;if(e&&i.has(e)){const i=p.opening&&Array.isArray(p.opening.attributes)&&p.opening.attributes.length>0,s=p.children||[],o=1===s.length&&("JSXText"===s[0]?.type||"JSXExpressionContainer"===s[0]?.type&&void 0!==t(s[0].expression)),l=!s.length,u=o;i&&!o?(n.push(p),r(p.children||[],n,!0)):l?n.push(p):u||("p"===e&&a?(n.push(p),r(p.children||[],n,!0,!1)):r(p.children||[],n,!1,!1));continue}n.push(p),r(p.children||[],n,!0);continue}"JSXFragment"!==p.type||r(p.children||[],n,o)}else{if(o&&!f)continue;if(o&&s(p))continue;if(s(p)){const t=c[e-1],i=c[e+1];if(t&&("JSXElement"===t.type||"JSXFragment"===t.type)&&i&&("JSXElement"===i.type||"JSXFragment"===i.type))continue;const s=n[n.length-1],r=c[e-1];if(s){if(r&&"JSXExpressionContainer"===r.type)continue;if("JSXText"===s.type&&r&&"JSXText"===r.type){s.value=String(s.value)+p.value;continue}}}if(o&&f&&0===e)continue;n.push(p)}}}const o=[];r(e,o,!1,!0);const p=new Set,a=e=>String(e).replace(/^\s*\n\s*/g,"").replace(/\s*\n\s*$/g,"");function l(e,n,r=!1){if(!e||0===e.length)return"";let u="",c=0;for(let f=0;f<e.length;f++){const y=e[f];if(y){if("JSXText"===y.type){if(s(y))continue;const t=e[f+1],n=e[f-1];if(n&&"JSXElement"===n.type){const t="Identifier"===n.opening?.name?.type?n.opening.name.value:void 0,s=t&&i.has(t),r=0===(n.children||[]).length;if(s&&r&&/^\s*\n\s*/.test(y.value)){const e=y.value.replace(/^\s*\n\s*/,"");if(e){u+=e;continue}continue}if(!s&&/^\s*\n\s*/.test(y.value)){const t=y.value.replace(/^\s*\n\s*/,"");if(t){const n=e[f-2];if(n&&"JSXText"===n.type){const e=n.value.replace(/\n\s*$/,""),i=/[A-Za-z0-9]$/.test(e),s=/^[A-Za-z0-9]/.test(t),r=/^[a-z]/.test(t);if(i&&s&&r){u+=t;continue}}u+=" "+t;continue}continue}}if(/\n\s*$/.test(y.value)&&t&&"JSXElement"===t.type){const n=y.value.replace(/\n\s*$/,"");if(n.trim()){const r="Identifier"===t.opening?.name?.type?t.opening.name.value:void 0,o=r&&i.has(r),a=(t.children||[]).length>0,l=/\s\n/.test(y.value),c=e[f+2],g=c&&"JSXText"===c.type&&!s(c)&&/[a-zA-Z0-9]/.test(c.value),v=!!(t.opening&&Array.isArray(t.opening.attributes)&&t.opening.attributes.length>0),d=/^\s/.test(n)&&!/^\n/.test(n),S=n.trim(),x=d?" "+S:S,m=/[A-Za-z0-9]$/.test(S),J=c&&"string"==typeof c.value&&/^[A-Za-z0-9]/.test(c.value.trim()),X=c&&"string"==typeof c.value&&/^[a-z]/.test(c.value.trim()),h=c&&"string"==typeof c.value&&/^\s/.test(c.value),$=v&&a&&g&&!(m&&J&&X&&!l&&!d&&!h);m&&J&&X&&!l&&!d&&!h&&p.add(t),u+=l||o&&a||!o&&g&&d||$?x+" ":x;continue}}u+=y.value;continue}if("JSXExpressionContainer"===y.type){const e=y.expression;if(!e)continue;const n=t(e);if(void 0!==n)u+=n;else if("Identifier"===e.type)u+=`{{${e.value}}}`;else if("ObjectExpression"===e.type){const t=e.properties[0];t&&"KeyValueProperty"===t.type&&t.key&&"Identifier"===t.key.type?u+=`{{${t.key.value}}}`:t&&"Identifier"===t.type?u+=`{{${t.value}}}`:u+="{{value}}"}else"MemberExpression"===e.type&&e.property&&"Identifier"===e.property.type?u+=`{{${e.property.value}}}`:"CallExpression"===e.type&&"Identifier"===e.callee?.type?u+=`{{${e.callee.value}}}`:u+="{{value}}";continue}if("JSXElement"===y.type){let p;y.opening&&y.opening.name&&"Identifier"===y.opening.name.type&&(p=y.opening.name.value);const g=r?c:void 0;if(r&&"JSXElement"===y.type&&c++,p&&i.has(p)){const r=y.opening&&Array.isArray(y.opening.attributes)&&y.opening.attributes.length>0,c=y.children||[],v=c.length>0,d=1===c.length&&("JSXText"===c[0]?.type||"JSXExpressionContainer"===c[0]?.type&&void 0!==t(c[0].expression));if(!v||d){const t=d?l(c,void 0):"";if(""!==String(t).trim())u+=`<${p}>${t}</${p}>`;else{const t=e[f-1];t&&"JSXText"===t.type&&/\n\s*$/.test(t.value)&&(u=u.replace(/\s+$/,"")),u+=`<${p} />`}}else if(r&&!d){const e=c;if(e.some(e=>e&&("JSXText"===e.type||"JSXExpressionContainer"===e.type)&&-1!==o.indexOf(e))){const t=o.indexOf(y),n=l(e,void 0);u+=`<${t}>${a(n)}</${t}>`}else{const e=n&&n.has(y)?n.get(y):o.indexOf(y),s=new Map;let r="number"==typeof e&&e>=0?e:0;for(const e of c)if(e&&"JSXElement"===e.type){const n=e.opening&&e.opening.name&&"Identifier"===e.opening.name.type?e.opening.name.value:void 0;if(n&&i.has(n)){const n=e.opening&&Array.isArray(e.opening.attributes)&&e.opening.attributes.length>0,i=e.children||[],o=1===i.length&&("JSXText"===i[0]?.type||"JSXExpressionContainer"===i[0]?.type&&void 0!==t(i[0].expression));!n&&(!i.length||o)||s.set(e,r++)}else s.set(e,r++)}const p=l(c,s.size?s:void 0);u+=`<${e}>${a(p)}</${e}>`}}else{const e=o.indexOf(y);if(-1!==e){const n=void 0!==g?g:e;if((()=>{let e=!1;for(const t of c)if(t)if("JSXElement"!==t.type){if("JSXExpressionContainer"===t.type&&-1!==o.indexOf(t))return e;if("JSXText"===t.type&&-1!==o.indexOf(t)){if(s(t))continue;if(!e)return!0;if(c.slice(c.indexOf(t)+1).some(e=>e&&"JSXElement"===e.type))return!0}}else e=!0;return!1})()){const e=l(c,void 0,!1);u+=`<${n}>${a(e)}</${n}>`;continue}const r=new Map;let p=n;for(const e of c)if(e&&"JSXElement"===e.type){const n=e.opening&&e.opening.name&&"Identifier"===e.opening.name.type?e.opening.name.value:void 0;if(n&&i.has(n)){const n=e.opening&&Array.isArray(e.opening.attributes)&&e.opening.attributes.length>0,i=e.children||[],s=1===i.length&&("JSXText"===i[0]?.type||"JSXExpressionContainer"===i[0]?.type&&void 0!==t(i[0].expression));!n&&(!i.length||s)||r.set(e,p++)}else r.set(e,p++)}const f=l(c,r.size>0?r:void 0,!1);u+=`<${n}>${a(f)}</${n}>`}else{const e=l(c,void 0,!1);u+=`<${p}>${a(e)}</${p}>`}}}else{const e=y.children||[];if(e.some(e=>e&&("JSXText"===e.type||"JSXExpressionContainer"===e.type)&&-1!==o.indexOf(e))){const t=o.indexOf(y),n=l(e,void 0);u+=`<${t}>${a(n)}</${t}>`}else{const t=new Map,s=n&&n.has(y)?n.get(y):o.indexOf(y);let r="number"==typeof s&&s>=0?s:0;for(const n of e)if(n&&"JSXElement"===n.type){const e=n.opening&&n.opening.name&&"Identifier"===n.opening.name.type?n.opening.name.value:void 0;if(e&&i.has(e)){const e=n.opening&&Array.isArray(n.opening.attributes)&&n.opening.attributes.length>0,i=n.children||[],s=1===i.length&&"JSXText"===i[0]?.type;!e&&(!i.length||s)||t.set(n,r++)}else t.set(n,r++)}const p=l(e,t.size?t:void 0);u+=`<${s}>${a(p)}</${s}>`}}continue}"JSXFragment"!==y.type||(u+=l(y.children||[]))}}return u}const u=l(e,void 0,!0),c=String(u).replace(/<br \/>\s*\n\s*/g,"<br />"),f=String(c),y=new Set;if(p&&p.size>0)for(let e=0;e<o.length;e++)p.has(o[e])&&y.add(e);let g=String(f);if(y&&y.size>0)for(const e of y)try{g=g.replace(new RegExp("\\s+<"+e+">","g"),"<"+e+">"),g=g.replace(new RegExp("<\\/"+e+">\\s+","g"),"</"+e+">")}catch(e){}g=g.replace(/<\/(\d+)>\s*\n\s*(\S)/g,(e,t,n)=>{const i=Number(t);return y.has(i)?`</${t}>${n}`:`</${t}> ${n}`}),g=g.replace(/(\S)\s*\n\s*<(\d+)/g,(e,t,n)=>{const i=Number(n);return y.has(i)?`${t}<${n}`:`${t} <${n}`}),g=g.replace(/\s*\n\s*/g," "),g=g.replace(/\s+/g," "),g=g.replace(/\s+\./g,".");const v=g.trim();let d=String(v);if(y&&y.size>0)for(const e of y)try{d=d.replace(new RegExp("[\\s\\u00A0]+<"+e+">","g"),"<"+e+">"),d=d.replace(new RegExp("<\\/"+e+">[\\s\\u00A0]+","g"),"</"+e+">")}catch(e){}try{for(let e=0;e<o.length;e++){const t=o[e];if(!t||"JSXElement"!==t.type)continue;const n=o[e-1],i=o[e+1];if(!n||!i)continue;if("JSXText"!==n.type||"JSXText"!==i.type)continue;const s=String(n.value),r=String(i.value),p=s.replace(/\n\s*$/,""),a=/[A-Za-z0-9]$/.test(p),l=/^[A-Za-z0-9]/.test(r.trim()),u=/^[a-z]/.test(r.trim()),c=/\s\n/.test(s),f=r&&/^\s/.test(r)&&!/^\n/.test(r);if(a&&l&&u&&!c&&!f){const t=e;d=d.replace(new RegExp("\\s+<"+t+">","g"),"<"+t+">")}}}catch(e){}return d.trim()}(i.children,s);let J;const X="JSXAttribute"===o?.type?n(o):void 0;if(void 0!==X)J=X;else{const e=s.extract.defaultValue;J="string"==typeof e?e:""}let h,$;if("JSXAttribute"===r?.type){if("StringLiteral"===r.value?.type){if(h=r.value,$=h.value,!$||""===$.trim())return null;if(x&&"StringLiteral"===h.type){const e=s.extract.nsSeparator??":",t=h.value;if(e&&t.startsWith(`${x}${e}`)){if($=t.slice(`${x}${e}`.length),!$||""===$.trim())return null;h={...h,value:$}}}}else"JSXExpressionContainer"===r.value?.type&&"JSXEmptyExpression"!==r.value.expression.type&&(h=r.value.expression);if(!h)return null}return o||!$||m.trim()?!o&&m.trim()&&(J=m):J=$,{keyExpression:h,serializedChildren:m,ns:x,defaultValue:J,hasCount:u,isOrdinal:g,contextExpression:d,optionsNode:f,explicitDefault:void 0!==X||(e=>{if(!e||!Array.isArray(e.properties))return!1;for(const t of e.properties)if(t&&"KeyValueProperty"===t.type&&t.key){const e="Identifier"===t.key.type&&t.key.value||"StringLiteral"===t.key.type&&t.key.value;if("string"==typeof e&&e.startsWith("defaultValue"))return!0}return!1})(f)}};
package/dist/esm/cli.js CHANGED
@@ -1,2 +1,2 @@
1
1
  #!/usr/bin/env node
2
- import{Command as o}from"commander";import e from"chokidar";import{glob as t}from"glob";import{minimatch as n}from"minimatch";import i from"chalk";import{ensureConfig as a,loadConfig as r}from"./config.js";import{detectConfig as c}from"./heuristic-config.js";import{runExtractor as s}from"./extractor/core/extractor.js";import"node:path";import"node:fs/promises";import"jiti";import{runTypesGenerator as l}from"./types-generator.js";import{runSyncer as p}from"./syncer.js";import{runMigrator as f}from"./migrator.js";import{runInit as m}from"./init.js";import{runLinterCli as d}from"./linter.js";import{runStatus as g}from"./status.js";import{runLocizeSync as u,runLocizeDownload as y,runLocizeMigrate as h}from"./locize.js";import{runRenameKey as w}from"./rename-key.js";const x=new o;x.name("i18next-cli").description("A unified, high-performance i18next CLI.").version("1.24.3"),x.option("-c, --config <path>","Path to i18next-cli config file (overrides detection)"),x.command("extract").description("Extract translation keys from source files and update resource files.").option("-w, --watch","Watch for file changes and re-run the extractor.").option("--ci","Exit with a non-zero status code if any files are updated.").option("--dry-run","Run the extractor without writing any files to disk.").option("--sync-primary","Sync primary language values with default values from code.").action(async o=>{try{const t=x.opts().config,i=await a(t),r=async()=>{const e=await s(i,{isWatchMode:!!o.watch,isDryRun:!!o.dryRun,syncPrimaryWithDefaults:!!o.syncPrimary});return o.ci&&!e?(console.log("✅ No files were updated."),process.exit(0)):o.ci&&e&&(console.error("❌ Some files were updated. This should not happen in CI mode."),process.exit(1)),e};if(await r(),o.watch){console.log("\nWatching for changes...");const o=await z(i.extract.input),t=j(i.extract.ignore),a=k(i.extract.output),c=[...t,...a].filter(Boolean),s=o.filter(o=>!c.some(e=>n(o,e,{dot:!0})));e.watch(s,{ignored:/node_modules/,persistent:!0}).on("change",o=>{console.log(`\nFile changed: ${o}`),r()})}}catch(o){console.error("Error running extractor:",o),process.exit(1)}}),x.command("status [locale]").description("Display translation status. Provide a locale for a detailed key-by-key view.").option("-n, --namespace <ns>","Filter the status report by a specific namespace").action(async(o,e)=>{const t=x.opts().config;let n=await r(t);if(!n){console.log(i.blue("No config file found. Attempting to detect project structure..."));const o=await c();o||(console.error(i.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${i.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(i.green("Project structure detected successfully!")),n=o}await g(n,{detail:o,namespace:e.namespace})}),x.command("types").description("Generate TypeScript definitions from translation resource files.").option("-w, --watch","Watch for file changes and re-run the type generator.").action(async o=>{const t=x.opts().config,i=await a(t),r=()=>l(i);if(await r(),o.watch){console.log("\nWatching for changes...");const o=await z(i.types?.input||[]),t=[...j(i.extract?.ignore)].filter(Boolean),a=o.filter(o=>!t.some(e=>n(o,e,{dot:!0})));e.watch(a,{persistent:!0}).on("change",o=>{console.log(`\nFile changed: ${o}`),r()})}}),x.command("sync").description("Synchronize secondary language files with the primary language file.").action(async()=>{const o=x.opts().config,e=await a(o);await p(e)}),x.command("migrate-config [configPath]").description("Migrate a legacy i18next-parser.config.js to the new format.").action(async o=>{await f(o)}),x.command("init").description("Create a new i18next.config.ts/js file with an interactive setup wizard.").action(m),x.command("lint").description("Find potential issues like hardcoded strings in your codebase.").option("-w, --watch","Watch for file changes and re-run the linter.").action(async o=>{const t=x.opts().config,a=async()=>{let o=await r(t);if(!o){console.log(i.blue("No config file found. Attempting to detect project structure..."));const e=await c();e||(console.error(i.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${i.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(i.green("Project structure detected successfully!")),o=e}await d(o)};if(await a(),o.watch){console.log("\nWatching for changes...");const o=await r(t);if(o?.extract?.input){const t=await z(o.extract.input),i=[...j(o.extract.ignore),...k(o.extract.output)].filter(Boolean),r=t.filter(o=>!i.some(e=>n(o,e,{dot:!0})));e.watch(r,{ignored:/node_modules/,persistent:!0}).on("change",o=>{console.log(`\nFile changed: ${o}`),a()})}}}),x.command("locize-sync").description("Synchronize local translations with your locize project.").option("--update-values","Update values of existing translations on locize.").option("--src-lng-only","Check for changes in source language only.").option("--compare-mtime","Compare modification times when syncing.").option("--dry-run","Run the command without making any changes.").action(async o=>{const e=x.opts().config,t=await a(e);await u(t,o)}),x.command("locize-download").description("Download all translations from your locize project.").action(async o=>{const e=x.opts().config,t=await a(e);await y(t,o)}),x.command("locize-migrate").description("Migrate local translation files to a new locize project.").action(async o=>{const e=x.opts().config,t=await a(e);await h(t,o)}),x.command("rename-key <oldKey> <newKey>").description("Rename a translation key across all source files and translation files.").option("--dry-run","Preview changes without modifying files").action(async(o,e,t)=>{try{const n=x.opts().config,r=await a(n),c=await w(r,o,e,t);c.success||(c.conflicts&&(console.error(i.red("\n❌ Conflicts detected:")),c.conflicts.forEach(o=>console.error(` - ${o}`))),c.error&&console.error(i.red(`\n❌ ${c.error}`)),process.exit(1));0===c.sourceFiles.reduce((o,e)=>o+e.changes,0)&&console.log(i.yellow(`\n⚠️ No usages found for "${o}"`))}catch(o){console.error(i.red("Error renaming key:"),o),process.exit(1)}}),x.parse(process.argv);const j=o=>Array.isArray(o)?o:o?[o]:[],k=o=>o&&"string"==typeof o?[o.replace(/\{\{[^}]+\}\}/g,"*")]:[],z=async(o=[])=>{const e=j(o),n=await Promise.all(e.map(o=>t(o||"",{nodir:!0})));return Array.from(new Set(n.flat()))};
2
+ import{Command as o}from"commander";import e from"chokidar";import{glob as t}from"glob";import{minimatch as n}from"minimatch";import i from"chalk";import{ensureConfig as a,loadConfig as r}from"./config.js";import{detectConfig as c}from"./heuristic-config.js";import{runExtractor as s}from"./extractor/core/extractor.js";import"node:path";import"node:fs/promises";import"jiti";import{runTypesGenerator as l}from"./types-generator.js";import{runSyncer as p}from"./syncer.js";import{runMigrator as f}from"./migrator.js";import{runInit as m}from"./init.js";import{runLinterCli as d}from"./linter.js";import{runStatus as g}from"./status.js";import{runLocizeSync as u,runLocizeDownload as y,runLocizeMigrate as h}from"./locize.js";import{runRenameKey as w}from"./rename-key.js";const x=new o;x.name("i18next-cli").description("A unified, high-performance i18next CLI.").version("1.24.5"),x.option("-c, --config <path>","Path to i18next-cli config file (overrides detection)"),x.command("extract").description("Extract translation keys from source files and update resource files.").option("-w, --watch","Watch for file changes and re-run the extractor.").option("--ci","Exit with a non-zero status code if any files are updated.").option("--dry-run","Run the extractor without writing any files to disk.").option("--sync-primary","Sync primary language values with default values from code.").action(async o=>{try{const t=x.opts().config,i=await a(t),r=async()=>{const e=await s(i,{isWatchMode:!!o.watch,isDryRun:!!o.dryRun,syncPrimaryWithDefaults:!!o.syncPrimary});return o.ci&&!e?(console.log("✅ No files were updated."),process.exit(0)):o.ci&&e&&(console.error("❌ Some files were updated. This should not happen in CI mode."),process.exit(1)),e};if(await r(),o.watch){console.log("\nWatching for changes...");const o=await z(i.extract.input),t=j(i.extract.ignore),a=k(i.extract.output),c=[...t,...a].filter(Boolean),s=o.filter(o=>!c.some(e=>n(o,e,{dot:!0})));e.watch(s,{ignored:/node_modules/,persistent:!0}).on("change",o=>{console.log(`\nFile changed: ${o}`),r()})}}catch(o){console.error("Error running extractor:",o),process.exit(1)}}),x.command("status [locale]").description("Display translation status. Provide a locale for a detailed key-by-key view.").option("-n, --namespace <ns>","Filter the status report by a specific namespace").action(async(o,e)=>{const t=x.opts().config;let n=await r(t);if(!n){console.log(i.blue("No config file found. Attempting to detect project structure..."));const o=await c();o||(console.error(i.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${i.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(i.green("Project structure detected successfully!")),n=o}await g(n,{detail:o,namespace:e.namespace})}),x.command("types").description("Generate TypeScript definitions from translation resource files.").option("-w, --watch","Watch for file changes and re-run the type generator.").action(async o=>{const t=x.opts().config,i=await a(t),r=()=>l(i);if(await r(),o.watch){console.log("\nWatching for changes...");const o=await z(i.types?.input||[]),t=[...j(i.extract?.ignore)].filter(Boolean),a=o.filter(o=>!t.some(e=>n(o,e,{dot:!0})));e.watch(a,{persistent:!0}).on("change",o=>{console.log(`\nFile changed: ${o}`),r()})}}),x.command("sync").description("Synchronize secondary language files with the primary language file.").action(async()=>{const o=x.opts().config,e=await a(o);await p(e)}),x.command("migrate-config [configPath]").description("Migrate a legacy i18next-parser.config.js to the new format.").action(async o=>{await f(o)}),x.command("init").description("Create a new i18next.config.ts/js file with an interactive setup wizard.").action(m),x.command("lint").description("Find potential issues like hardcoded strings in your codebase.").option("-w, --watch","Watch for file changes and re-run the linter.").action(async o=>{const t=x.opts().config,a=async()=>{let o=await r(t);if(!o){console.log(i.blue("No config file found. Attempting to detect project structure..."));const e=await c();e||(console.error(i.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${i.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(i.green("Project structure detected successfully!")),o=e}await d(o)};if(await a(),o.watch){console.log("\nWatching for changes...");const o=await r(t);if(o?.extract?.input){const t=await z(o.extract.input),i=[...j(o.extract.ignore),...k(o.extract.output)].filter(Boolean),r=t.filter(o=>!i.some(e=>n(o,e,{dot:!0})));e.watch(r,{ignored:/node_modules/,persistent:!0}).on("change",o=>{console.log(`\nFile changed: ${o}`),a()})}}}),x.command("locize-sync").description("Synchronize local translations with your locize project.").option("--update-values","Update values of existing translations on locize.").option("--src-lng-only","Check for changes in source language only.").option("--compare-mtime","Compare modification times when syncing.").option("--dry-run","Run the command without making any changes.").action(async o=>{const e=x.opts().config,t=await a(e);await u(t,o)}),x.command("locize-download").description("Download all translations from your locize project.").action(async o=>{const e=x.opts().config,t=await a(e);await y(t,o)}),x.command("locize-migrate").description("Migrate local translation files to a new locize project.").action(async o=>{const e=x.opts().config,t=await a(e);await h(t,o)}),x.command("rename-key <oldKey> <newKey>").description("Rename a translation key across all source files and translation files.").option("--dry-run","Preview changes without modifying files").action(async(o,e,t)=>{try{const n=x.opts().config,r=await a(n),c=await w(r,o,e,t);c.success||(c.conflicts&&(console.error(i.red("\n❌ Conflicts detected:")),c.conflicts.forEach(o=>console.error(` - ${o}`))),c.error&&console.error(i.red(`\n❌ ${c.error}`)),process.exit(1));0===c.sourceFiles.reduce((o,e)=>o+e.changes,0)&&console.log(i.yellow(`\n⚠️ No usages found for "${o}"`))}catch(o){console.error(i.red("Error renaming key:"),o),process.exit(1)}}),x.parse(process.argv);const j=o=>Array.isArray(o)?o:o?[o]:[],k=o=>o&&"string"==typeof o?[o.replace(/\{\{[^}]+\}\}/g,"*")]:[],z=async(o=[])=>{const e=j(o),n=await Promise.all(e.map(o=>t(o||"",{nodir:!0})));return Array.from(new Set(n.flat()))};
@@ -1 +1 @@
1
- import{ScopeManager as e}from"../parsers/scope-manager.js";import{ExpressionResolver as r}from"../parsers/expression-resolver.js";import{CallExpressionHandler as s}from"../parsers/call-expression-handler.js";import{JSXHandler as t}from"../parsers/jsx-handler.js";class o{pluginContext;config;logger;hooks;get objectKeys(){return this.callExpressionHandler.objectKeys}scopeManager;expressionResolver;callExpressionHandler;jsxHandler;currentFile="";currentCode="";constructor(o,a,i,n,l){this.pluginContext=a,this.config=o,this.logger=i,this.hooks={onBeforeVisitNode:n?.onBeforeVisitNode,onAfterVisitNode:n?.onAfterVisitNode,resolvePossibleKeyStringValues:n?.resolvePossibleKeyStringValues,resolvePossibleContextStringValues:n?.resolvePossibleContextStringValues},this.scopeManager=new e(o),this.expressionResolver=l??new r(this.hooks),this.callExpressionHandler=new s(o,a,i,this.expressionResolver,()=>this.getCurrentFile(),()=>this.getCurrentCode()),this.jsxHandler=new t(o,a,this.expressionResolver,()=>this.getCurrentFile(),()=>this.getCurrentCode())}visit(e){this.scopeManager.reset(),this.expressionResolver.resetFileSymbols(),this.scopeManager.enterScope(),this.walk(e),this.scopeManager.exitScope()}walk(e){if(!e)return;let r=!1;switch("Function"!==e.type&&"ArrowFunctionExpression"!==e.type&&"FunctionExpression"!==e.type||(this.scopeManager.enterScope(),r=!0),this.hooks.onBeforeVisitNode?.(e),e.type){case"VariableDeclarator":this.scopeManager.handleVariableDeclarator(e),this.expressionResolver.captureVariableDeclarator(e);break;case"TSEnumDeclaration":case"TsEnumDeclaration":case"TsEnumDecl":this.expressionResolver.captureEnumDeclaration(e);break;case"CallExpression":this.callExpressionHandler.handleCallExpression(e,this.scopeManager.getVarFromScope.bind(this.scopeManager));break;case"JSXElement":this.jsxHandler.handleJSXElement(e,this.scopeManager.getVarFromScope.bind(this.scopeManager))}this.hooks.onAfterVisitNode?.(e);for(const r in e){if("span"===r)continue;const s=e[r];if(Array.isArray(s)){for(const e of s)if(e&&"object"==typeof e)if("VariableDeclarator"!==e.type){if(e&&e.id&&Array.isArray(e.members)&&this.expressionResolver.captureEnumDeclaration(e),"VariableDeclaration"===e.type&&Array.isArray(e.declarations))for(const r of e.declarations)r&&"object"==typeof r&&"VariableDeclarator"===r.type&&(this.scopeManager.handleVariableDeclarator(r),this.expressionResolver.captureVariableDeclarator(r))}else this.scopeManager.handleVariableDeclarator(e),this.expressionResolver.captureVariableDeclarator(e);for(const e of s)e&&"object"==typeof e&&this.walk(e)}else s&&"object"==typeof s&&this.walk(s)}r&&this.scopeManager.exitScope()}getVarFromScope(e){return this.scopeManager.getVarFromScope(e)}setCurrentFile(e,r){this.currentFile=e,this.currentCode=r}getCurrentFile(){return this.currentFile}getCurrentCode(){return this.currentCode}}export{o as ASTVisitors};
1
+ import{ScopeManager as e}from"../parsers/scope-manager.js";import{ExpressionResolver as r}from"../parsers/expression-resolver.js";import{CallExpressionHandler as s}from"../parsers/call-expression-handler.js";import{JSXHandler as t}from"../parsers/jsx-handler.js";class o{pluginContext;config;logger;hooks;get objectKeys(){return this.callExpressionHandler.objectKeys}scopeManager;expressionResolver;callExpressionHandler;jsxHandler;currentFile="";currentCode="";constructor(o,a,i,n,l){this.pluginContext=a,this.config=o,this.logger=i,this.hooks={onBeforeVisitNode:n?.onBeforeVisitNode,onAfterVisitNode:n?.onAfterVisitNode,resolvePossibleKeyStringValues:n?.resolvePossibleKeyStringValues,resolvePossibleContextStringValues:n?.resolvePossibleContextStringValues},this.scopeManager=new e(o),this.expressionResolver=l??new r(this.hooks),this.callExpressionHandler=new s(o,a,i,this.expressionResolver,()=>this.getCurrentFile(),()=>this.getCurrentCode()),this.jsxHandler=new t(o,a,this.expressionResolver,()=>this.getCurrentFile(),()=>this.getCurrentCode())}visit(e){this.scopeManager.reset(),this.expressionResolver.resetFileSymbols(),this.scopeManager.enterScope(),this.walk(e),this.scopeManager.exitScope()}walk(e){if(!e)return;let r=!1;switch("Function"!==e.type&&"ArrowFunctionExpression"!==e.type&&"FunctionExpression"!==e.type||(this.scopeManager.enterScope(),r=!0),this.hooks.onBeforeVisitNode?.(e),e.type){case"VariableDeclarator":this.scopeManager.handleVariableDeclarator(e),this.expressionResolver.captureVariableDeclarator(e);break;case"TSEnumDeclaration":case"TsEnumDeclaration":case"TsEnumDecl":this.expressionResolver.captureEnumDeclaration(e);break;case"CallExpression":this.callExpressionHandler.handleCallExpression(e,this.scopeManager.getVarFromScope.bind(this.scopeManager));break;case"JSXElement":this.jsxHandler.handleJSXElement(e,this.scopeManager.getVarFromScope.bind(this.scopeManager))}this.hooks.onAfterVisitNode?.(e);for(const r in e){if("span"===r)continue;const s=e[r];if(Array.isArray(s)){for(const e of s)if(e&&"object"==typeof e)if("VariableDeclarator"!==e.type){if(e&&e.id&&Array.isArray(e.members)&&this.expressionResolver.captureEnumDeclaration(e),"VariableDeclaration"===e.type&&Array.isArray(e.declarations))for(const r of e.declarations)r&&"object"==typeof r&&"VariableDeclarator"===r.type&&(this.scopeManager.handleVariableDeclarator(r),this.expressionResolver.captureVariableDeclarator(r))}else this.scopeManager.handleVariableDeclarator(e),this.expressionResolver.captureVariableDeclarator(e);for(const e of s)e&&"object"==typeof e&&this.walk(e)}else s&&"object"==typeof s&&this.walk(s)}r&&this.scopeManager.exitScope()}getVarFromScope(e){return this.scopeManager.getVarFromScope(e)}setCurrentFile(e,r){this.currentFile=e,this.currentCode=r,this.callExpressionHandler.resetSearchIndex(),this.jsxHandler.resetSearchIndex()}getCurrentFile(){return this.currentFile}getCurrentCode(){return this.currentCode}}export{o as ASTVisitors};
@@ -1 +1 @@
1
- import{isSimpleTemplateLiteral as e,getObjectPropValue as t,getObjectPropValueExpression as r}from"./ast-utils.js";class n{pluginContext;config;logger;expressionResolver;objectKeys=new Set;getCurrentFile;getCurrentCode;constructor(e,t,r,n,i,o){this.config=e,this.pluginContext=t,this.logger=r,this.expressionResolver=n,this.getCurrentFile=i,this.getCurrentCode=o}getLocationFromSpan(e){if(!e||"number"!=typeof e.start)return;const t=this.getCurrentCode(),r=e.start,n=t.substring(0,r).split("\n");return{line:n.length,column:n[n.length-1].length}}handleCallExpression(n,i){const o=this.getFunctionName(n.callee);if(!o)return;const s=i(o),l=this.config.extract.functions||["t","*.t"];let a=void 0!==s;if(!a)for(const e of l)if(e.startsWith("*.")){if(o.endsWith(e.substring(1))){a=!0;break}}else if(e===o){a=!0;break}if(!a||0===n.arguments.length)return;const{keysToProcess:u,isSelectorAPI:f}=this.handleCallExpressionArgument(n,0);if(0===u.length)return;let c=!1;const p=this.config.extract.pluralSeparator??"_";for(let e=0;e<u.length;e++)u[e].endsWith(`${p}ordinal`)&&(c=!0,u[e]=u[e].slice(0,-8));let y,g;if(n.arguments.length>1){const t=n.arguments[1].expression;"ObjectExpression"===t.type?g=t:"StringLiteral"===t.type?y=t.value:"TemplateLiteral"===t.type&&e(t)&&(y=t.quasis[0].cooked)}if(n.arguments.length>2){const e=n.arguments[2].expression;"ObjectExpression"===e.type&&(g=e)}const h=g?t(g,"defaultValue"):void 0,d="string"==typeof h?h:y,x=e=>{if(!e||!Array.isArray(e.properties))return!1;for(const t of e.properties)if(t&&"KeyValueProperty"===t.type&&t.key){const e="Identifier"===t.key.type&&t.key.value||"StringLiteral"===t.key.type&&t.key.value;if("string"==typeof e&&e.startsWith("defaultValue"))return!0}return!1},k="string"==typeof d||x(g),$=x(g),m=Boolean($||"string"==typeof d&&!("string"==typeof(v=d)&&/{{\s*count\s*}}/.test(v)));var v;for(let e=0;e<u.length;e++){let i,o=u[e];if(g){const e=t(g,"ns");"string"==typeof e&&(i=e)}const l=this.config.extract.nsSeparator??":";if(!i&&l&&o.includes(l)){const e=o.split(l);if(i=e.shift(),o=e.join(l),!o||""===o.trim()){this.logger.warn(`Skipping key that became empty after namespace removal: '${i}${l}'`);continue}}!i&&s?.defaultNs&&(i=s.defaultNs),i||(i=this.config.extract.defaultNS);let a=o;if(s?.keyPrefix){const e=this.config.extract.keySeparator??".";if(a=!1!==e?s.keyPrefix.endsWith(e)?`${s.keyPrefix}${o}`:`${s.keyPrefix}${e}${o}`:`${s.keyPrefix}${o}`,!1!==e){if(a.split(e).some(e=>""===e.trim())){this.logger.warn(`Skipping key with empty segments: '${a}' (keyPrefix: '${s.keyPrefix}', key: '${o}')`);continue}}}const p=e===u.length-1&&d||o;if(g){const e=r(g,"context"),n=[];if("StringLiteral"===e?.type||"NumericLiteral"===e?.type||"BooleanLiteral"===e?.type){const t=`${e.value}`,r=this.config.extract.contextSeparator??"_";""!==t&&n.push({key:`${a}${r}${t}`,ns:i,defaultValue:p,explicitDefault:k})}else if(e){const t=this.expressionResolver.resolvePossibleContextStringValues(e),r=this.config.extract.contextSeparator??"_";t.length>0&&t.forEach(e=>{n.push({key:`${a}${r}${e}`,ns:i,defaultValue:p,explicitDefault:k})}),n.push({key:a,ns:i,defaultValue:p,explicitDefault:k,keyAcceptingContext:a})}const o=e=>{if(e){if("KeyValueProperty"===e.type&&e.key){if("Identifier"===e.key.type)return e.key.value;if("StringLiteral"===e.key.type)return e.key.value}return"KeyValueProperty"===e.type&&e.value&&"Identifier"===e.value.type?e.key&&"Identifier"===e.key.type?e.key.value:void 0:"ShorthandProperty"!==e.type&&"Identifier"!==e.type||!e.value?e.key&&"string"==typeof e.key?e.key:void 0:e.value}},s=(()=>{if(!g||!Array.isArray(g.properties))return!1;for(const e of g.properties){if("count"===o(e))return!0}return!1})(),l=(()=>{if(!g||!Array.isArray(g.properties))return!1;for(const e of g.properties){if("ordinal"===o(e))return!("KeyValueProperty"!==e.type||!e.value||"BooleanLiteral"!==e.value.type)&&Boolean(e.value.value)}return!1})();if(s||c){try{const e=c?"ordinal":"cardinal",t=this.config.extract?.primaryLanguage||(Array.isArray(this.config.locales)?this.config.locales[0]:void 0)||"en";let r=!1;try{const n=new Intl.PluralRules(t,{type:e}).resolvedOptions().pluralCategories;1===n.length&&"other"===n[0]&&(r=!0)}catch{}if(!r){const t=new Set;for(const r of this.config.locales)try{new Intl.PluralRules(r,{type:e}).resolvedOptions().pluralCategories.forEach(e=>t.add(e))}catch{new Intl.PluralRules("en",{type:e}).resolvedOptions().pluralCategories.forEach(e=>t.add(e))}const n=Array.from(t).sort();1===n.length&&"other"===n[0]&&(r=!0)}if(r){if(n.length>0)for(const e of n)this.pluginContext.addKey({key:e.key,ns:e.ns,defaultValue:e.defaultValue,hasCount:!0,isOrdinal:c});else this.pluginContext.addKey({key:a,ns:i,defaultValue:p,hasCount:!0,isOrdinal:c});continue}}catch(e){}this.config.extract.disablePlurals?n.length>0?n.forEach(this.pluginContext.addKey):this.pluginContext.addKey({key:a,ns:i,defaultValue:p,explicitDefault:k}):this.handlePluralKeys(a,i,g,l||c,d,m);continue}if(n.length>0){n.forEach(this.pluginContext.addKey);continue}!0===t(g,"returnObjects")&&this.objectKeys.add(a)}f&&this.objectKeys.add(a);{const e=n.span?this.getLocationFromSpan(n.span):void 0;this.pluginContext.addKey({key:a,ns:i,defaultValue:p,explicitDefault:k,locations:e?[{file:this.getCurrentFile(),line:e.line,column:e.column}]:void 0})}}}handleCallExpressionArgument(e,t){const r=e.arguments[t].expression,n=[];let i=!1;if("ArrowFunctionExpression"===r.type){const e=this.extractKeyFromSelector(r);e&&(n.push(e),i=!0)}else if("ArrayExpression"===r.type)for(const e of r.elements)e?.expression&&n.push(...this.expressionResolver.resolvePossibleKeyStringValues(e.expression));else n.push(...this.expressionResolver.resolvePossibleKeyStringValues(r));return{keysToProcess:n.filter(e=>!!e),isSelectorAPI:i}}extractKeyFromSelector(e){let t=e.body;if("BlockStatement"===t.type){const e=t.stmts.find(e=>"ReturnStatement"===e.type);if("ReturnStatement"!==e?.type||!e.argument)return null;t=e.argument}let r=t;const n=[];for(;"MemberExpression"===r.type;){const e=r.property;if("Identifier"===e.type)n.unshift(e.value);else{if("Computed"!==e.type||"StringLiteral"!==e.expression.type)return null;n.unshift(e.expression.value)}r=r.object}if(n.length>0){const e=this.config.extract.keySeparator,t="string"==typeof e?e:".";return n.join(t)}return null}handlePluralKeys(e,n,i,o,s,l){try{const a=o?"ordinal":"cardinal",u=new Set;for(const e of this.config.locales)try{const t=new Intl.PluralRules(e,{type:a});t.resolvedOptions().pluralCategories.forEach(e=>u.add(e))}catch(e){const t=new Intl.PluralRules("en",{type:a});t.resolvedOptions().pluralCategories.forEach(e=>u.add(e))}const f=Array.from(u).sort(),c=this.config.extract.pluralSeparator??"_",p=t(i,"defaultValue"),y=t(i,`defaultValue${c}other`),g=t(i,`defaultValue${c}ordinal${c}other`),h=r(i,"context"),d=[];if(h){const t=this.expressionResolver.resolvePossibleContextStringValues(h);if(t.length>0)if("StringLiteral"===h.type)for(const r of t)r.length>0&&d.push({key:e,context:r});else{for(const r of t)r.length>0&&d.push({key:e,context:r});!1!==this.config.extract?.generateBasePluralForms&&d.push({key:e})}else d.push({key:e})}else d.push({key:e});const x=this.config.extract?.primaryLanguage||(Array.isArray(this.config.locales)?this.config.locales[0]:void 0)||"en";let k=!1;try{const e=new Intl.PluralRules(x,{type:a}).resolvedOptions().pluralCategories;1===e.length&&"other"===e[0]&&(k=!0)}catch{k=!1}if(k||1===f.length&&"other"===f[0]){for(const{key:e,context:r}of d){const a=t(i,`defaultValue${c}other`);let u;u="string"==typeof a?a:"string"==typeof p?p:"string"==typeof s?s:r?`${e}_${r}`:e;const f=this.config.extract.contextSeparator??"_",y=r?`${e}${f}${r}`:e;this.pluginContext.addKey({key:y,ns:n,defaultValue:u,hasCount:!0,isOrdinal:o,explicitDefault:Boolean(l||"string"==typeof a)})}return}for(const{key:r,context:a}of d)for(const u of f){const f=t(i,o?`defaultValue${c}ordinal${c}${u}`:`defaultValue${c}${u}`);let h,d;if(h="string"==typeof f?f:"one"===u&&"string"==typeof p?p:"one"===u&&"string"==typeof s?s:o&&"string"==typeof g?g:o||"string"!=typeof y?"string"==typeof p?p:"string"==typeof s?s:r:y,a){const e=this.config.extract.contextSeparator??"_";d=o?`${r}${e}${a}${c}ordinal${c}${u}`:`${r}${e}${a}${c}${u}`}else d=o?`${r}${c}ordinal${c}${u}`:`${r}${c}${u}`;this.pluginContext.addKey({key:d,ns:n,defaultValue:h,hasCount:!0,isOrdinal:o,explicitDefault:Boolean(l||"string"==typeof f||"string"==typeof y),keyAcceptingContext:void 0!==a?e:void 0})}}catch(r){this.logger.warn(`Could not determine plural rules for language "${this.config.extract?.primaryLanguage}". Falling back to simple key extraction.`);const o=s||t(i,"defaultValue");this.pluginContext.addKey({key:e,ns:n,defaultValue:"string"==typeof o?o:e})}}getFunctionName(e){if("Identifier"===e.type)return e.value;if("MemberExpression"===e.type){const t=[];let r=e;for(;"MemberExpression"===r.type;){if("Identifier"!==r.property.type)return null;t.unshift(r.property.value),r=r.object}if("ThisExpression"===r.type)t.unshift("this");else{if("Identifier"!==r.type)return null;t.unshift(r.value)}return t.join(".")}return null}}export{n as CallExpressionHandler};
1
+ import{isSimpleTemplateLiteral as e,getObjectPropValue as t,getObjectPropValueExpression as r}from"./ast-utils.js";class n{pluginContext;config;logger;expressionResolver;objectKeys=new Set;getCurrentFile;getCurrentCode;lastSearchIndex=0;constructor(e,t,r,n,i,s){this.config=e,this.pluginContext=t,this.logger=r,this.expressionResolver=n,this.getCurrentFile=i,this.getCurrentCode=s}resetSearchIndex(){this.lastSearchIndex=0}getLocationFromNode(e){const t=this.getCurrentCode();let r;if("CallExpression"===e.type&&e.arguments.length>0){const t=e.arguments[0].expression;"StringLiteral"===t.type?r=t.raw??`'${t.value}'`:"TemplateLiteral"===t.type&&(r="`")}if(!r)return;const n=t.indexOf(r,this.lastSearchIndex);if(-1===n)return;this.lastSearchIndex=n+r.length;const i=t.substring(0,n).split("\n");return{line:i.length,column:i[i.length-1].length}}handleCallExpression(n,i){const s=this.getFunctionName(n.callee);if(!s)return;const o=i(s),l=this.config.extract.functions||["t","*.t"];let a=void 0!==o;if(!a)for(const e of l)if(e.startsWith("*.")){if(s.endsWith(e.substring(1))){a=!0;break}}else if(e===s){a=!0;break}if(!a||0===n.arguments.length)return;const{keysToProcess:u,isSelectorAPI:f}=this.handleCallExpressionArgument(n,0);if(0===u.length)return;let c=!1;const p=this.config.extract.pluralSeparator??"_";for(let e=0;e<u.length;e++)u[e].endsWith(`${p}ordinal`)&&(c=!0,u[e]=u[e].slice(0,-8));let y,g;if(n.arguments.length>1){const t=n.arguments[1].expression;"ObjectExpression"===t.type?g=t:"StringLiteral"===t.type?y=t.value:"TemplateLiteral"===t.type&&e(t)&&(y=t.quasis[0].cooked)}if(n.arguments.length>2){const e=n.arguments[2].expression;"ObjectExpression"===e.type&&(g=e)}const h=g?t(g,"defaultValue"):void 0,d="string"==typeof h?h:y,x=e=>{if(!e||!Array.isArray(e.properties))return!1;for(const t of e.properties)if(t&&"KeyValueProperty"===t.type&&t.key){const e="Identifier"===t.key.type&&t.key.value||"StringLiteral"===t.key.type&&t.key.value;if("string"==typeof e&&e.startsWith("defaultValue"))return!0}return!1},k="string"==typeof d||x(g),$=x(g),m=Boolean($||"string"==typeof d&&!("string"==typeof(v=d)&&/{{\s*count\s*}}/.test(v)));var v;for(let e=0;e<u.length;e++){let i,s=u[e];if(g){const e=t(g,"ns");"string"==typeof e&&(i=e)}const l=this.config.extract.nsSeparator??":";if(!i&&l&&s.includes(l)){const e=s.split(l);if(i=e.shift(),s=e.join(l),!s||""===s.trim()){this.logger.warn(`Skipping key that became empty after namespace removal: '${i}${l}'`);continue}}!i&&o?.defaultNs&&(i=o.defaultNs),i||(i=this.config.extract.defaultNS);let a=s;if(o?.keyPrefix){const e=this.config.extract.keySeparator??".";if(a=!1!==e?o.keyPrefix.endsWith(e)?`${o.keyPrefix}${s}`:`${o.keyPrefix}${e}${s}`:`${o.keyPrefix}${s}`,!1!==e){if(a.split(e).some(e=>""===e.trim())){this.logger.warn(`Skipping key with empty segments: '${a}' (keyPrefix: '${o.keyPrefix}', key: '${s}')`);continue}}}const p=e===u.length-1&&d||s;if(g){const e=r(g,"context"),n=[];if("StringLiteral"===e?.type||"NumericLiteral"===e?.type||"BooleanLiteral"===e?.type){const t=`${e.value}`,r=this.config.extract.contextSeparator??"_";""!==t&&n.push({key:`${a}${r}${t}`,ns:i,defaultValue:p,explicitDefault:k})}else if(e){const t=this.expressionResolver.resolvePossibleContextStringValues(e),r=this.config.extract.contextSeparator??"_";t.length>0&&t.forEach(e=>{n.push({key:`${a}${r}${e}`,ns:i,defaultValue:p,explicitDefault:k})}),n.push({key:a,ns:i,defaultValue:p,explicitDefault:k,keyAcceptingContext:a})}const s=e=>{if(e){if("KeyValueProperty"===e.type&&e.key){if("Identifier"===e.key.type)return e.key.value;if("StringLiteral"===e.key.type)return e.key.value}return"KeyValueProperty"===e.type&&e.value&&"Identifier"===e.value.type?e.key&&"Identifier"===e.key.type?e.key.value:void 0:"ShorthandProperty"!==e.type&&"Identifier"!==e.type||!e.value?e.key&&"string"==typeof e.key?e.key:void 0:e.value}},o=(()=>{if(!g||!Array.isArray(g.properties))return!1;for(const e of g.properties){if("count"===s(e))return!0}return!1})(),l=(()=>{if(!g||!Array.isArray(g.properties))return!1;for(const e of g.properties){if("ordinal"===s(e))return!("KeyValueProperty"!==e.type||!e.value||"BooleanLiteral"!==e.value.type)&&Boolean(e.value.value)}return!1})();if(o||c){try{const e=c?"ordinal":"cardinal",t=this.config.extract?.primaryLanguage||(Array.isArray(this.config.locales)?this.config.locales[0]:void 0)||"en";let r=!1;try{const n=new Intl.PluralRules(t,{type:e}).resolvedOptions().pluralCategories;1===n.length&&"other"===n[0]&&(r=!0)}catch{}if(!r){const t=new Set;for(const r of this.config.locales)try{new Intl.PluralRules(r,{type:e}).resolvedOptions().pluralCategories.forEach(e=>t.add(e))}catch{new Intl.PluralRules("en",{type:e}).resolvedOptions().pluralCategories.forEach(e=>t.add(e))}const n=Array.from(t).sort();1===n.length&&"other"===n[0]&&(r=!0)}if(r){if(n.length>0)for(const e of n)this.pluginContext.addKey({key:e.key,ns:e.ns,defaultValue:e.defaultValue,hasCount:!0,isOrdinal:c});else this.pluginContext.addKey({key:a,ns:i,defaultValue:p,hasCount:!0,isOrdinal:c});continue}}catch(e){}this.config.extract.disablePlurals?n.length>0?n.forEach(this.pluginContext.addKey):this.pluginContext.addKey({key:a,ns:i,defaultValue:p,explicitDefault:k}):this.handlePluralKeys(a,i,g,l||c,d,m);continue}if(n.length>0){n.forEach(this.pluginContext.addKey);continue}!0===t(g,"returnObjects")&&this.objectKeys.add(a)}f&&this.objectKeys.add(a);{const e=this.getLocationFromNode(n);this.pluginContext.addKey({key:a,ns:i,defaultValue:p,explicitDefault:k,locations:e?[{file:this.getCurrentFile(),line:e.line,column:e.column}]:void 0})}}}handleCallExpressionArgument(e,t){const r=e.arguments[t].expression,n=[];let i=!1;if("ArrowFunctionExpression"===r.type){const e=this.extractKeyFromSelector(r);e&&(n.push(e),i=!0)}else if("ArrayExpression"===r.type)for(const e of r.elements)e?.expression&&n.push(...this.expressionResolver.resolvePossibleKeyStringValues(e.expression));else n.push(...this.expressionResolver.resolvePossibleKeyStringValues(r));return{keysToProcess:n.filter(e=>!!e),isSelectorAPI:i}}extractKeyFromSelector(e){let t=e.body;if("BlockStatement"===t.type){const e=t.stmts.find(e=>"ReturnStatement"===e.type);if("ReturnStatement"!==e?.type||!e.argument)return null;t=e.argument}let r=t;const n=[];for(;"MemberExpression"===r.type;){const e=r.property;if("Identifier"===e.type)n.unshift(e.value);else{if("Computed"!==e.type||"StringLiteral"!==e.expression.type)return null;n.unshift(e.expression.value)}r=r.object}if(n.length>0){const e=this.config.extract.keySeparator,t="string"==typeof e?e:".";return n.join(t)}return null}handlePluralKeys(e,n,i,s,o,l){try{const a=s?"ordinal":"cardinal",u=new Set;for(const e of this.config.locales)try{const t=new Intl.PluralRules(e,{type:a});t.resolvedOptions().pluralCategories.forEach(e=>u.add(e))}catch(e){const t=new Intl.PluralRules("en",{type:a});t.resolvedOptions().pluralCategories.forEach(e=>u.add(e))}const f=Array.from(u).sort(),c=this.config.extract.pluralSeparator??"_",p=t(i,"defaultValue"),y=t(i,`defaultValue${c}other`),g=t(i,`defaultValue${c}ordinal${c}other`),h=r(i,"context"),d=[];if(h){const t=this.expressionResolver.resolvePossibleContextStringValues(h);if(t.length>0)if("StringLiteral"===h.type)for(const r of t)r.length>0&&d.push({key:e,context:r});else{for(const r of t)r.length>0&&d.push({key:e,context:r});!1!==this.config.extract?.generateBasePluralForms&&d.push({key:e})}else d.push({key:e})}else d.push({key:e});const x=this.config.extract?.primaryLanguage||(Array.isArray(this.config.locales)?this.config.locales[0]:void 0)||"en";let k=!1;try{const e=new Intl.PluralRules(x,{type:a}).resolvedOptions().pluralCategories;1===e.length&&"other"===e[0]&&(k=!0)}catch{k=!1}if(k||1===f.length&&"other"===f[0]){for(const{key:e,context:r}of d){const a=t(i,`defaultValue${c}other`);let u;u="string"==typeof a?a:"string"==typeof p?p:"string"==typeof o?o:r?`${e}_${r}`:e;const f=this.config.extract.contextSeparator??"_",y=r?`${e}${f}${r}`:e;this.pluginContext.addKey({key:y,ns:n,defaultValue:u,hasCount:!0,isOrdinal:s,explicitDefault:Boolean(l||"string"==typeof a)})}return}for(const{key:r,context:a}of d)for(const u of f){const f=t(i,s?`defaultValue${c}ordinal${c}${u}`:`defaultValue${c}${u}`);let h,d;if(h="string"==typeof f?f:"one"===u&&"string"==typeof p?p:"one"===u&&"string"==typeof o?o:s&&"string"==typeof g?g:s||"string"!=typeof y?"string"==typeof p?p:"string"==typeof o?o:r:y,a){const e=this.config.extract.contextSeparator??"_";d=s?`${r}${e}${a}${c}ordinal${c}${u}`:`${r}${e}${a}${c}${u}`}else d=s?`${r}${c}ordinal${c}${u}`:`${r}${c}${u}`;this.pluginContext.addKey({key:d,ns:n,defaultValue:h,hasCount:!0,isOrdinal:s,explicitDefault:Boolean(l||"string"==typeof f||"string"==typeof y),keyAcceptingContext:void 0!==a?e:void 0})}}catch(r){this.logger.warn(`Could not determine plural rules for language "${this.config.extract?.primaryLanguage}". Falling back to simple key extraction.`);const s=o||t(i,"defaultValue");this.pluginContext.addKey({key:e,ns:n,defaultValue:"string"==typeof s?s:e})}}getFunctionName(e){if("Identifier"===e.type)return e.value;if("MemberExpression"===e.type){const t=[];let r=e;for(;"MemberExpression"===r.type;){if("Identifier"!==r.property.type)return null;t.unshift(r.property.value),r=r.object}if("ThisExpression"===r.type)t.unshift("this");else{if("Identifier"!==r.type)return null;t.unshift(r.value)}return t.join(".")}return null}}export{n as CallExpressionHandler};
@@ -1 +1 @@
1
- import{extractFromTransComponent as e}from"./jsx-parser.js";import{getObjectPropValue as t}from"./ast-utils.js";class n{config;pluginContext;expressionResolver;getCurrentFile;getCurrentCode;constructor(e,t,n,o,s){this.config=e,this.pluginContext=t,this.expressionResolver=n,this.getCurrentFile=o,this.getCurrentCode=s}getLocationFromSpan(e){if(!e||"number"!=typeof e.start)return;const t=this.getCurrentCode(),n=e.start,o=t.substring(0,n).split("\n");return{line:o.length,column:o[o.length-1].length}}handleJSXElement(t,n){const o=this.getElementName(t);if(o&&(this.config.extract.transComponents||["Trans"]).includes(o)){const o=e(t,this.config),s=[];if(o){if(o.keyExpression){const e=this.expressionResolver.resolvePossibleKeyStringValues(o.keyExpression);s.push(...e)}else s.push(o.serializedChildren);let e;const{contextExpression:i,optionsNode:a,defaultValue:l,hasCount:r,isOrdinal:u,serializedChildren:f}=o,c=t.span?this.getLocationFromSpan(t.span):void 0,p=c?[{file:this.getCurrentFile(),line:c.line,column:c.column}]:void 0;if(o.ns){const{ns:t}=o;e=s.map(e=>({key:e,ns:t,defaultValue:l||f,hasCount:r,isOrdinal:u,locations:p}))}else{e=s.map(e=>{const t=this.config.extract.nsSeparator??":";let n;if(t&&e.includes(t)){let o;[n,...o]=e.split(t),e=o.join(t)}return{key:e,ns:n,defaultValue:l||f,hasCount:r,isOrdinal:u,explicitDefault:o.explicitDefault,locations:p}});const i=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"t"===e.name.value);if("JSXAttribute"===i?.type&&"JSXExpressionContainer"===i.value?.type&&"Identifier"===i.value.expression.type){const t=n(i.value.expression.value);t?.defaultNs&&e.forEach(e=>{e.ns||(e.ns=t.defaultNs)})}}if(e.forEach(e=>{e.ns||(e.ns=this.config.extract.defaultNS)}),i&&r)if(this.config.extract.disablePlurals){const t=this.expressionResolver.resolvePossibleContextStringValues(i),n=this.config.extract.contextSeparator??"_";if(t.length>0)if("StringLiteral"===i.type)for(const o of t)for(const t of e){const e=`${t.key}${n}${o}`;this.pluginContext.addKey({key:e,ns:t.ns,defaultValue:t.defaultValue,locations:t.locations})}else{e.forEach(e=>{this.pluginContext.addKey({key:e.key,ns:e.ns,defaultValue:e.defaultValue,locations:e.locations,keyAcceptingContext:e.key})});for(const o of t)for(const t of e){const e=`${t.key}${n}${o}`;this.pluginContext.addKey({key:e,ns:t.ns,defaultValue:t.defaultValue,locations:t.locations})}}else e.forEach(e=>{this.pluginContext.addKey({key:e.key,ns:e.ns,defaultValue:e.defaultValue,locations:e.locations,keyAcceptingContext:e.key})})}else{const n=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),o=!!n,s=this.expressionResolver.resolvePossibleContextStringValues(i),l=this.config.extract.contextSeparator??"_";if(s.length>0){e.forEach(e=>this.generatePluralKeysForTrans(e.key,e.defaultValue,e.ns,o,a,void 0,e.locations,e.key));for(const t of s)for(const n of e){const e=`${n.key}${l}${t}`;this.generatePluralKeysForTrans(e,n.defaultValue,n.ns,o,a,n.explicitDefault,n.locations,n.key)}}else e.forEach(e=>this.generatePluralKeysForTrans(e.key,e.defaultValue,e.ns,o,a,e.explicitDefault,e.locations))}else if(i){const t=this.expressionResolver.resolvePossibleContextStringValues(i),n=this.config.extract.contextSeparator??"_";if(t.length>0){for(const o of t)for(const{key:t,ns:s,defaultValue:i,locations:a}of e)this.pluginContext.addKey({key:`${t}${n}${o}`,ns:s,defaultValue:i,locations:a});"StringLiteral"!==i.type&&e.forEach(e=>{this.pluginContext.addKey({key:e.key,ns:e.ns,defaultValue:e.defaultValue,locations:e.locations,keyAcceptingContext:e.key})})}else e.forEach(e=>{this.pluginContext.addKey({key:e.key,ns:e.ns,defaultValue:e.defaultValue,locations:e.locations,keyAcceptingContext:e.key})})}else if(r)if(this.config.extract.disablePlurals)e.forEach(e=>{this.pluginContext.addKey({key:e.key,ns:e.ns,defaultValue:e.defaultValue,locations:e.locations})});else{const n=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),o=!!n;e.forEach(e=>this.generatePluralKeysForTrans(e.key,e.defaultValue,e.ns,o,a,e.explicitDefault,e.locations))}else e.forEach(e=>{this.pluginContext.addKey({key:e.key,ns:e.ns,defaultValue:e.defaultValue,locations:e.locations})})}}}generatePluralKeysForTrans(e,n,o,s,i,a,l,r){try{const u=s?"ordinal":"cardinal",f=new Intl.PluralRules(this.config.extract?.primaryLanguage,{type:u}).resolvedOptions().pluralCategories,c=this.config.extract.pluralSeparator??"_";let p,d;if(i&&(p=t(i,`defaultValue${c}other`),d=t(i,`defaultValue${c}ordinal${c}other`)),1===f.length&&"other"===f[0]){const u=i?t(i,`defaultValue${c}other`):void 0,f="string"==typeof u?u:"string"==typeof n?n:e;return void this.pluginContext.addKey({key:e,ns:o,defaultValue:f,hasCount:!0,isOrdinal:s,explicitDefault:Boolean(a||"string"==typeof u||"string"==typeof p),locations:l,keyAcceptingContext:r})}for(const u of f){const f=i?t(i,s?`defaultValue${c}ordinal${c}${u}`:`defaultValue${c}${u}`):void 0;let y;y="string"==typeof f?f:"one"===u&&"string"==typeof n?n:s&&"string"==typeof d?d:s||"string"!=typeof p?"string"==typeof n?n:e:p;const g=s?`${e}${c}ordinal${c}${u}`:`${e}${c}${u}`;this.pluginContext.addKey({key:g,ns:o,defaultValue:y,hasCount:!0,isOrdinal:s,explicitDefault:Boolean(a||"string"==typeof f||"string"==typeof p),locations:l,keyAcceptingContext:r})}}catch(t){this.pluginContext.addKey({key:e,ns:o,defaultValue:n,locations:l})}}getElementName(e){if("Identifier"===e.opening.name.type)return e.opening.name.value;if("JSXMemberExpression"===e.opening.name.type){let t=e.opening.name;const n=[];for(;"JSXMemberExpression"===t.type;)"Identifier"===t.property.type&&n.unshift(t.property.value),t=t.object;return"Identifier"===t.type&&n.unshift(t.value),n.join(".")}}}export{n as JSXHandler};
1
+ import{extractFromTransComponent as e}from"./jsx-parser.js";import{getObjectPropValue as t}from"./ast-utils.js";class n{config;pluginContext;expressionResolver;getCurrentFile;getCurrentCode;lastSearchIndex=0;constructor(e,t,n,o,s){this.config=e,this.pluginContext=t,this.expressionResolver=n,this.getCurrentFile=o,this.getCurrentCode=s}resetSearchIndex(){this.lastSearchIndex=0}getLocationFromNode(e){const t=this.getCurrentCode();let n;if("JSXElement"===e.type&&e.opening){const t=e.opening.name?.value;t&&(n=`<${t}`)}if(!n)return;const o=t.indexOf(n,this.lastSearchIndex);if(-1===o)return;this.lastSearchIndex=o+n.length;const s=t.substring(0,o).split("\n");return{line:s.length,column:s[s.length-1].length}}handleJSXElement(t,n){const o=this.getElementName(t);if(o&&(this.config.extract.transComponents||["Trans"]).includes(o)){const o=e(t,this.config),s=[];if(o){if(o.keyExpression){const e=this.expressionResolver.resolvePossibleKeyStringValues(o.keyExpression);s.push(...e)}else s.push(o.serializedChildren);let e;const{contextExpression:i,optionsNode:l,defaultValue:a,hasCount:r,isOrdinal:u,serializedChildren:c}=o,f=this.getLocationFromNode(t),d=f?[{file:this.getCurrentFile(),line:f.line,column:f.column}]:void 0;if(o.ns){const{ns:t}=o;e=s.map(e=>({key:e,ns:t,defaultValue:a||c,hasCount:r,isOrdinal:u,locations:d}))}else{e=s.map(e=>{const t=this.config.extract.nsSeparator??":";let n;if(t&&e.includes(t)){let o;[n,...o]=e.split(t),e=o.join(t)}return{key:e,ns:n,defaultValue:a||c,hasCount:r,isOrdinal:u,explicitDefault:o.explicitDefault,locations:d}});const i=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"t"===e.name.value);if("JSXAttribute"===i?.type&&"JSXExpressionContainer"===i.value?.type&&"Identifier"===i.value.expression.type){const t=n(i.value.expression.value);t?.defaultNs&&e.forEach(e=>{e.ns||(e.ns=t.defaultNs)})}}if(e.forEach(e=>{e.ns||(e.ns=this.config.extract.defaultNS)}),i&&r)if(this.config.extract.disablePlurals){const t=this.expressionResolver.resolvePossibleContextStringValues(i),n=this.config.extract.contextSeparator??"_";if(t.length>0)if("StringLiteral"===i.type)for(const o of t)for(const t of e){const e=`${t.key}${n}${o}`;this.pluginContext.addKey({key:e,ns:t.ns,defaultValue:t.defaultValue,locations:t.locations})}else{e.forEach(e=>{this.pluginContext.addKey({key:e.key,ns:e.ns,defaultValue:e.defaultValue,locations:e.locations,keyAcceptingContext:e.key})});for(const o of t)for(const t of e){const e=`${t.key}${n}${o}`;this.pluginContext.addKey({key:e,ns:t.ns,defaultValue:t.defaultValue,locations:t.locations})}}else e.forEach(e=>{this.pluginContext.addKey({key:e.key,ns:e.ns,defaultValue:e.defaultValue,locations:e.locations,keyAcceptingContext:e.key})})}else{const n=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),o=!!n,s=this.expressionResolver.resolvePossibleContextStringValues(i),a=this.config.extract.contextSeparator??"_";if(s.length>0){e.forEach(e=>this.generatePluralKeysForTrans(e.key,e.defaultValue,e.ns,o,l,void 0,e.locations,e.key));for(const t of s)for(const n of e){const e=`${n.key}${a}${t}`;this.generatePluralKeysForTrans(e,n.defaultValue,n.ns,o,l,n.explicitDefault,n.locations,n.key)}}else e.forEach(e=>this.generatePluralKeysForTrans(e.key,e.defaultValue,e.ns,o,l,e.explicitDefault,e.locations))}else if(i){const t=this.expressionResolver.resolvePossibleContextStringValues(i),n=this.config.extract.contextSeparator??"_";if(t.length>0){for(const o of t)for(const{key:t,ns:s,defaultValue:i,locations:l}of e)this.pluginContext.addKey({key:`${t}${n}${o}`,ns:s,defaultValue:i,locations:l});"StringLiteral"!==i.type&&e.forEach(e=>{this.pluginContext.addKey({key:e.key,ns:e.ns,defaultValue:e.defaultValue,locations:e.locations,keyAcceptingContext:e.key})})}else e.forEach(e=>{this.pluginContext.addKey({key:e.key,ns:e.ns,defaultValue:e.defaultValue,locations:e.locations,keyAcceptingContext:e.key})})}else if(r)if(this.config.extract.disablePlurals)e.forEach(e=>{this.pluginContext.addKey({key:e.key,ns:e.ns,defaultValue:e.defaultValue,locations:e.locations})});else{const n=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),o=!!n;e.forEach(e=>this.generatePluralKeysForTrans(e.key,e.defaultValue,e.ns,o,l,e.explicitDefault,e.locations))}else e.forEach(e=>{this.pluginContext.addKey({key:e.key,ns:e.ns,defaultValue:e.defaultValue,locations:e.locations})})}}}generatePluralKeysForTrans(e,n,o,s,i,l,a,r){try{const u=s?"ordinal":"cardinal",c=new Intl.PluralRules(this.config.extract?.primaryLanguage,{type:u}).resolvedOptions().pluralCategories,f=this.config.extract.pluralSeparator??"_";let d,p;if(i&&(d=t(i,`defaultValue${f}other`),p=t(i,`defaultValue${f}ordinal${f}other`)),1===c.length&&"other"===c[0]){const u=i?t(i,`defaultValue${f}other`):void 0,c="string"==typeof u?u:"string"==typeof n?n:e;return void this.pluginContext.addKey({key:e,ns:o,defaultValue:c,hasCount:!0,isOrdinal:s,explicitDefault:Boolean(l||"string"==typeof u||"string"==typeof d),locations:a,keyAcceptingContext:r})}for(const u of c){const c=i?t(i,s?`defaultValue${f}ordinal${f}${u}`:`defaultValue${f}${u}`):void 0;let y;y="string"==typeof c?c:"one"===u&&"string"==typeof n?n:s&&"string"==typeof p?p:s||"string"!=typeof d?"string"==typeof n?n:e:d;const g=s?`${e}${f}ordinal${f}${u}`:`${e}${f}${u}`;this.pluginContext.addKey({key:g,ns:o,defaultValue:y,hasCount:!0,isOrdinal:s,explicitDefault:Boolean(l||"string"==typeof c||"string"==typeof d),locations:a,keyAcceptingContext:r})}}catch(t){this.pluginContext.addKey({key:e,ns:o,defaultValue:n,locations:a})}}getElementName(e){if("Identifier"===e.opening.name.type)return e.opening.name.value;if("JSXMemberExpression"===e.opening.name.type){let t=e.opening.name;const n=[];for(;"JSXMemberExpression"===t.type;)"Identifier"===t.property.type&&n.unshift(t.property.value),t=t.object;return"Identifier"===t.type&&n.unshift(t.value),n.join(".")}}}export{n as JSXHandler};
@@ -1 +1 @@
1
- import{getObjectPropValueExpression as e,getObjectPropValue as t,isSimpleTemplateLiteral as n}from"./ast-utils.js";function i(e){if(e)return"StringLiteral"===e.type?e.value:"TemplateLiteral"===e.type&&n(e)?e.quasis[0].cooked:void 0}function s(e){return"StringLiteral"===e.value?.type?e.value.value:"JSXExpressionContainer"===e.value?.type?i(e.value.expression):void 0}function o(n,o){const r=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"i18nKey"===e.name.value),p=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"defaults"===e.name.value),a=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"count"===e.name.value),l=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"values"===e.name.value);let u;a||"JSXAttribute"!==l?.type||"JSXExpressionContainer"!==l.value?.type||"ObjectExpression"!==l.value.expression.type||(u=e(l.value.expression,"count"));const y=!!a||!!u,f=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"tOptions"===e.name.value),c="JSXAttribute"===f?.type&&"JSXExpressionContainer"===f.value?.type&&"ObjectExpression"===f.value.expression.type?f.value.expression:void 0,g=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),d=!!g,v=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"context"===e.name.value);let S="JSXAttribute"===v?.type&&"JSXExpressionContainer"===v.value?.type?v.value.expression:"JSXAttribute"===v?.type&&"StringLiteral"===v.value?.type?v.value:void 0;const x=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ns"===e.name.value);let m;m="JSXAttribute"===x?.type?s(x):void 0,c&&(void 0===m&&(m=t(c,"ns")),void 0===S&&(S=e(c,"context")));const J=function(e,t){if(!e||0===e.length)return"";const n=new Set(t.extract.transKeepBasicHtmlNodesFor??["br","strong","i","p"]),s=e=>e&&"JSXText"===e.type&&/^\s*$/.test(e.value)&&e.value.includes("\n");function o(e,t,r=!1,p=!1){if(!e||!e.length)return;const a=p&&e.filter(e=>e&&"JSXElement"===e.type&&"p"===e.opening?.name?.value).length>1;let l=0,u=e.length-1;for(;l<=u&&s(e[l]);)l++;for(;u>=l&&s(e[u]);)u--;const y=l<=u?e.slice(l,u+1):[],f=y.some(e=>e&&("JSXElement"===e.type||"JSXFragment"===e.type));for(let e=0;e<y.length;e++){const p=y[e];if(p)if("JSXText"!==p.type){if("JSXExpressionContainer"===p.type){if(r&&!f&&p.expression){const e=p.expression.type;if("ObjectExpression"===e){const e=p.expression.properties&&p.expression.properties[0];if(e&&"KeyValueProperty"===e.type)continue}const t=i(p.expression);if(void 0!==t){if(!(/^\s*$/.test(t)&&!t.includes("\n")))continue}else if("Identifier"===e||"MemberExpression"===e||"CallExpression"===e)continue}const n=i(p.expression);if(void 0!==n){const i=/^\s*$/.test(n)&&!n.includes("\n"),o=y[e-1],r=y[e+1];if(i){const n=y[e+2];if(r&&"JSXText"===r.type&&s(r)&&n&&("JSXElement"===n.type||"JSXFragment"===n.type)){const t=y[e-1],n=y[e-2];if(!t||"JSXText"!==t.type&&n&&"JSXExpressionContainer"===n.type)continue}if(o&&("JSXElement"===o.type||"JSXFragment"===o.type)&&r&&"JSXText"===r.type&&s(r))continue;const i=!r||"JSXText"===r.type&&!s(r);if(o&&"JSXText"===o.type&&i){const e=t[t.length-1];if(e&&"JSXText"===e.type){e.value=String(e.value)+p.expression.value;continue}}if(o&&("JSXElement"===o.type||"JSXFragment"===o.type)&&r&&"JSXText"===r.type&&s(r))continue}}t.push(p);continue}if("JSXElement"===p.type){const e=p.opening&&p.opening.name&&"Identifier"===p.opening.name.type?p.opening.name.value:void 0;if(e&&n.has(e)){const n=p.opening&&Array.isArray(p.opening.attributes)&&p.opening.attributes.length>0,s=p.children||[],r=1===s.length&&("JSXText"===s[0]?.type||"JSXExpressionContainer"===s[0]?.type&&void 0!==i(s[0].expression)),l=!s.length,u=r;n&&!r?(t.push(p),o(p.children||[],t,!0)):l?t.push(p):u||("p"===e&&a?(t.push(p),o(p.children||[],t,!0,!1)):o(p.children||[],t,!1,!1));continue}t.push(p),o(p.children||[],t,!0);continue}"JSXFragment"!==p.type||o(p.children||[],t,r)}else{if(r&&!f)continue;if(r&&s(p))continue;if(s(p)){const n=y[e-1],i=y[e+1];if(n&&("JSXElement"===n.type||"JSXFragment"===n.type)&&i&&("JSXElement"===i.type||"JSXFragment"===i.type))continue;const s=t[t.length-1],o=y[e-1];if(s){if(o&&"JSXExpressionContainer"===o.type)continue;if("JSXText"===s.type&&o&&"JSXText"===o.type){s.value=String(s.value)+p.value;continue}}}if(r&&f&&0===e)continue;t.push(p)}}}const r=[];o(e,r,!1,!0);const p=e=>String(e).replace(/^\s*\n\s*/g,"").replace(/\s*\n\s*$/g,"");function a(e,t,o=!1){if(!e||0===e.length)return"";let l="",u=0;for(let y=0;y<e.length;y++){const f=e[y];if(f){if("JSXText"===f.type){if(s(f))continue;const t=e[y+1],i=e[y-1];if(i&&"JSXElement"===i.type){const e="Identifier"===i.opening?.name?.type?i.opening.name.value:void 0,t=e&&n.has(e),s=0===(i.children||[]).length;if(t&&s&&/^\s*\n\s*/.test(f.value)){const e=f.value.replace(/^\s*\n\s*/,"");if(e){l+=e;continue}continue}}if(/\n\s*$/.test(f.value)&&t&&"JSXElement"===t.type){const i=f.value.replace(/\n\s*$/,"");if(i.trim()){const o="Identifier"===t.opening?.name?.type?t.opening.name.value:void 0,r=o&&n.has(o),p=(t.children||[]).length>0,a=/\s\n/.test(f.value),u=e[y+2],c=u&&"JSXText"===u.type&&!s(u)&&/[a-zA-Z0-9]/.test(u.value),g=/^\s/.test(i),d=i.trim(),v=g?" "+d:d;l+=a||r&&p||!r&&c?v+" ":v;continue}}l+=f.value;continue}if("JSXExpressionContainer"===f.type){const e=f.expression;if(!e)continue;const t=i(e);if(void 0!==t)l+=t;else if("Identifier"===e.type)l+=`{{${e.value}}}`;else if("ObjectExpression"===e.type){const t=e.properties[0];t&&"KeyValueProperty"===t.type&&t.key&&"Identifier"===t.key.type?l+=`{{${t.key.value}}}`:t&&"Identifier"===t.type?l+=`{{${t.value}}}`:l+="{{value}}"}else"MemberExpression"===e.type&&e.property&&"Identifier"===e.property.type?l+=`{{${e.property.value}}}`:"CallExpression"===e.type&&"Identifier"===e.callee?.type?l+=`{{${e.callee.value}}}`:l+="{{value}}";continue}if("JSXElement"===f.type){let c;f.opening&&f.opening.name&&"Identifier"===f.opening.name.type&&(c=f.opening.name.value);const g=o?u:void 0;if(o&&"JSXElement"===f.type&&u++,c&&n.has(c)){const o=f.opening&&Array.isArray(f.opening.attributes)&&f.opening.attributes.length>0,u=f.children||[],d=u.length>0,v=1===u.length&&("JSXText"===u[0]?.type||"JSXExpressionContainer"===u[0]?.type&&void 0!==i(u[0].expression));if(!d||v){const t=v?a(u,void 0):"";if(""!==String(t).trim())l+=`<${c}>${t}</${c}>`;else{const t=e[y-1];t&&"JSXText"===t.type&&/\n\s*$/.test(t.value)&&(l=l.replace(/\s+$/,"")),l+=`<${c} />`}}else if(o&&!v){const e=u;if(e.some(e=>e&&("JSXText"===e.type||"JSXExpressionContainer"===e.type)&&-1!==r.indexOf(e))){const t=r.indexOf(f),n=a(e,void 0);l+=`<${t}>${p(n)}</${t}>`}else{const i=new Map;let s=0;for(const t of e)if(t&&"JSXElement"===t.type){const e=t.opening&&t.opening.name&&"Identifier"===t.opening.name.type?t.opening.name.value:void 0;if(e&&n.has(e)){const e=t.opening&&Array.isArray(t.opening.attributes)&&t.opening.attributes.length>0,n=t.children||[],o=1===n.length&&"JSXText"===n[0]?.type;(e||n.length&&!o)&&i.set(t,s++)}else i.set(t,s++)}const o=t&&t.has(f)?t.get(f):r.indexOf(f),u=a(e,i.size?i:void 0);l+=`<${o}>${p(u)}</${o}>`}}else{const e=r.indexOf(f);if(-1!==e){const t=void 0!==g?g:e;if((()=>{let e=!1;for(const t of u)if(t)if("JSXElement"!==t.type){if("JSXExpressionContainer"===t.type&&-1!==r.indexOf(t))return e;if("JSXText"===t.type&&-1!==r.indexOf(t)){if(s(t))continue;if(!e)return!0;if(u.slice(u.indexOf(t)+1).some(e=>e&&"JSXElement"===e.type))return!0}}else e=!0;return!1})()){const e=a(u,void 0,!1);l+=`<${t}>${p(e)}</${t}>`;continue}const o=new Map;let y=t;for(const e of u)if(e&&"JSXElement"===e.type){const t=e.opening&&e.opening.name&&"Identifier"===e.opening.name.type?e.opening.name.value:void 0;if(t&&n.has(t)){const t=e.opening&&Array.isArray(e.opening.attributes)&&e.opening.attributes.length>0,n=e.children||[],s=1===n.length&&("JSXText"===n[0]?.type||"JSXExpressionContainer"===n[0]?.type&&void 0!==i(n[0].expression));!t&&(!n.length||s)||o.set(e,y++)}else o.set(e,y++)}const f=a(u,o.size>0?o:void 0,!1);l+=`<${t}>${p(f)}</${t}>`}else{const e=a(u,void 0,!1);l+=`<${c}>${p(e)}</${c}>`}}}else{const e=f.children||[];if(e.some(e=>e&&("JSXText"===e.type||"JSXExpressionContainer"===e.type)&&-1!==r.indexOf(e))){const t=r.indexOf(f),n=a(e,void 0);l+=`<${t}>${p(n)}</${t}>`}else{const i=new Map;let s=0;for(const t of e)if(t&&"JSXElement"===t.type){const e=t.opening&&t.opening.name&&"Identifier"===t.opening.name.type?t.opening.name.value:void 0;if(e&&n.has(e)){const e=t.opening&&Array.isArray(t.opening.attributes)&&t.opening.attributes.length>0,n=t.children||[],o=1===n.length&&"JSXText"===n[0]?.type;!e&&(!n.length||o)||i.set(t,s++)}else i.set(t,s++)}const o=t&&t.has(f)?t.get(f):r.indexOf(f),u=a(e,i.size?i:void 0);l+=`<${o}>${p(u)}</${o}>`}}continue}"JSXFragment"!==f.type||(l+=a(f.children||[]))}}return l}const l=a(e,void 0,!0),u=String(l).replace(/<br \/>\s*\n\s*/g,"<br />").replace(/\s+/g," ");return u.replace(/\s+\./g,".").trim()}(n.children,o);let X;const h="JSXAttribute"===p?.type?s(p):void 0;if(void 0!==h)X=h;else{const e=o.extract.defaultValue;X="string"==typeof e?e:""}let E,b;if("JSXAttribute"===r?.type){if("StringLiteral"===r.value?.type){if(E=r.value,b=E.value,!b||""===b.trim())return null;if(m&&"StringLiteral"===E.type){const e=o.extract.nsSeparator??":",t=E.value;if(e&&t.startsWith(`${m}${e}`)){if(b=t.slice(`${m}${e}`.length),!b||""===b.trim())return null;E={...E,value:b}}}}else"JSXExpressionContainer"===r.value?.type&&"JSXEmptyExpression"!==r.value.expression.type&&(E=r.value.expression);if(!E)return null}p||!b||J.trim()?!p&&J.trim()&&(X=J):X=b;return{keyExpression:E,serializedChildren:J,ns:m,defaultValue:X,hasCount:y,isOrdinal:d,contextExpression:S,optionsNode:c,explicitDefault:void 0!==h||(e=>{if(!e||!Array.isArray(e.properties))return!1;for(const t of e.properties)if(t&&"KeyValueProperty"===t.type&&t.key){const e="Identifier"===t.key.type&&t.key.value||"StringLiteral"===t.key.type&&t.key.value;if("string"==typeof e&&e.startsWith("defaultValue"))return!0}return!1})(c)}}export{o as extractFromTransComponent};
1
+ import{getObjectPropValueExpression as e,getObjectPropValue as t,isSimpleTemplateLiteral as n}from"./ast-utils.js";function i(e){if(e)return"StringLiteral"===e.type?e.value:"TemplateLiteral"===e.type&&n(e)?e.quasis[0].cooked:void 0}function s(e){return"StringLiteral"===e.value?.type?e.value.value:"JSXExpressionContainer"===e.value?.type?i(e.value.expression):void 0}function r(n,r){const o=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"i18nKey"===e.name.value),p=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"defaults"===e.name.value),a=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"count"===e.name.value),l=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"values"===e.name.value);let u;a||"JSXAttribute"!==l?.type||"JSXExpressionContainer"!==l.value?.type||"ObjectExpression"!==l.value.expression.type||(u=e(l.value.expression,"count"));const c=!!a||!!u,f=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"tOptions"===e.name.value),y="JSXAttribute"===f?.type&&"JSXExpressionContainer"===f.value?.type&&"ObjectExpression"===f.value.expression.type?f.value.expression:void 0,g=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),v=!!g,d=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"context"===e.name.value);let S="JSXAttribute"===d?.type&&"JSXExpressionContainer"===d.value?.type?d.value.expression:"JSXAttribute"===d?.type&&"StringLiteral"===d.value?.type?d.value:void 0;const x=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ns"===e.name.value);let m;m="JSXAttribute"===x?.type?s(x):void 0,y&&(void 0===m&&(m=t(y,"ns")),void 0===S&&(S=e(y,"context")));const J=function(e,t){if(!e||0===e.length)return"";const n=new Set(t.extract.transKeepBasicHtmlNodesFor??["br","strong","i","p"]),s=e=>e&&"JSXText"===e.type&&/^\s*$/.test(e.value)&&e.value.includes("\n");function r(e,t,o=!1,p=!1){if(!e||!e.length)return;const a=p&&e.filter(e=>e&&"JSXElement"===e.type&&"p"===e.opening?.name?.value).length>1;let l=0,u=e.length-1;for(;l<=u&&s(e[l]);)l++;for(;u>=l&&s(e[u]);)u--;const c=l<=u?e.slice(l,u+1):[],f=c.some(e=>e&&("JSXElement"===e.type||"JSXFragment"===e.type));for(let e=0;e<c.length;e++){const p=c[e];if(p)if("JSXText"!==p.type){if("JSXExpressionContainer"===p.type){if(o&&!f&&p.expression){const e=p.expression.type;if("ObjectExpression"===e){const e=p.expression.properties&&p.expression.properties[0];if(e&&"KeyValueProperty"===e.type)continue}const t=i(p.expression);if(void 0!==t){if(!(/^\s*$/.test(t)&&!t.includes("\n")))continue}else if("Identifier"===e||"MemberExpression"===e||"CallExpression"===e)continue}const n=i(p.expression);if(void 0!==n){const i=/^\s*$/.test(n)&&!n.includes("\n"),r=c[e-1],o=c[e+1];if(i){const n=c[e+2];if(o&&"JSXText"===o.type&&s(o)&&n&&("JSXElement"===n.type||"JSXFragment"===n.type)){const t=c[e-1],n=c[e-2];if(!t||"JSXText"!==t.type&&n&&"JSXExpressionContainer"===n.type)continue}if(r&&("JSXElement"===r.type||"JSXFragment"===r.type)&&o&&"JSXText"===o.type&&s(o))continue;const i=!o||"JSXText"===o.type&&!s(o);if(r&&"JSXText"===r.type&&i){const e=t[t.length-1];if(e&&"JSXText"===e.type){e.value=String(e.value)+p.expression.value;continue}}if(r&&("JSXElement"===r.type||"JSXFragment"===r.type)&&o&&"JSXText"===o.type&&s(o))continue}}t.push(p);continue}if("JSXElement"===p.type){const e=p.opening&&p.opening.name&&"Identifier"===p.opening.name.type?p.opening.name.value:void 0;if(e&&n.has(e)){const n=p.opening&&Array.isArray(p.opening.attributes)&&p.opening.attributes.length>0,s=p.children||[],o=1===s.length&&("JSXText"===s[0]?.type||"JSXExpressionContainer"===s[0]?.type&&void 0!==i(s[0].expression)),l=!s.length,u=o;n&&!o?(t.push(p),r(p.children||[],t,!0)):l?t.push(p):u||("p"===e&&a?(t.push(p),r(p.children||[],t,!0,!1)):r(p.children||[],t,!1,!1));continue}t.push(p),r(p.children||[],t,!0);continue}"JSXFragment"!==p.type||r(p.children||[],t,o)}else{if(o&&!f)continue;if(o&&s(p))continue;if(s(p)){const n=c[e-1],i=c[e+1];if(n&&("JSXElement"===n.type||"JSXFragment"===n.type)&&i&&("JSXElement"===i.type||"JSXFragment"===i.type))continue;const s=t[t.length-1],r=c[e-1];if(s){if(r&&"JSXExpressionContainer"===r.type)continue;if("JSXText"===s.type&&r&&"JSXText"===r.type){s.value=String(s.value)+p.value;continue}}}if(o&&f&&0===e)continue;t.push(p)}}}const o=[];r(e,o,!1,!0);const p=new Set,a=e=>String(e).replace(/^\s*\n\s*/g,"").replace(/\s*\n\s*$/g,"");function l(e,t,r=!1){if(!e||0===e.length)return"";let u="",c=0;for(let f=0;f<e.length;f++){const y=e[f];if(y){if("JSXText"===y.type){if(s(y))continue;const t=e[f+1],i=e[f-1];if(i&&"JSXElement"===i.type){const t="Identifier"===i.opening?.name?.type?i.opening.name.value:void 0,s=t&&n.has(t),r=0===(i.children||[]).length;if(s&&r&&/^\s*\n\s*/.test(y.value)){const e=y.value.replace(/^\s*\n\s*/,"");if(e){u+=e;continue}continue}if(!s&&/^\s*\n\s*/.test(y.value)){const t=y.value.replace(/^\s*\n\s*/,"");if(t){const n=e[f-2];if(n&&"JSXText"===n.type){const e=n.value.replace(/\n\s*$/,""),i=/[A-Za-z0-9]$/.test(e),s=/^[A-Za-z0-9]/.test(t),r=/^[a-z]/.test(t);if(i&&s&&r){u+=t;continue}}u+=" "+t;continue}continue}}if(/\n\s*$/.test(y.value)&&t&&"JSXElement"===t.type){const i=y.value.replace(/\n\s*$/,"");if(i.trim()){const r="Identifier"===t.opening?.name?.type?t.opening.name.value:void 0,o=r&&n.has(r),a=(t.children||[]).length>0,l=/\s\n/.test(y.value),c=e[f+2],g=c&&"JSXText"===c.type&&!s(c)&&/[a-zA-Z0-9]/.test(c.value),v=!!(t.opening&&Array.isArray(t.opening.attributes)&&t.opening.attributes.length>0),d=/^\s/.test(i)&&!/^\n/.test(i),S=i.trim(),x=d?" "+S:S,m=/[A-Za-z0-9]$/.test(S),J=c&&"string"==typeof c.value&&/^[A-Za-z0-9]/.test(c.value.trim()),X=c&&"string"==typeof c.value&&/^[a-z]/.test(c.value.trim()),h=c&&"string"==typeof c.value&&/^\s/.test(c.value),$=v&&a&&g&&!(m&&J&&X&&!l&&!d&&!h);m&&J&&X&&!l&&!d&&!h&&p.add(t),u+=l||o&&a||!o&&g&&d||$?x+" ":x;continue}}u+=y.value;continue}if("JSXExpressionContainer"===y.type){const e=y.expression;if(!e)continue;const t=i(e);if(void 0!==t)u+=t;else if("Identifier"===e.type)u+=`{{${e.value}}}`;else if("ObjectExpression"===e.type){const t=e.properties[0];t&&"KeyValueProperty"===t.type&&t.key&&"Identifier"===t.key.type?u+=`{{${t.key.value}}}`:t&&"Identifier"===t.type?u+=`{{${t.value}}}`:u+="{{value}}"}else"MemberExpression"===e.type&&e.property&&"Identifier"===e.property.type?u+=`{{${e.property.value}}}`:"CallExpression"===e.type&&"Identifier"===e.callee?.type?u+=`{{${e.callee.value}}}`:u+="{{value}}";continue}if("JSXElement"===y.type){let p;y.opening&&y.opening.name&&"Identifier"===y.opening.name.type&&(p=y.opening.name.value);const g=r?c:void 0;if(r&&"JSXElement"===y.type&&c++,p&&n.has(p)){const r=y.opening&&Array.isArray(y.opening.attributes)&&y.opening.attributes.length>0,c=y.children||[],v=c.length>0,d=1===c.length&&("JSXText"===c[0]?.type||"JSXExpressionContainer"===c[0]?.type&&void 0!==i(c[0].expression));if(!v||d){const t=d?l(c,void 0):"";if(""!==String(t).trim())u+=`<${p}>${t}</${p}>`;else{const t=e[f-1];t&&"JSXText"===t.type&&/\n\s*$/.test(t.value)&&(u=u.replace(/\s+$/,"")),u+=`<${p} />`}}else if(r&&!d){const e=c;if(e.some(e=>e&&("JSXText"===e.type||"JSXExpressionContainer"===e.type)&&-1!==o.indexOf(e))){const t=o.indexOf(y),n=l(e,void 0);u+=`<${t}>${a(n)}</${t}>`}else{const e=t&&t.has(y)?t.get(y):o.indexOf(y),s=new Map;let r="number"==typeof e&&e>=0?e:0;for(const e of c)if(e&&"JSXElement"===e.type){const t=e.opening&&e.opening.name&&"Identifier"===e.opening.name.type?e.opening.name.value:void 0;if(t&&n.has(t)){const t=e.opening&&Array.isArray(e.opening.attributes)&&e.opening.attributes.length>0,n=e.children||[],o=1===n.length&&("JSXText"===n[0]?.type||"JSXExpressionContainer"===n[0]?.type&&void 0!==i(n[0].expression));!t&&(!n.length||o)||s.set(e,r++)}else s.set(e,r++)}const p=l(c,s.size?s:void 0);u+=`<${e}>${a(p)}</${e}>`}}else{const e=o.indexOf(y);if(-1!==e){const t=void 0!==g?g:e;if((()=>{let e=!1;for(const t of c)if(t)if("JSXElement"!==t.type){if("JSXExpressionContainer"===t.type&&-1!==o.indexOf(t))return e;if("JSXText"===t.type&&-1!==o.indexOf(t)){if(s(t))continue;if(!e)return!0;if(c.slice(c.indexOf(t)+1).some(e=>e&&"JSXElement"===e.type))return!0}}else e=!0;return!1})()){const e=l(c,void 0,!1);u+=`<${t}>${a(e)}</${t}>`;continue}const r=new Map;let p=t;for(const e of c)if(e&&"JSXElement"===e.type){const t=e.opening&&e.opening.name&&"Identifier"===e.opening.name.type?e.opening.name.value:void 0;if(t&&n.has(t)){const t=e.opening&&Array.isArray(e.opening.attributes)&&e.opening.attributes.length>0,n=e.children||[],s=1===n.length&&("JSXText"===n[0]?.type||"JSXExpressionContainer"===n[0]?.type&&void 0!==i(n[0].expression));!t&&(!n.length||s)||r.set(e,p++)}else r.set(e,p++)}const f=l(c,r.size>0?r:void 0,!1);u+=`<${t}>${a(f)}</${t}>`}else{const e=l(c,void 0,!1);u+=`<${p}>${a(e)}</${p}>`}}}else{const e=y.children||[];if(e.some(e=>e&&("JSXText"===e.type||"JSXExpressionContainer"===e.type)&&-1!==o.indexOf(e))){const t=o.indexOf(y),n=l(e,void 0);u+=`<${t}>${a(n)}</${t}>`}else{const i=new Map,s=t&&t.has(y)?t.get(y):o.indexOf(y);let r="number"==typeof s&&s>=0?s:0;for(const t of e)if(t&&"JSXElement"===t.type){const e=t.opening&&t.opening.name&&"Identifier"===t.opening.name.type?t.opening.name.value:void 0;if(e&&n.has(e)){const e=t.opening&&Array.isArray(t.opening.attributes)&&t.opening.attributes.length>0,n=t.children||[],s=1===n.length&&"JSXText"===n[0]?.type;!e&&(!n.length||s)||i.set(t,r++)}else i.set(t,r++)}const p=l(e,i.size?i:void 0);u+=`<${s}>${a(p)}</${s}>`}}continue}"JSXFragment"!==y.type||(u+=l(y.children||[]))}}return u}const u=l(e,void 0,!0),c=String(u).replace(/<br \/>\s*\n\s*/g,"<br />"),f=String(c),y=new Set;if(p&&p.size>0)for(let e=0;e<o.length;e++)p.has(o[e])&&y.add(e);let g=String(f);if(y&&y.size>0)for(const e of y)try{g=g.replace(new RegExp("\\s+<"+e+">","g"),"<"+e+">"),g=g.replace(new RegExp("<\\/"+e+">\\s+","g"),"</"+e+">")}catch(e){}g=g.replace(/<\/(\d+)>\s*\n\s*(\S)/g,(e,t,n)=>{const i=Number(t);return y.has(i)?`</${t}>${n}`:`</${t}> ${n}`}),g=g.replace(/(\S)\s*\n\s*<(\d+)/g,(e,t,n)=>{const i=Number(n);return y.has(i)?`${t}<${n}`:`${t} <${n}`}),g=g.replace(/\s*\n\s*/g," "),g=g.replace(/\s+/g," "),g=g.replace(/\s+\./g,".");const v=g.trim();let d=String(v);if(y&&y.size>0)for(const e of y)try{d=d.replace(new RegExp("[\\s\\u00A0]+<"+e+">","g"),"<"+e+">"),d=d.replace(new RegExp("<\\/"+e+">[\\s\\u00A0]+","g"),"</"+e+">")}catch(e){}try{for(let e=0;e<o.length;e++){const t=o[e];if(!t||"JSXElement"!==t.type)continue;const n=o[e-1],i=o[e+1];if(!n||!i)continue;if("JSXText"!==n.type||"JSXText"!==i.type)continue;const s=String(n.value),r=String(i.value),p=s.replace(/\n\s*$/,""),a=/[A-Za-z0-9]$/.test(p),l=/^[A-Za-z0-9]/.test(r.trim()),u=/^[a-z]/.test(r.trim()),c=/\s\n/.test(s),f=r&&/^\s/.test(r)&&!/^\n/.test(r);if(a&&l&&u&&!c&&!f){const t=e;d=d.replace(new RegExp("\\s+<"+t+">","g"),"<"+t+">")}}}catch(e){}return d.trim()}(n.children,r);let X;const h="JSXAttribute"===p?.type?s(p):void 0;if(void 0!==h)X=h;else{const e=r.extract.defaultValue;X="string"==typeof e?e:""}let $,E;if("JSXAttribute"===o?.type){if("StringLiteral"===o.value?.type){if($=o.value,E=$.value,!E||""===E.trim())return null;if(m&&"StringLiteral"===$.type){const e=r.extract.nsSeparator??":",t=$.value;if(e&&t.startsWith(`${m}${e}`)){if(E=t.slice(`${m}${e}`.length),!E||""===E.trim())return null;$={...$,value:E}}}}else"JSXExpressionContainer"===o.value?.type&&"JSXEmptyExpression"!==o.value.expression.type&&($=o.value.expression);if(!$)return null}p||!E||J.trim()?!p&&J.trim()&&(X=J):X=E;return{keyExpression:$,serializedChildren:J,ns:m,defaultValue:X,hasCount:c,isOrdinal:v,contextExpression:S,optionsNode:y,explicitDefault:void 0!==h||(e=>{if(!e||!Array.isArray(e.properties))return!1;for(const t of e.properties)if(t&&"KeyValueProperty"===t.type&&t.key){const e="Identifier"===t.key.type&&t.key.value||"StringLiteral"===t.key.type&&t.key.value;if("string"==typeof e&&e.startsWith("defaultValue"))return!0}return!1})(y)}}export{r as extractFromTransComponent};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "i18next-cli",
3
- "version": "1.24.3",
3
+ "version": "1.24.5",
4
4
  "description": "A unified, high-performance i18next CLI.",
5
5
  "type": "module",
6
6
  "bin": {
package/src/cli.ts CHANGED
@@ -23,7 +23,7 @@ const program = new Command()
23
23
  program
24
24
  .name('i18next-cli')
25
25
  .description('A unified, high-performance i18next CLI.')
26
- .version('1.24.3')
26
+ .version('1.24.5')
27
27
 
28
28
  // new: global config override option
29
29
  program.option('-c, --config <path>', 'Path to i18next-cli config file (overrides detection)')
@@ -228,16 +228,15 @@ export class ASTVisitors {
228
228
  }
229
229
 
230
230
  /**
231
- * Sets the current file path used by the extractor.
232
- *
233
- * @param file - The file path (absolute or relative) to set as the current processing context.
234
- * @remarks
235
- * Updating the current file allows subsequent AST visitors and extraction logic to
236
- * associate nodes, messages, and errors with the correct source file.
231
+ * Sets the current file path and code used by the extractor.
232
+ * Also resets the search index for location tracking.
237
233
  */
238
234
  public setCurrentFile (file: string, code: string): void {
239
235
  this.currentFile = file
240
236
  this.currentCode = code
237
+ // Reset search indexes when processing a new file
238
+ this.callExpressionHandler.resetSearchIndex()
239
+ this.jsxHandler.resetSearchIndex()
241
240
  }
242
241
 
243
242
  /**
@@ -11,6 +11,7 @@ export class CallExpressionHandler {
11
11
  public objectKeys = new Set<string>()
12
12
  private getCurrentFile: () => string
13
13
  private getCurrentCode: () => string
14
+ private lastSearchIndex: number = 0
14
15
 
15
16
  constructor (
16
17
  config: Omit<I18nextToolkitConfig, 'plugins'>,
@@ -29,18 +30,52 @@ export class CallExpressionHandler {
29
30
  }
30
31
 
31
32
  /**
32
- * Helper method to calculate line and column from byte offset.
33
- * SWC provides byte offsets in span.start, not line/column directly.
33
+ * Reset the search index when starting to process a new file.
34
+ * This should be called before processing each file.
34
35
  */
35
- private getLocationFromSpan (span: any): { line: number, column: number } | undefined {
36
- if (!span || typeof span.start !== 'number') return undefined
36
+ public resetSearchIndex (): void {
37
+ this.lastSearchIndex = 0
38
+ }
37
39
 
40
+ /**
41
+ * Helper method to calculate line and column from a position in the code.
42
+ * Uses string searching instead of SWC span offsets to avoid accumulation bugs.
43
+ */
44
+ private getLocationFromNode (node: any): { line: number, column: number } | undefined {
38
45
  const code = this.getCurrentCode()
39
- const offset = span.start
40
46
 
41
- // Calculate line and column from byte offset
42
- const upToOffset = code.substring(0, offset)
43
- const lines = upToOffset.split('\n')
47
+ // Extract searchable text from the node
48
+ // For CallExpression, we can search for the key argument
49
+ let searchText: string | undefined
50
+
51
+ if (node.type === 'CallExpression' && node.arguments.length > 0) {
52
+ const firstArg = node.arguments[0].expression
53
+
54
+ if (firstArg.type === 'StringLiteral') {
55
+ // Search for the string literal including quotes
56
+ searchText = firstArg.raw ?? `'${firstArg.value}'`
57
+ } else if (firstArg.type === 'TemplateLiteral') {
58
+ // For template literals, search for the backtick
59
+ searchText = '`'
60
+ }
61
+ }
62
+
63
+ if (!searchText) return undefined
64
+
65
+ // Search for the text starting from last known position
66
+ const position = code.indexOf(searchText, this.lastSearchIndex)
67
+
68
+ if (position === -1) {
69
+ // Not found - might be a parsing issue, skip location tracking
70
+ return undefined
71
+ }
72
+
73
+ // Update last search position for next search
74
+ this.lastSearchIndex = position + searchText.length
75
+
76
+ // Calculate line and column from the position
77
+ const upToPosition = code.substring(0, position)
78
+ const lines = upToPosition.split('\n')
44
79
 
45
80
  return {
46
81
  line: lines.length,
@@ -396,8 +431,8 @@ export class CallExpressionHandler {
396
431
 
397
432
  // 5. Default case: Add the simple key
398
433
  {
399
- // ✅ Use the helper method to calculate proper line/column
400
- const location = node.span ? this.getLocationFromSpan(node.span) : undefined
434
+ // ✅ Use the helper method to find location by searching the code
435
+ const location = this.getLocationFromNode(node)
401
436
 
402
437
  this.pluginContext.addKey({
403
438
  key: finalKey,
@@ -9,33 +9,56 @@ export class JSXHandler {
9
9
  private pluginContext: PluginContext
10
10
  private expressionResolver: ExpressionResolver
11
11
  private getCurrentFile: () => string
12
- private getCurrentCode: () => string // ✅ Add this
12
+ private getCurrentCode: () => string
13
+ private lastSearchIndex: number = 0
13
14
 
14
15
  constructor (
15
16
  config: Omit<I18nextToolkitConfig, 'plugins'>,
16
17
  pluginContext: PluginContext,
17
18
  expressionResolver: ExpressionResolver,
18
19
  getCurrentFile: () => string,
19
- getCurrentCode: () => string // ✅ Add parameter
20
+ getCurrentCode: () => string
20
21
  ) {
21
22
  this.config = config
22
23
  this.pluginContext = pluginContext
23
24
  this.expressionResolver = expressionResolver
24
25
  this.getCurrentFile = getCurrentFile
25
- this.getCurrentCode = getCurrentCode // ✅ Store it
26
+ this.getCurrentCode = getCurrentCode
26
27
  }
27
28
 
28
29
  /**
29
- * Helper method to calculate line and column from byte offset.
30
+ * Reset the search index when starting to process a new file.
30
31
  */
31
- private getLocationFromSpan (span: any): { line: number, column: number } | undefined {
32
- if (!span || typeof span.start !== 'number') return undefined
32
+ public resetSearchIndex (): void {
33
+ this.lastSearchIndex = 0
34
+ }
33
35
 
36
+ /**
37
+ * Helper method to calculate line and column by searching for the JSX element in the code.
38
+ */
39
+ private getLocationFromNode (node: any): { line: number, column: number } | undefined {
34
40
  const code = this.getCurrentCode()
35
- const offset = span.start
36
41
 
37
- const upToOffset = code.substring(0, offset)
38
- const lines = upToOffset.split('\n')
42
+ // For JSXElement, search for the opening tag
43
+ let searchText: string | undefined
44
+
45
+ if (node.type === 'JSXElement' && node.opening) {
46
+ const tagName = node.opening.name?.value
47
+ if (tagName) {
48
+ searchText = `<${tagName}`
49
+ }
50
+ }
51
+
52
+ if (!searchText) return undefined
53
+
54
+ const position = code.indexOf(searchText, this.lastSearchIndex)
55
+
56
+ if (position === -1) return undefined
57
+
58
+ this.lastSearchIndex = position + searchText.length
59
+
60
+ const upToPosition = code.substring(0, position)
61
+ const lines = upToPosition.split('\n')
39
62
 
40
63
  return {
41
64
  line: lines.length,
@@ -72,8 +95,8 @@ export class JSXHandler {
72
95
 
73
96
  const { contextExpression, optionsNode, defaultValue, hasCount, isOrdinal, serializedChildren } = extractedAttributes
74
97
 
75
- // Extract location information using the helper method
76
- const location = node.span ? this.getLocationFromSpan(node.span) : undefined
98
+ // Extract location information using the helper method
99
+ const location = this.getLocationFromNode(node)
77
100
  const locations = location
78
101
  ? [{
79
102
  file: this.getCurrentFile(),
@@ -578,6 +578,10 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
578
578
  const globalSlots: any[] = []
579
579
  collectSlots(children, globalSlots, false, true)
580
580
 
581
+ // Track ELEMENT NODES that MUST be tight (no spaces) because the element splits a word across a newline.
582
+ // We'll map these node refs to numeric global indices later before string cleanup.
583
+ const tightNoSpaceNodes = new Set<any>()
584
+
581
585
  // Trim only newline-only indentation at the edges of serialized inner text.
582
586
  // This preserves single leading/trailing spaces which are meaningful between inline placeholders.
583
587
  const trimFormattingEdges = (s: string) =>
@@ -624,6 +628,34 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
624
628
  // If nothing left after trimming, skip this node
625
629
  continue
626
630
  }
631
+
632
+ // If previous node is a non-preserved element (an indexed placeholder)
633
+ // and the current text starts with a formatting newline, detect
634
+ // the "word-split" scenario: previous meaningful text (before the element)
635
+ // ends with an alnum char and this trimmed text starts with alnum.
636
+ // In that case do NOT insert any separating space — the inline element
637
+ // split a single word across lines.
638
+ if (!prevIsPreservedTag && /^\s*\n\s*/.test(node.value)) {
639
+ const trimmedValue = node.value.replace(/^\s*\n\s*/, '')
640
+ if (trimmedValue) {
641
+ const prevPrev = nodes[i - 2]
642
+ if (prevPrev && prevPrev.type === 'JSXText') {
643
+ const prevPrevTrimmed = prevPrev.value.replace(/\n\s*$/, '')
644
+ const prevEndsAlnum = /[A-Za-z0-9]$/.test(prevPrevTrimmed)
645
+ const nextStartsAlnum = /^[A-Za-z0-9]/.test(trimmedValue)
646
+ const nextStartsLowercase = /^[a-z]/.test(trimmedValue)
647
+ if (prevEndsAlnum && nextStartsAlnum && nextStartsLowercase) {
648
+ // word-split: do NOT insert a space
649
+ out += trimmedValue
650
+ continue
651
+ }
652
+ }
653
+ // non-word-split: insert a separating space before the trimmed text
654
+ out += ' ' + trimmedValue
655
+ continue
656
+ }
657
+ continue
658
+ }
627
659
  }
628
660
 
629
661
  // If this text node ends with newline+whitespace and is followed by an element,
@@ -648,8 +680,13 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
648
680
  !isFormattingWhitespace(nodeAfterNext) &&
649
681
  /[a-zA-Z0-9]/.test(nodeAfterNext.value)
650
682
 
683
+ // Does the next element have attributes? (helps decide spacing for tags like <a href="...">)
684
+ const nextHasAttrs = !!(nextNode.opening && Array.isArray((nextNode.opening as any).attributes) && (nextNode.opening as any).attributes.length > 0)
685
+
651
686
  // Preserve leading whitespace
652
- const hasLeadingSpace = /^\s/.test(textWithoutTrailingNewline)
687
+ // Only treat a real leading space (not a leading newline + indentation) as "leading space"
688
+ const hasLeadingSpace = /^\s/.test(textWithoutTrailingNewline) && !/^\n/.test(textWithoutTrailingNewline)
689
+
653
690
  const trimmed = textWithoutTrailingNewline.trim()
654
691
  const withLeading = hasLeadingSpace ? ' ' + trimmed : trimmed
655
692
 
@@ -657,7 +694,46 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
657
694
  // 1. There was an explicit space before the newline, OR
658
695
  // 2. The next element is NOT a preserved tag AND has text after (word boundary)
659
696
  // Preserved tags like <br />, <p>, etc. provide their own separation
660
- if (hasSpaceBeforeNewline || (isPreservedTag && nextHasChildren) || (!isPreservedTag && hasTextAfter)) {
697
+ // Require an explicit leading space for the "non-preserved + hasTextAfter" case
698
+ // Detect "word-split" case more strictly:
699
+ // - previous trimmed ends with alnum
700
+ // - the text after the element starts with alnum
701
+ // - there was no explicit space before the newline and no explicit leading space,
702
+ // - AND the text-after does NOT itself start with an explicit space.
703
+ const prevEndsWithAlnum = /[A-Za-z0-9]$/.test(trimmed)
704
+ const nextStartsWithAlnum = nodeAfterNext && typeof nodeAfterNext.value === 'string' && /^[A-Za-z0-9]/.test(nodeAfterNext.value.trim())
705
+ const nextStartsWithLowercase = nodeAfterNext && typeof nodeAfterNext.value === 'string' && /^[a-z]/.test(nodeAfterNext.value.trim())
706
+ const nextHasLeadingSpace = nodeAfterNext && typeof nodeAfterNext.value === 'string' && /^\s/.test(nodeAfterNext.value)
707
+
708
+ // Only treat as a word-split (no space) when the following word begins
709
+ // with a lowercase letter — this avoids removing spaces between separate
710
+ // capitalized / distinct words like "First <1>Second".
711
+ const shouldInsertForNextWithAttrs = nextHasAttrs && nextHasChildren && hasTextAfter && !(
712
+ prevEndsWithAlnum &&
713
+ nextStartsWithAlnum &&
714
+ nextStartsWithLowercase &&
715
+ !hasSpaceBeforeNewline &&
716
+ !hasLeadingSpace &&
717
+ !nextHasLeadingSpace
718
+ )
719
+
720
+ // Persist a "tight" decision so post-normalization can remove any artificial
721
+ // spaces that were introduced by whitespace collapsing/newline handling.
722
+ // This ensures cases like "word\n <1>link</1>\n word" become "word<1>link</1>word".
723
+ const isWordSplitStrict = prevEndsWithAlnum && nextStartsWithAlnum && nextStartsWithLowercase && !hasSpaceBeforeNewline && !hasLeadingSpace && !nextHasLeadingSpace
724
+ if (isWordSplitStrict) {
725
+ // mark the actual element node; map to numeric index later
726
+ tightNoSpaceNodes.add(nextNode)
727
+ }
728
+
729
+ if (
730
+ hasSpaceBeforeNewline ||
731
+ (isPreservedTag && nextHasChildren) ||
732
+ // non-preserved with text after must have an explicit leading space
733
+ (!isPreservedTag && hasTextAfter && hasLeadingSpace) ||
734
+ // next element with attrs: only when not a word-split (see above)
735
+ shouldInsertForNextWithAttrs
736
+ ) {
661
737
  out += withLeading + ' '
662
738
  } else {
663
739
  out += withLeading
@@ -761,25 +837,35 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
761
837
  const inner = visitNodes(childrenLocal, undefined)
762
838
  out += `<${idx}>${trimFormattingEdges(inner)}</${idx}>`
763
839
  } else {
840
+ // Determine the starting index for local child indexes:
841
+ // reuse the parent's global index so nested placeholders get
842
+ // indices consistent with parent indexing (avoid starting at 0).
843
+ const idx = localIndexMap && localIndexMap.has(node) ? localIndexMap.get(node) : globalSlots.indexOf(node)
764
844
  const childrenLocalMap = new Map<any, number>()
765
- let localIdxCounter = 0
766
- for (const ch of childrenLocal) {
845
+ let localIdxCounter = typeof idx === 'number' && idx >= 0 ? idx : 0
846
+
847
+ for (const ch of children) {
767
848
  if (!ch) continue
768
849
  if (ch.type === 'JSXElement') {
769
850
  const chTag = ch.opening && ch.opening.name && ch.opening.name.type === 'Identifier'
770
851
  ? ch.opening.name.value
771
852
  : undefined
772
853
  if (chTag && allowedTags.has(chTag)) {
854
+ // Check if this child will be preserved as literal HTML
773
855
  const chHasAttrs =
774
856
  ch.opening &&
775
857
  Array.isArray((ch.opening as any).attributes) &&
776
858
  (ch.opening as any).attributes.length > 0
777
859
  const chChildren = ch.children || []
778
- const chIsSingleText = chChildren.length === 1 && chChildren[0]?.type === 'JSXText'
779
- // Only skip indexing if it would be preserved as literal
780
- if (!chHasAttrs && (!chChildren.length || chIsSingleText)) {
781
- // Will be preserved, don't index
782
- } else {
860
+ const chIsSinglePureText =
861
+ chChildren.length === 1 && (
862
+ chChildren[0]?.type === 'JSXText' ||
863
+ (chChildren[0]?.type === 'JSXExpressionContainer' &&
864
+ getStringLiteralFromExpression(chChildren[0].expression) !== undefined)
865
+ )
866
+ const chWillBePreserved = !chHasAttrs && (!chChildren.length || chIsSinglePureText)
867
+ if (!chWillBePreserved) {
868
+ // Will be indexed, add to local map
783
869
  childrenLocalMap.set(ch, localIdxCounter++)
784
870
  }
785
871
  } else {
@@ -788,8 +874,7 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
788
874
  }
789
875
  }
790
876
 
791
- const idx = localIndexMap && localIndexMap.has(node) ? localIndexMap.get(node) : globalSlots.indexOf(node)
792
- const inner = visitNodes(childrenLocal, childrenLocalMap.size ? childrenLocalMap : undefined)
877
+ const inner = visitNodes(children, childrenLocalMap.size ? childrenLocalMap : undefined)
793
878
  out += `<${idx}>${trimFormattingEdges(inner)}</${idx}>`
794
879
  }
795
880
  } else {
@@ -914,7 +999,9 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
914
999
  out += `<${idx}>${trimFormattingEdges(inner)}</${idx}>`
915
1000
  } else {
916
1001
  const childrenLocalMap = new Map<any, number>()
917
- let localIdxCounter = 0
1002
+ const idx = localIndexMap && localIndexMap.has(node) ? localIndexMap.get(node) : globalSlots.indexOf(node)
1003
+ let localIdxCounter = typeof idx === 'number' && idx >= 0 ? idx : 0
1004
+
918
1005
  for (const ch of children) {
919
1006
  if (!ch) continue
920
1007
  if (ch.type === 'JSXElement') {
@@ -940,7 +1027,6 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
940
1027
  }
941
1028
  }
942
1029
 
943
- const idx = localIndexMap && localIndexMap.has(node) ? localIndexMap.get(node) : globalSlots.indexOf(node)
944
1030
  const inner = visitNodes(children, childrenLocalMap.size ? childrenLocalMap : undefined)
945
1031
  out += `<${idx}>${trimFormattingEdges(inner)}</${idx}>`
946
1032
  }
@@ -965,11 +1051,94 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
965
1051
  // 1. First, handle <br /> followed by whitespace+newline (boundary formatting)
966
1052
  const afterBrCleanup = String(result).replace(/<br \/>\s*\n\s*/g, '<br />')
967
1053
 
968
- // 2. Then normalize remaining whitespace sequences to single space
969
- const normalized = afterBrCleanup.replace(/\s+/g, ' ')
1054
+ const raw = String(afterBrCleanup)
1055
+
1056
+ const tightNoSpaceIndices = new Set<number>()
970
1057
 
971
- // 3. Remove space before period at end
972
- const finalResult = normalized.replace(/\s+\./g, '.')
1058
+ // Map node-based tight markers into numeric global-slot indices (used by later regex passes).
1059
+ if (tightNoSpaceNodes && tightNoSpaceNodes.size > 0) {
1060
+ for (let i = 0; i < globalSlots.length; i++) {
1061
+ if (tightNoSpaceNodes.has(globalSlots[i])) tightNoSpaceIndices.add(i)
1062
+ }
1063
+ }
1064
+
1065
+ // 1) Remove spaces around explicitly-marked tight indices (word-splits)
1066
+ let tmp = String(raw)
1067
+ if (tightNoSpaceIndices && tightNoSpaceIndices.size > 0) {
1068
+ for (const id of tightNoSpaceIndices) {
1069
+ try {
1070
+ tmp = tmp.replace(new RegExp('\\s+<' + id + '>', 'g'), '<' + id + '>')
1071
+ tmp = tmp.replace(new RegExp('<\\/' + id + '>\\s+', 'g'), '</' + id + '>')
1072
+ } catch (e) { /* ignore */ }
1073
+ }
1074
+ }
1075
+
1076
+ // 2) For non-tight placeholders, if there was a newline boundary between
1077
+ // a closing tag and following text OR between preceding text and an
1078
+ // opening tag, ensure a single separating space. This recovers spaces
1079
+ // that are semantically meaningful when the source had newline
1080
+ // boundaries but not a word-split.
1081
+ tmp = tmp.replace(/<\/(\d+)>\s*\n\s*(\S)/g, (m, idx, after) => {
1082
+ const id = Number(idx)
1083
+ return tightNoSpaceIndices.has(id) ? `</${idx}>${after}` : `</${idx}> ${after}`
1084
+ })
1085
+ tmp = tmp.replace(/(\S)\s*\n\s*<(\d+)/g, (m, before, idx) => {
1086
+ const id = Number(idx)
1087
+ return tightNoSpaceIndices.has(id) ? `${before}<${idx}` : `${before} <${idx}`
1088
+ })
1089
+
1090
+ // 3) Collapse remaining newlines/indentation and whitespace to single spaces,
1091
+ // remove space before period and trim.
1092
+ tmp = tmp.replace(/\s*\n\s*/g, ' ')
1093
+ tmp = tmp.replace(/\s+/g, ' ')
1094
+ tmp = tmp.replace(/\s+\./g, '.')
1095
+ const finalResult = tmp.trim()
1096
+
1097
+ // Final guaranteed cleanup for tight (word-split) placeholders:
1098
+ // remove any spaces (including NBSP) left before opening or after closing numeric placeholders
1099
+ // to ensure "word <1>link</1>word" -> "word<1>link</1>word" when marked tight.
1100
+ let postFinal = String(finalResult)
1101
+ if (tightNoSpaceIndices && tightNoSpaceIndices.size > 0) {
1102
+ for (const id of tightNoSpaceIndices) {
1103
+ try {
1104
+ // remove ordinary whitespace and non-breaking space variants
1105
+ postFinal = postFinal.replace(new RegExp('[\\s\\u00A0]+<' + id + '>', 'g'), '<' + id + '>')
1106
+ postFinal = postFinal.replace(new RegExp('<\\/' + id + '>[\\s\\u00A0]+', 'g'), '</' + id + '>')
1107
+ } catch (e) { /* ignore */ }
1108
+ }
1109
+ }
1110
+
1111
+ // Additional deterministic pass:
1112
+ // If globalSlots show an element whose previous slot is JSXText ending with alnum
1113
+ // and next slot is JSXText starting with alnum, and the original previous text did
1114
+ // not have an explicit space-before-newline nor the next text a leading space,
1115
+ // remove any single space left before the opening placeholder in the final string.
1116
+ try {
1117
+ for (let idx = 0; idx < globalSlots.length; idx++) {
1118
+ const s = globalSlots[idx]
1119
+ if (!s || s.type !== 'JSXElement') continue
1120
+ const prev = globalSlots[idx - 1]
1121
+ const next = globalSlots[idx + 1]
1122
+ if (!prev || !next) continue
1123
+ if (prev.type !== 'JSXText' || next.type !== 'JSXText') continue
1124
+
1125
+ const prevRaw = String(prev.value)
1126
+ const nextRaw = String(next.value)
1127
+ const prevTrimmed = prevRaw.replace(/\n\s*$/, '')
1128
+ const prevEndsAlnum = /[A-Za-z0-9]$/.test(prevTrimmed)
1129
+ const nextStartsAlnum = /^[A-Za-z0-9]/.test(nextRaw.trim())
1130
+ const nextStartsLowercase = /^[a-z]/.test(nextRaw.trim())
1131
+ const hasSpaceBeforeNewline = /\s\n/.test(prevRaw)
1132
+ // Treat newline-leading indentation as NOT an explicit leading space.
1133
+ const nextHasLeadingSpace = nextRaw && /^\s/.test(nextRaw) && !/^\n/.test(nextRaw)
1134
+
1135
+ // Only collapse the space for true word-splits where the next token starts lowercase.
1136
+ if (prevEndsAlnum && nextStartsAlnum && nextStartsLowercase && !hasSpaceBeforeNewline && !nextHasLeadingSpace) {
1137
+ const id = idx
1138
+ postFinal = postFinal.replace(new RegExp('\\s+<' + id + '>', 'g'), '<' + id + '>')
1139
+ }
1140
+ }
1141
+ } catch (e) { /* ignore */ }
973
1142
 
974
- return finalResult.trim()
1143
+ return postFinal.trim()
975
1144
  }
@@ -75,12 +75,8 @@ export declare class ASTVisitors {
75
75
  */
76
76
  getVarFromScope(name: string): ScopeInfo | undefined;
77
77
  /**
78
- * Sets the current file path used by the extractor.
79
- *
80
- * @param file - The file path (absolute or relative) to set as the current processing context.
81
- * @remarks
82
- * Updating the current file allows subsequent AST visitors and extraction logic to
83
- * associate nodes, messages, and errors with the correct source file.
78
+ * Sets the current file path and code used by the extractor.
79
+ * Also resets the search index for location tracking.
84
80
  */
85
81
  setCurrentFile(file: string, code: string): void;
86
82
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"ast-visitors.d.ts","sourceRoot":"","sources":["../../../src/extractor/core/ast-visitors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAQ,MAAM,WAAW,CAAA;AAC7C,OAAO,KAAK,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAE1G,OAAO,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAA;AAInE;;;;;;;;;;;;;;;;;;;;;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,KAAK,CAAiB;IAE9B,IAAW,UAAU,gBAEpB;IAED,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAc;IAC3C,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAoB;IACvD,OAAO,CAAC,QAAQ,CAAC,qBAAqB,CAAuB;IAC7D,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAY;IACvC,OAAO,CAAC,WAAW,CAAa;IAChC,OAAO,CAAC,WAAW,CAAa;IAEhC;;;;;;OAMG;gBAED,MAAM,EAAE,IAAI,CAAC,oBAAoB,EAAE,SAAS,CAAC,EAC7C,aAAa,EAAE,aAAa,EAC5B,MAAM,EAAE,MAAM,EACd,KAAK,CAAC,EAAE,eAAe,EACvB,kBAAkB,CAAC,EAAE,kBAAkB;IAgCzC;;;;;OAKG;IACI,KAAK,CAAE,IAAI,EAAE,MAAM,GAAG,IAAI;IAUjC;;;;;;;;;;;;OAYG;IACH,OAAO,CAAC,IAAI;IAiGZ;;;;;;;;OAQG;IACI,eAAe,CAAE,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,SAAS;IAI5D;;;;;;;OAOG;IACI,cAAc,CAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI;IAKxD;;;;;;OAMG;IACI,cAAc,IAAK,MAAM;IAIhC;;OAEG;IACI,cAAc,IAAK,MAAM;CAGjC"}
1
+ {"version":3,"file":"ast-visitors.d.ts","sourceRoot":"","sources":["../../../src/extractor/core/ast-visitors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAQ,MAAM,WAAW,CAAA;AAC7C,OAAO,KAAK,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAE1G,OAAO,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAA;AAInE;;;;;;;;;;;;;;;;;;;;;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,KAAK,CAAiB;IAE9B,IAAW,UAAU,gBAEpB;IAED,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAc;IAC3C,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAoB;IACvD,OAAO,CAAC,QAAQ,CAAC,qBAAqB,CAAuB;IAC7D,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAY;IACvC,OAAO,CAAC,WAAW,CAAa;IAChC,OAAO,CAAC,WAAW,CAAa;IAEhC;;;;;;OAMG;gBAED,MAAM,EAAE,IAAI,CAAC,oBAAoB,EAAE,SAAS,CAAC,EAC7C,aAAa,EAAE,aAAa,EAC5B,MAAM,EAAE,MAAM,EACd,KAAK,CAAC,EAAE,eAAe,EACvB,kBAAkB,CAAC,EAAE,kBAAkB;IAgCzC;;;;;OAKG;IACI,KAAK,CAAE,IAAI,EAAE,MAAM,GAAG,IAAI;IAUjC;;;;;;;;;;;;OAYG;IACH,OAAO,CAAC,IAAI;IAiGZ;;;;;;;;OAQG;IACI,eAAe,CAAE,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,SAAS;IAI5D;;;OAGG;IACI,cAAc,CAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI;IAQxD;;;;;;OAMG;IACI,cAAc,IAAK,MAAM;IAIhC;;OAEG;IACI,cAAc,IAAK,MAAM;CAGjC"}
@@ -9,12 +9,18 @@ export declare class CallExpressionHandler {
9
9
  objectKeys: Set<string>;
10
10
  private getCurrentFile;
11
11
  private getCurrentCode;
12
+ private lastSearchIndex;
12
13
  constructor(config: Omit<I18nextToolkitConfig, 'plugins'>, pluginContext: PluginContext, logger: Logger, expressionResolver: ExpressionResolver, getCurrentFile: () => string, getCurrentCode: () => string);
13
14
  /**
14
- * Helper method to calculate line and column from byte offset.
15
- * SWC provides byte offsets in span.start, not line/column directly.
15
+ * Reset the search index when starting to process a new file.
16
+ * This should be called before processing each file.
16
17
  */
17
- private getLocationFromSpan;
18
+ resetSearchIndex(): void;
19
+ /**
20
+ * Helper method to calculate line and column from a position in the code.
21
+ * Uses string searching instead of SWC span offsets to avoid accumulation bugs.
22
+ */
23
+ private getLocationFromNode;
18
24
  /**
19
25
  * Processes function call expressions to extract translation keys.
20
26
  *
@@ -1 +1 @@
1
- {"version":3,"file":"call-expression-handler.d.ts","sourceRoot":"","sources":["../../../src/extractor/parsers/call-expression-handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAA6C,MAAM,WAAW,CAAA;AAC1F,OAAO,KAAK,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,EAAgB,SAAS,EAAE,MAAM,aAAa,CAAA;AACvG,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AAG1D,qBAAa,qBAAqB;IAChC,OAAO,CAAC,aAAa,CAAe;IACpC,OAAO,CAAC,MAAM,CAAuC;IACrD,OAAO,CAAC,MAAM,CAAQ;IACtB,OAAO,CAAC,kBAAkB,CAAoB;IACvC,UAAU,cAAoB;IACrC,OAAO,CAAC,cAAc,CAAc;IACpC,OAAO,CAAC,cAAc,CAAc;gBAGlC,MAAM,EAAE,IAAI,CAAC,oBAAoB,EAAE,SAAS,CAAC,EAC7C,aAAa,EAAE,aAAa,EAC5B,MAAM,EAAE,MAAM,EACd,kBAAkB,EAAE,kBAAkB,EACtC,cAAc,EAAE,MAAM,MAAM,EAC5B,cAAc,EAAE,MAAM,MAAM;IAU9B;;;OAGG;IACH,OAAO,CAAC,mBAAmB;IAgB3B;;;;;;;;;;;;;OAaG;IACH,oBAAoB,CAAE,IAAI,EAAE,cAAc,EAAE,YAAY,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,SAAS,GAAG,SAAS,GAAG,IAAI;IAkWxG;;;;;;OAMG;IACH,OAAO,CAAC,4BAA4B;IA8BpC;;;;;;;;;;;;;OAaG;IACH,OAAO,CAAC,sBAAsB;IA2C9B;;;;;;;;;;;OAWG;IACH,OAAO,CAAC,gBAAgB;IA6LxB;;;;;;;;;OASG;IACH,OAAO,CAAC,eAAe;CA2BxB"}
1
+ {"version":3,"file":"call-expression-handler.d.ts","sourceRoot":"","sources":["../../../src/extractor/parsers/call-expression-handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAA6C,MAAM,WAAW,CAAA;AAC1F,OAAO,KAAK,EAAE,aAAa,EAAE,oBAAoB,EAAE,MAAM,EAAgB,SAAS,EAAE,MAAM,aAAa,CAAA;AACvG,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AAG1D,qBAAa,qBAAqB;IAChC,OAAO,CAAC,aAAa,CAAe;IACpC,OAAO,CAAC,MAAM,CAAuC;IACrD,OAAO,CAAC,MAAM,CAAQ;IACtB,OAAO,CAAC,kBAAkB,CAAoB;IACvC,UAAU,cAAoB;IACrC,OAAO,CAAC,cAAc,CAAc;IACpC,OAAO,CAAC,cAAc,CAAc;IACpC,OAAO,CAAC,eAAe,CAAY;gBAGjC,MAAM,EAAE,IAAI,CAAC,oBAAoB,EAAE,SAAS,CAAC,EAC7C,aAAa,EAAE,aAAa,EAC5B,MAAM,EAAE,MAAM,EACd,kBAAkB,EAAE,kBAAkB,EACtC,cAAc,EAAE,MAAM,MAAM,EAC5B,cAAc,EAAE,MAAM,MAAM;IAU9B;;;OAGG;IACI,gBAAgB,IAAK,IAAI;IAIhC;;;OAGG;IACH,OAAO,CAAC,mBAAmB;IA0C3B;;;;;;;;;;;;;OAaG;IACH,oBAAoB,CAAE,IAAI,EAAE,cAAc,EAAE,YAAY,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,SAAS,GAAG,SAAS,GAAG,IAAI;IAkWxG;;;;;;OAMG;IACH,OAAO,CAAC,4BAA4B;IA8BpC;;;;;;;;;;;;;OAaG;IACH,OAAO,CAAC,sBAAsB;IA2C9B;;;;;;;;;;;OAWG;IACH,OAAO,CAAC,gBAAgB;IA6LxB;;;;;;;;;OASG;IACH,OAAO,CAAC,eAAe;CA2BxB"}
@@ -7,11 +7,16 @@ export declare class JSXHandler {
7
7
  private expressionResolver;
8
8
  private getCurrentFile;
9
9
  private getCurrentCode;
10
+ private lastSearchIndex;
10
11
  constructor(config: Omit<I18nextToolkitConfig, 'plugins'>, pluginContext: PluginContext, expressionResolver: ExpressionResolver, getCurrentFile: () => string, getCurrentCode: () => string);
11
12
  /**
12
- * Helper method to calculate line and column from byte offset.
13
+ * Reset the search index when starting to process a new file.
13
14
  */
14
- private getLocationFromSpan;
15
+ resetSearchIndex(): void;
16
+ /**
17
+ * Helper method to calculate line and column by searching for the JSX element in the code.
18
+ */
19
+ private getLocationFromNode;
15
20
  /**
16
21
  * Processes JSX elements to extract translation keys from Trans components.
17
22
  *
@@ -1 +1 @@
1
- {"version":3,"file":"jsx-handler.d.ts","sourceRoot":"","sources":["../../../src/extractor/parsers/jsx-handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAoB,MAAM,WAAW,CAAA;AAC7D,OAAO,KAAK,EAAE,aAAa,EAAE,oBAAoB,EAAgB,MAAM,aAAa,CAAA;AACpF,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AAI1D,qBAAa,UAAU;IACrB,OAAO,CAAC,MAAM,CAAuC;IACrD,OAAO,CAAC,aAAa,CAAe;IACpC,OAAO,CAAC,kBAAkB,CAAoB;IAC9C,OAAO,CAAC,cAAc,CAAc;IACpC,OAAO,CAAC,cAAc,CAAc;gBAGlC,MAAM,EAAE,IAAI,CAAC,oBAAoB,EAAE,SAAS,CAAC,EAC7C,aAAa,EAAE,aAAa,EAC5B,kBAAkB,EAAE,kBAAkB,EACtC,cAAc,EAAE,MAAM,MAAM,EAC5B,cAAc,EAAE,MAAM,MAAM;IAS9B;;OAEG;IACH,OAAO,CAAC,mBAAmB;IAe3B;;;;;;;;OAQG;IACH,gBAAgB,CAAE,IAAI,EAAE,UAAU,EAAE,YAAY,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK;QAAE,SAAS,CAAC,EAAE,MAAM,CAAC;QAAC,SAAS,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,SAAS,GAAG,IAAI;IAgRjI;;;;;;;;;;;OAWG;IACH,OAAO,CAAC,0BAA0B;IAqGlC;;;;;;;;;OASG;IACH,OAAO,CAAC,cAAc;CAevB"}
1
+ {"version":3,"file":"jsx-handler.d.ts","sourceRoot":"","sources":["../../../src/extractor/parsers/jsx-handler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAoB,MAAM,WAAW,CAAA;AAC7D,OAAO,KAAK,EAAE,aAAa,EAAE,oBAAoB,EAAgB,MAAM,aAAa,CAAA;AACpF,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AAI1D,qBAAa,UAAU;IACrB,OAAO,CAAC,MAAM,CAAuC;IACrD,OAAO,CAAC,aAAa,CAAe;IACpC,OAAO,CAAC,kBAAkB,CAAoB;IAC9C,OAAO,CAAC,cAAc,CAAc;IACpC,OAAO,CAAC,cAAc,CAAc;IACpC,OAAO,CAAC,eAAe,CAAY;gBAGjC,MAAM,EAAE,IAAI,CAAC,oBAAoB,EAAE,SAAS,CAAC,EAC7C,aAAa,EAAE,aAAa,EAC5B,kBAAkB,EAAE,kBAAkB,EACtC,cAAc,EAAE,MAAM,MAAM,EAC5B,cAAc,EAAE,MAAM,MAAM;IAS9B;;OAEG;IACI,gBAAgB,IAAK,IAAI;IAIhC;;OAEG;IACH,OAAO,CAAC,mBAAmB;IA8B3B;;;;;;;;OAQG;IACH,gBAAgB,CAAE,IAAI,EAAE,UAAU,EAAE,YAAY,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK;QAAE,SAAS,CAAC,EAAE,MAAM,CAAC;QAAC,SAAS,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,SAAS,GAAG,IAAI;IAgRjI;;;;;;;;;;;OAWG;IACH,OAAO,CAAC,0BAA0B;IAqGlC;;;;;;;;;OASG;IACH,OAAO,CAAC,cAAc;CAevB"}