i18next-cli 1.33.2 → 1.33.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cjs/cli.js
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"),require("@croct/json5-parser");var s=require("./types-generator.js"),l=require("./syncer.js"),d=require("./migrator.js"),u=require("./init.js"),p=require("./linter.js"),g=require("./status.js"),f=require("./locize.js"),y=require("./rename-key.js");const h=new e.Command;h.name("i18next-cli").description("A unified, high-performance i18next CLI.").version("1.33.
|
|
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"),require("@croct/json5-parser");var s=require("./types-generator.js"),l=require("./syncer.js"),d=require("./migrator.js"),u=require("./init.js"),p=require("./linter.js"),g=require("./status.js"),f=require("./locize.js"),y=require("./rename-key.js");const h=new e.Command;h.name("i18next-cli").description("A unified, high-performance i18next CLI.").version("1.33.3"),h.option("-c, --config <path>","Path to i18next-cli config file (overrides detection)"),h.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.").option("--sync-all","Sync primary language values with default values from code AND clear synced keys in all other locales.").action(async e=>{try{const t=h.opts().config,i=await r.ensureConfig(t),a=async()=>{const o=!!e.syncPrimary||!!e.syncAll,t=await c.runExtractor(i,{isWatchMode:!!e.watch,isDryRun:!!e.dryRun,syncPrimaryWithDefaults:o,syncAll:!!e.syncAll});return e.ci&&!t?(console.log("✅ No files were updated."),process.exit(0)):e.ci&&t&&(console.error("❌ Some files were updated. This should not happen in CI mode."),process.exit(1)),t};if(await a(),e.watch){console.log("\nWatching for changes...");const e=await x(i.extract.input),t=m(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)}}),h.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=h.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 g.runStatus(n,{detail:e,namespace:o.namespace})}),h.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=h.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=[...m(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()})}}),h.command("sync").description("Synchronize secondary language files with the primary language file.").action(async()=>{const e=h.opts().config,o=await r.ensureConfig(e);await l.runSyncer(o)}),h.command("migrate-config [configPath]").description("Migrate a legacy i18next-parser.config.js to the new format.").action(async e=>{await d.runMigrator(e)}),h.command("init").description("Create a new i18next.config.ts/js file with an interactive setup wizard.").action(u.runInit),h.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=h.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 p.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=[...m(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()})}}}),h.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.").option("--cdn-type <standard|pro>","Specify the cdn endpoint that should be used (depends on which cdn type you've in your locize project)").action(async e=>{const o=h.opts().config,t=await r.ensureConfig(o);await f.runLocizeSync(t,e)}),h.command("locize-download").description("Download all translations from your locize project.").option("--cdn-type <standard|pro>","Specify the cdn endpoint that should be used (depends on which cdn type you've in your locize project)").action(async e=>{const o=h.opts().config,t=await r.ensureConfig(o);await f.runLocizeDownload(t,e)}),h.command("locize-migrate").description("Migrate local translation files to a new locize project.").option("--cdn-type <standard|pro>","Specify the cdn endpoint that should be used (depends on which cdn type you've in your locize project)").action(async e=>{const o=h.opts().config,t=await r.ensureConfig(o);await f.runLocizeMigrate(t,e)}),h.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=h.opts().config,a=await r.ensureConfig(n),c=await y.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)}}),h.parse(process.argv);const m=e=>Array.isArray(e)?e:e?[e]:[],w=e=>e&&"string"==typeof e?[e.replace(/\{\{[^}]+\}\}/g,"*")]:[],x=async(e=[])=>{const o=m(e),n=await Promise.all(o.map(e=>t.glob(e||"",{nodir:!0})));return Array.from(new Set(n.flat()))};exports.program=h;
|
|
@@ -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(t){if(t)return"StringLiteral"===t.type?t.value:"TemplateLiteral"===t.type&&e.isSimpleTemplateLiteral(t)?t.quasis?.[0]?.cooked:"ParenExpression"===t.type&&t.expr?n(t.expr):"ParenthesizedExpression"===t.type&&t.expression||"TsAsExpression"===t.type&&t.expression?n(t.expression):void 0}function i(e){return"StringLiteral"===e.value?.type?e.value.value:"JSXExpressionContainer"===e.value?.type?t(e.value.expression):void 0}exports.extractFromTransComponent=function(s,r){const o=s.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"i18nKey"===e.name.value),p=s.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"defaults"===e.name.value),l=s.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"count"===e.name.value),a=s.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"values"===e.name.value);let c;l||"JSXAttribute"!==a?.type||"JSXExpressionContainer"!==a.value?.type||"ObjectExpression"!==a.value.expression.type||(c=e.getObjectPropValueExpression(a.value.expression,"count"));const u=!!l||!!c,f=s.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,d=s.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),g=!!d,x=s.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"context"===e.name.value);let h="JSXAttribute"===x?.type&&"JSXExpressionContainer"===x.value?.type?x.value.expression:"JSXAttribute"===x?.type&&"StringLiteral"===x.value?.type?x.value:void 0;const v=s.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ns"===e.name.value);let S;S="JSXAttribute"===v?.type?i(v):void 0,y&&(void 0===S&&(S=e.getObjectPropValue(y,"ns")),void 0===h&&(h=e.getObjectPropValueExpression(y,"context")));const m=function(e,i){if(!e||0===e.length)return"";const s=new Set(i.extract.transKeepBasicHtmlNodesFor??["br","strong","i","p"]),r=e=>e&&"JSXText"===e.type&&/^\s*$/.test(e.value)&&e.value.includes("\n"),o=e=>e&&("JSXElement"===e.type||"JSXFragment"===e.type),p=(e,t)=>{for(let n=t;n<e.length;n++){const t=e[n];if(t&&("JSXText"!==t.type||!r(t)))return t}};function l(e,n,i=!1,a=!1){if(!e||!e.length)return;const c=a&&e.filter(e=>e&&"JSXElement"===e.type&&"p"===e.opening?.name?.value).length>1;let u=0,f=e.length-1;for(;u<=f&&r(e[u]);)u++;for(;f>=u&&r(e[f]);)f--;const y=u<=f?e.slice(u,f+1):[],d=y.some(e=>e&&("JSXElement"===e.type||"JSXFragment"===e.type));for(let e=0;e<y.length;e++){const u=y[e];if(u)if("JSXText"!==u.type){if("JSXExpressionContainer"===u.type){if(i&&!d&&u.expression){const e=u.expression.type;if("ObjectExpression"===e)continue;if(void 0!==t(u.expression))continue;if("Identifier"===e||"MemberExpression"===e||"CallExpression"===e)continue}const s=t(u.expression);if(void 0!==s){const t=/^\s*$/.test(s)&&!s.includes("\n"),i=y[e-1],l=y[e+1];if(t){const t=p(y,e+1);if(o(i)&&o(t));else{const t=y[e+2];if(l&&"JSXText"===l.type&&r(l)&&t&&("JSXElement"===t.type||"JSXFragment"===t.type)){const t=y[e-1],n=y[e-2];if(!t||"JSXText"!==t.type&&n&&"JSXExpressionContainer"===n.type)continue}if(i&&("JSXElement"===i.type||"JSXFragment"===i.type)&&l&&"JSXText"===l.type&&r(l))continue;const s=!l||"JSXText"===l.type&&!r(l);if(i&&"JSXText"===i.type&&s){const e=n[n.length-1];if(e&&"JSXText"===e.type){e.value=String(e.value)+u.expression.value;continue}}if(i&&("JSXElement"===i.type||"JSXFragment"===i.type)&&l&&"JSXText"===l.type&&r(l))continue}}}n.push(u);continue}if("JSXElement"===u.type){const e=u.opening&&u.opening.name&&"Identifier"===u.opening.name.type?u.opening.name.value:void 0;if(e&&s.has(e)){const i=u.opening&&Array.isArray(u.opening.attributes)&&u.opening.attributes.length>0,o=u.children||[],p=1===o.length&&("JSXText"===o[0]?.type||"JSXExpressionContainer"===o[0]?.type&&void 0!==t(o[0].expression)),f=!o.length,d=p;if(i&&!p)n.push(u),l(u.children||[],n,!0);else if(f)n.push(u);else if(!d){if((u.children||[]).some(e=>"JSXExpressionContainer"===e.type&&void 0===t(e.expression)))n.push(u),l(u.children||[],n,!0);else if("p"===e){const e=(o||[]).some(e=>{if(!e||"JSXElement"!==e.type)return!1;const n=e.opening&&e.opening.name&&"Identifier"===e.opening.name.type?e.opening.name.value:void 0,i=e.opening&&Array.isArray(e.opening.attributes)&&e.opening.attributes.length>0,r=e.children||[],o=1===r.length&&("JSXText"===r[0]?.type||"JSXExpressionContainer"===r[0]?.type&&void 0!==t(r[0].expression));return!(n&&s.has(n)&&!i&&(0===r.length||o))}),i=a&&y.some(e=>e&&e!==u&&"JSXText"===e.type&&!r(e));c||a&&e&&!i?(n.push(u),l(u.children||[],n,!0,!1)):l(u.children||[],n,!1,!1)}else l(u.children||[],n,!1,!1)}continue}n.push(u),l(u.children||[],n,!0);continue}"JSXFragment"!==u.type||l(u.children||[],n,i)}else{if(i&&!d)continue;if(i&&r(u))continue;if(r(u)){const t=y[e-1],i=y[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=y[e-1];if(s){if(r&&"JSXExpressionContainer"===r.type)continue;if("JSXText"===s.type&&r&&"JSXText"===r.type){s.value=String(s.value)+u.value;continue}}}if(i&&d&&0===e)continue;n.push(u)}}}const a=[];function c(e){if(!e||!e.length)return!1;let t=!1;for(const n of e)if(n)if("JSXElement"!==n.type){if("JSXExpressionContainer"===n.type&&-1!==a.indexOf(n))return t;if("JSXText"===n.type&&-1!==a.indexOf(n)){if(r(n))continue;if(!t)return!0;const i=e.indexOf(n);if(e.slice(i+1).some(e=>e&&"JSXElement"===e.type))return!0}}else t=!0;return!1}l(e,a,!1,!0);const u=new Set,f=e=>String(e).replace(/^\s*\n\s*/g,"").replace(/\s*\n\s*$/g,"");function y(e,i,o=!1){if(!e||0===e.length)return"";let p="";const l=e=>{if(!e)return-1;if(i&&i.has(e))return i.get(e);if(i)for(const[t,n]of i.entries())try{if(t&&e&&t.span&&e.span&&t.span.start===e.span.start&&t.span.end===e.span.end)return n}catch(e){}return a.indexOf(e)},d=t=>{if(!o)return a.indexOf(t);const n=e.indexOf(t);if(-1===n)return-1;let i=0;for(let t=0;t<n;t++){const n=e[t];n&&("JSXText"===n.type?r(n)||i++:"JSXExpressionContainer"===n.type?n.expression&&"JSXEmptyExpression"!==n.expression.type&&i++:"JSXElement"===n.type&&i++)}return i};for(let i=0;i<e.length;i++){const g=e[i];if(g){if("JSXText"===g.type){if(r(g))continue;const t=e[i+1],n=e[i-1];if(n&&"JSXElement"===n.type){const t="Identifier"===n.opening?.name?.type?n.opening.name.value:void 0,r=t&&s.has(t),o=0===(n.children||[]).length;if(r&&o&&/^\s*\n\s*/.test(g.value)){const e=g.value.replace(/^\s*\n\s*/,"");if(e){p+=e;continue}continue}if(!r&&/^\s*\n\s*/.test(g.value)){const t=g.value.replace(/^\s*\n\s*/,"");if(t){if(n&&"JSXElement"===n.type&&Array.isArray(n.children)&&0===n.children.length){p+=t;continue}const s=e[i-2];if(s&&"JSXText"===s.type){const e=s.value.replace(/\n\s*$/,""),n=/[A-Za-z0-9]$/.test(e),i=/^[A-Za-z0-9]/.test(t),r=/^[a-z]/.test(t);if(n&&i&&r){p+=t;continue}}p+=" "+t;continue}continue}}if(/\n\s*$/.test(g.value)&&t&&"JSXElement"===t.type){const n=g.value.replace(/\n\s*$/,"");if(n.trim()){const o="Identifier"===t.opening?.name?.type?t.opening.name.value:void 0,l=o&&s.has(o),a=(t.children||[]).length>0,c=/\s\n/.test(g.value),f=e[i+2],y=f&&"JSXText"===f.type&&!r(f)&&/[a-zA-Z0-9]/.test(f.value),d=!!(t.opening&&Array.isArray(t.opening.attributes)&&t.opening.attributes.length>0),x=/^\s/.test(n)&&!/^\n/.test(n),h=n.trim(),v=x?" "+h:h,S=/[A-Za-z0-9]$/.test(h),m=f&&"string"==typeof f.value&&/^[A-Za-z0-9]/.test(f.value.trim()),J=f&&"string"==typeof f.value&&/^[a-z]/.test(f.value.trim()),X=f&&"string"==typeof f.value&&/^\s/.test(f.value)&&!/^\n/.test(f.value),E=d&&a&&y&&!(S&&m&&J&&!c&&!x&&!X),$=f&&"string"==typeof f.value&&/^[,;:!?.]/.test(f.value.trim()),b=E&&!$;S&&m&&J&&!c&&!x&&!X&&u.add(t),p+=c||l&&a||!l&&y&&x||b?v+" ":v;continue}}p+=g.value;continue}if("JSXExpressionContainer"===g.type){const e=g.expression;if(!e)continue;const i=t(e);if(void 0!==i)p+=i;else if("Identifier"===e.type)p+=`{{${e.value}}}`;else if("TsAsExpression"===e.type&&"ObjectExpression"===e.expression?.type){const t=e.expression,n=t.properties.filter(e=>"KeyValueProperty"===e.type&&e.key&&"Identifier"===e.key.type).map(e=>e.key.value);if(n.length>0)p+=n.map(e=>`{{${e}}}`).join("");else{const e=t.properties[0];if(!e||"Identifier"!==e.type)throw new Error("Unrecognized expression in JSX placeholder");p+=`{{${e.value}}}`}}else if("ObjectExpression"===e.type){const t=e.properties[0];if(t&&"KeyValueProperty"===t.type&&t.key&&"Identifier"===t.key.type)p+=`{{${t.key.value}}}`;else{if(!t||"Identifier"!==t.type)throw new Error("Unrecognized expression in JSX placeholder");p+=`{{${t.value}}}`}}else if("MemberExpression"===e.type&&e.property&&"Identifier"===e.property.type)p+=`{{${e.property.value}}}`;else if("CallExpression"===e.type&&"Identifier"===e.callee?.type)p+=`{{${e.callee.value}}}`;else if("ConditionalExpression"===e.type){const t=n(e.consequent),i=n(e.alternate);p+=void 0!==t&&void 0!==i?t.length>=i.length?t:i:void 0!==t?t:void 0!==i?i:""}else if("JSXEmptyExpression"!==e.type)throw new Error(`Unrecognized expression in JSX placeholder: ${e.type}`);continue}if("JSXElement"===g.type){const n=o?d(g):void 0;let u;if(g.opening&&g.opening.name&&"Identifier"===g.opening.name.type&&(u=g.opening.name.value),u&&s.has(u)){const d=g.opening&&Array.isArray(g.opening.attributes)&&g.opening.attributes.length>0,x=g.children||[],h=x.length>0,v=1===x.length&&("JSXText"===x[0]?.type||"JSXExpressionContainer"===x[0]?.type&&void 0!==t(x[0].expression)),S="p"===u;let m=0;if(S&&o&&(m=e.filter(e=>e&&"JSXElement"===e.type&&"p"===e.opening?.name?.value).length),!h||v){const t=v?y(x,void 0):"";if(""!==String(t).trim())if(s.has(u)&&"p"!==u)p+=`<${u}>${f(t)}</${u}>`;else if(S&&o&&m>1)p+=`<${u}>${f(t)}</${u}>`;else if(S){const e=o&&void 0!==n?n:a.indexOf(g);p+=`<${e}>${f(t)}</${e}>`}else{const e=o&&void 0!==n?n:a.indexOf(g);p+=`<${e}>${f(t)}</${e}>`}else{const t=e[i-1];t&&"JSXText"===t.type&&/\n\s*$/.test(t.value)&&(p=p.replace(/\s+$/,"")),p+=`<${u} />`}}else if(d&&!v){const e=x,i=o&&void 0!==n?n:l(g);if(c(e)){const n=new Map;let r=0;for(const i of e)if(i&&"JSXElement"===i.type){const e=i.opening&&i.opening.name&&"Identifier"===i.opening.name.type?i.opening.name.value:void 0;if(e&&s.has(e)){const e=i.opening&&Array.isArray(i.opening.attributes)&&i.opening.attributes.length>0,s=i.children||[],o=1===s.length&&("JSXText"===s[0]?.type||"JSXExpressionContainer"===s[0]?.type&&void 0!==t(s[0].expression));!e&&(!s.length||o)||n.set(i,r++)}else n.set(i,r++)}const o=y(e,n.size?n:void 0);p+=`<${i}>${f(o)}</${i}>`}else{const e=new Map;let n=0;for(const i of x)if(i&&"JSXElement"===i.type){const r=i.opening&&i.opening.name&&"Identifier"===i.opening.name.type?i.opening.name.value:void 0;if(r&&s.has(r)){const s=i.opening&&Array.isArray(i.opening.attributes)&&i.opening.attributes.length>0,r=i.children||[],o=1===r.length&&("JSXText"===r[0]?.type||"JSXExpressionContainer"===r[0]?.type&&void 0!==t(r[0].expression));!s&&(!r.length||o)||e.set(i,n++)}else e.set(i,n++)}const r=y(x,e.size?e:void 0);p+=`<${i}>${f(r)}</${i}>`}}else{const e=a.indexOf(g);if(-1!==e){const i=void 0!==n?n:e;if((()=>{let e=!1;for(const t of x)if(t)if("JSXElement"!==t.type){if("JSXExpressionContainer"===t.type&&-1!==a.indexOf(t))return e;if("JSXText"===t.type&&-1!==a.indexOf(t)){if(r(t))continue;if(!e)return!0;if(x.slice(x.indexOf(t)+1).some(e=>e&&"JSXElement"===e.type))return!0}}else e=!0;return!1})()){if(0===i){const e=y(x,void 0,!1);p+=`<${i}>${f(e)}</${i}>`;continue}const e=new Map;for(const e of x)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&&s.has(n)){const n=e.opening&&Array.isArray(e.opening.attributes)&&e.opening.attributes.length>0,s=e.children||[],r=1===s.length&&("JSXText"===s[0]?.type||"JSXExpressionContainer"===s[0]?.type&&void 0!==t(s[0].expression));if(!(!n&&(!s.length||r))){const t=y(e.children,void 0,!1);p+=`<${i}>${f(t)}</${i}>`}}else{const t=y(e.children,void 0,!1);p+=`<${i}>${f(t)}</${i}>`}}const n=y(x,e.size?e:void 0,!1);p+=`<${i}>${f(n)}</${i}>`;continue}const o=new Map;let l=0;for(const e of x)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&&s.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)||o.set(e,l++)}else o.set(e,l++)}const c=y(x,o.size>0?o:void 0,!1);p+=`<${i}>${f(c)}</${i}>`}else{const e=y(x,void 0,!1);p+=`<${u}>${f(e)}</${u}>`}}}else{const e=g.children||[];if(e.some(e=>e&&("JSXText"===e.type||"JSXExpressionContainer"===e.type)&&-1!==a.indexOf(e))){const t=a.indexOf(g),n=y(e,void 0);p+=`<${t}>${f(n)}</${t}>`}else{const t=new Map,n=l(g);let i=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&&s.has(e)){const e=n.opening&&Array.isArray(n.opening.attributes)&&n.opening.attributes.length>0,s=n.children||[],r=1===s.length&&"JSXText"===s[0]?.type;!e&&(!s.length||r)||t.set(n,i++)}else t.set(n,i++)}const r=y(e,t.size?t:void 0);p+=`<${n}>${f(r)}</${n}>`}}continue}"JSXFragment"!==g.type||(p+=y(g.children||[]))}}return p}const d=y(e,void 0,!0),g=String(d).replace(/<br \/>\s*\n\s*/g,"<br />"),x=String(g),h=new Set;if(u&&u.size>0)for(let e=0;e<a.length;e++)u.has(a[e])&&h.add(e);let v=String(x);if(h&&h.size>0)for(const e of h)try{v=v.replace(new RegExp("\\s+<"+e+">","g"),"<"+e+">"),v=v.replace(new RegExp("<\\/"+e+">\\s+","g"),"</"+e+">")}catch(e){}v=v.replace(/<\/(\d+)>\s*\n\s*(\S)/g,(e,t,n)=>{const i=Number(t);return h.has(i)?`</${t}>${n}`:`</${t}> ${n}`}),v=v.replace(/(\S)\s*\n\s*<(\d+)/g,(e,t,n)=>{const i=Number(n);return h.has(i)?`${t}<${n}`:`${t} <${n}`}),v=v.replace(/\s*\n\s*/g," "),v=v.replace(/\s+/g," "),v=v.replace(/\s+([,;:!?.])/g,"$1");const S=v.trim();let m=String(S);if(h&&h.size>0)for(const e of h)try{m=m.replace(new RegExp("[\\s\\u00A0]+<"+e+">","g"),"<"+e+">"),m=m.replace(new RegExp("<\\/"+e+">[\\s\\u00A0]+","g"),"</"+e+">")}catch(e){}try{for(let e=0;e<a.length;e++){const t=a[e];if(!t||"JSXElement"!==t.type)continue;const n=a[e-1],i=a[e+1];if(!n||!i)continue;if("JSXText"!==n.type||"JSXText"!==i.type)continue;const s=String(n.value),r=String(i.value),o=s.replace(/\n\s*$/,""),p=/[A-Za-z0-9]$/.test(o),l=/^[A-Za-z0-9]/.test(r.trim()),c=/^[a-z]/.test(r.trim()),u=/\s\n/.test(s),f=r&&/^\s/.test(r)&&!/^\n/.test(r);if(p&&l&&c&&!u&&!f){const t=e;m=m.replace(new RegExp("\\s+<"+t+">","g"),"<"+t+">")}}}catch(e){}function J(e){if(!e||!e.includes("<"))return e;function t(e){const t=[],n=[];let i=0;const s=/<\/?(\d+)>|<[^>]+>/g;let r;for(;r=s.exec(e);){const o=r[0],p=r.index;if(p>i){const s={type:"text",text:e.slice(i,p)};n.length?n[n.length-1].node.children.push(s):t.push(s)}const l=/^<\/(\d+)>$/.exec(o),a=/^<(\d+)>$/.exec(o);if(a){const e=Number(a[1]),i={type:"ph",idx:e,children:[]};n.length?n[n.length-1].node.children.push(i):t.push(i),n.push({node:i,idx:e})}else if(l)n.length&&n.pop();else{const e={type:"text",text:o};n.length?n[n.length-1].node.children.push(e):t.push(e)}i=s.lastIndex}if(i<e.length){const s={type:"text",text:e.slice(i)};n.length?n[n.length-1].node.children.push(s):t.push(s)}return t}function n(e,t=null){let i="";for(const o of e)if("text"===o.type)i+=o.text;else{const e=o.children.filter(e=>"ph"===e.type);let p;if("number"==typeof o.idx){const t=a[o.idx];if(t&&Array.isArray(t.children)){const n=new Map;let i=0;for(const e of t.children){const t=a.indexOf(e);if("JSXText"===e.type){if(r(e))continue;-1!==t&&n.set(t,i),i++}else"JSXElement"===e.type?(-1!==t&&n.set(t,i),i++):"JSXExpressionContainer"===e.type&&-1!==t&&(n.set(t,i),i++)}e.every(e=>n.has(e.idx))&&(p=n)}}if(!p){const n=e.map(e=>e.idx);if(n.length<=1||n.every((e,t)=>0===t||e===n[t-1]+1)){p=new Map;let n=0;try{const i=a[o.idx];if(i&&i.span){const r=i.span.start,p=i.span.end;let l=0,c=-1;for(let e=o.idx+1;e<a.length;e++){const t=a[e];if(t&&t.span&&(t.span.start>=r&&t.span.end<=p&&"JSXElement"===t.type)){c=e;break}}if(-1!==c)for(let e=o.idx+1;e<c;e++){const t=a[e];t&&t.span&&(t.span.start>=r&&t.span.end<=p&&("JSXText"!==t.type&&"JSXExpressionContainer"!==t.type||l++))}else for(let e=o.idx+1;e<a.length;e++){const t=a[e];t&&t.span&&(t.span.start>=r&&t.span.end<=p&&("JSXText"!==t.type&&"JSXExpressionContainer"!==t.type||l++))}const u=i?.opening?.name?.value,f=u&&s.has(u),y=null===t;1===e.length&&(n=Math.max(1,l+1),!y||f||0!==e[0].idx||"a"!==u&&"p"!==u||(n=0))}}catch(e){}for(const t of e)p.has(t.idx)||p.set(t.idx,n++)}}const l=o.children.map(e=>{if("text"===e.type)return e.text;const t=e.idx,i=p&&p.has(t)?p.get(t):t;return`<${i}>${n(e.children,i)}</${i}>`}).join("");i+=`<${o.idx}>${l}</${o.idx}>`}return i}try{const i=t(e);return n(i)}catch(t){return e}}return m=J(m),m.trim()}(s.children,r);let J;const X="JSXAttribute"===p?.type?i(p):void 0;if(void 0!==X)J=X;else{const e=r.extract.defaultValue;J="string"==typeof e?e:""}let E,$;if("JSXAttribute"===o?.type){if("StringLiteral"===o.value?.type){if(E=o.value,$=E.value,!$||""===$.trim())return null;if(S&&"StringLiteral"===E.type){const e=r.extract.nsSeparator??":",t=E.value;if(e&&t.startsWith(`${S}${e}`)){if($=t.slice(`${S}${e}`.length),!$||""===$.trim())return null;E={...E,value:$}}}}else"JSXExpressionContainer"===o.value?.type&&"JSXEmptyExpression"!==o.value.expression.type&&(E=o.value.expression);if(!E)return null}return p||!$||m.trim()?!p&&m.trim()&&(J=m):J=$,{keyExpression:E,serializedChildren:m,ns:S,defaultValue:J,hasCount:u,isOrdinal:g,contextExpression:h,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(t){if(t)return"StringLiteral"===t.type?t.value:"TemplateLiteral"===t.type&&e.isSimpleTemplateLiteral(t)?t.quasis?.[0]?.cooked:"ParenExpression"===t.type&&t.expr?n(t.expr):"ParenthesizedExpression"===t.type&&t.expression||"TsAsExpression"===t.type&&t.expression?n(t.expression):void 0}function i(e){return"StringLiteral"===e.value?.type?e.value.value:"JSXExpressionContainer"===e.value?.type?t(e.value.expression):void 0}exports.extractFromTransComponent=function(s,r){const o=s.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"i18nKey"===e.name.value),p=s.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"defaults"===e.name.value),l=s.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"count"===e.name.value),a=s.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"values"===e.name.value);let c;l||"JSXAttribute"!==a?.type||"JSXExpressionContainer"!==a.value?.type||"ObjectExpression"!==a.value.expression.type||(c=e.getObjectPropValueExpression(a.value.expression,"count"));const u=!!l||!!c,f=s.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,d=s.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),g=!!d,x=s.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"context"===e.name.value);let h="JSXAttribute"===x?.type&&"JSXExpressionContainer"===x.value?.type?x.value.expression:"JSXAttribute"===x?.type&&"StringLiteral"===x.value?.type?x.value:void 0;const v=s.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ns"===e.name.value);let S;S="JSXAttribute"===v?.type?i(v):void 0,y&&(void 0===S&&(S=e.getObjectPropValue(y,"ns")),void 0===h&&(h=e.getObjectPropValueExpression(y,"context")));const m=function(e,i){if(!e||0===e.length)return"";const s=new Set(i.extract.transKeepBasicHtmlNodesFor??["br","strong","i","p"]),r=e=>e&&"JSXText"===e.type&&/^\s*$/.test(e.value)&&e.value.includes("\n"),o=e=>e&&("JSXElement"===e.type||"JSXFragment"===e.type),p=(e,t)=>{for(let n=t;n<e.length;n++){const t=e[n];if(t&&("JSXText"!==t.type||!r(t)))return t}},l=e=>{let t=e;for(;t;)if("ParenExpression"===t.type&&t.expr)t=t.expr;else if("ParenthesizedExpression"===t.type&&t.expression)t=t.expression;else{if("TsAsExpression"!==t.type||!t.expression)break;t=t.expression}return t};function a(e,i,c=!1,u=!1){if(!e||!e.length)return;const f=u&&e.filter(e=>e&&"JSXElement"===e.type&&"p"===e.opening?.name?.value).length>1;let y=0,d=e.length-1;for(;y<=d&&r(e[y]);)y++;for(;d>=y&&r(e[d]);)d--;const g=y<=d?e.slice(y,d+1):[],x=g.some(e=>e&&("JSXElement"===e.type||"JSXFragment"===e.type));for(let e=0;e<g.length;e++){const y=g[e];if(y)if("JSXText"!==y.type){if("JSXExpressionContainer"===y.type){if(c&&!x&&y.expression){const e=l(y.expression),t=e?.type;if("ObjectExpression"===t)continue;if(void 0!==n(e))continue;if("Identifier"===t||"MemberExpression"===t||"CallExpression"===t)continue}const s=t(y.expression);if(void 0!==s){const t=/^\s*$/.test(s)&&!s.includes("\n"),n=g[e-1],l=g[e+1];if(t){const t=p(g,e+1);if(o(n)&&o(t));else{const t=g[e+2];if(l&&"JSXText"===l.type&&r(l)&&t&&("JSXElement"===t.type||"JSXFragment"===t.type)){const t=g[e-1],n=g[e-2];if(!t||"JSXText"!==t.type&&n&&"JSXExpressionContainer"===n.type)continue}if(n&&("JSXElement"===n.type||"JSXFragment"===n.type)&&l&&"JSXText"===l.type&&r(l))continue;const s=!l||"JSXText"===l.type&&!r(l);if(n&&"JSXText"===n.type&&s){const e=i[i.length-1];if(e&&"JSXText"===e.type){e.value=String(e.value)+y.expression.value;continue}}if(n&&("JSXElement"===n.type||"JSXFragment"===n.type)&&l&&"JSXText"===l.type&&r(l))continue}}}i.push(y);continue}if("JSXElement"===y.type){const e=y.opening&&y.opening.name&&"Identifier"===y.opening.name.type?y.opening.name.value:void 0;if(e&&s.has(e)){const n=y.opening&&Array.isArray(y.opening.attributes)&&y.opening.attributes.length>0,o=y.children||[],p=1===o.length&&("JSXText"===o[0]?.type||"JSXExpressionContainer"===o[0]?.type&&void 0!==t(o[0].expression)),l=!o.length,c=p;if(n&&!p)i.push(y),a(y.children||[],i,!0);else if(l)i.push(y);else if(!c){if((y.children||[]).some(e=>"JSXExpressionContainer"===e.type&&void 0===t(e.expression)))i.push(y),a(y.children||[],i,!0);else if("p"===e){const e=(o||[]).some(e=>{if(!e||"JSXElement"!==e.type)return!1;const n=e.opening&&e.opening.name&&"Identifier"===e.opening.name.type?e.opening.name.value:void 0,i=e.opening&&Array.isArray(e.opening.attributes)&&e.opening.attributes.length>0,r=e.children||[],o=1===r.length&&("JSXText"===r[0]?.type||"JSXExpressionContainer"===r[0]?.type&&void 0!==t(r[0].expression));return!(n&&s.has(n)&&!i&&(0===r.length||o))}),n=u&&g.some(e=>e&&e!==y&&"JSXText"===e.type&&!r(e));f||u&&e&&!n?(i.push(y),a(y.children||[],i,!0,!1)):a(y.children||[],i,!1,!1)}else a(y.children||[],i,!1,!1)}continue}i.push(y),a(y.children||[],i,!0);continue}"JSXFragment"!==y.type||a(y.children||[],i,c)}else{if(c&&!x)continue;if(c&&r(y))continue;if(r(y)){const t=g[e-1],n=g[e+1];if(t&&("JSXElement"===t.type||"JSXFragment"===t.type)&&n&&("JSXElement"===n.type||"JSXFragment"===n.type))continue;const s=i[i.length-1],r=g[e-1];if(s){if(r&&"JSXExpressionContainer"===r.type)continue;if("JSXText"===s.type&&r&&"JSXText"===r.type){s.value=String(s.value)+y.value;continue}}}if(c&&x&&0===e)continue;i.push(y)}}}const c=[];function u(e){if(!e||!e.length)return!1;let t=!1;for(const n of e)if(n)if("JSXElement"!==n.type){if("JSXExpressionContainer"===n.type&&-1!==c.indexOf(n))return t;if("JSXText"===n.type&&-1!==c.indexOf(n)){if(r(n))continue;if(!t)return!0;const i=e.indexOf(n);if(e.slice(i+1).some(e=>e&&"JSXElement"===e.type))return!0}}else t=!0;return!1}a(e,c,!1,!0);const f=new Set,y=e=>String(e).replace(/^\s*\n\s*/g,"").replace(/\s*\n\s*$/g,"");function d(e,i,o=!1){if(!e||0===e.length)return"";let p="";const l=e=>{if(!e)return-1;if(i&&i.has(e))return i.get(e);if(i)for(const[t,n]of i.entries())try{if(t&&e&&t.span&&e.span&&t.span.start===e.span.start&&t.span.end===e.span.end)return n}catch(e){}return c.indexOf(e)},a=t=>{if(!o)return c.indexOf(t);const n=e.indexOf(t);if(-1===n)return-1;let i=0;for(let t=0;t<n;t++){const n=e[t];n&&("JSXText"===n.type?r(n)||i++:"JSXExpressionContainer"===n.type?n.expression&&"JSXEmptyExpression"!==n.expression.type&&i++:"JSXElement"===n.type&&i++)}return i};for(let i=0;i<e.length;i++){const g=e[i];if(g){if("JSXText"===g.type){if(r(g))continue;const t=e[i+1],n=e[i-1];if(n&&"JSXElement"===n.type){const t="Identifier"===n.opening?.name?.type?n.opening.name.value:void 0,r=t&&s.has(t),o=0===(n.children||[]).length;if(r&&o&&/^\s*\n\s*/.test(g.value)){const e=g.value.replace(/^\s*\n\s*/,"");if(e){p+=e;continue}continue}if(!r&&/^\s*\n\s*/.test(g.value)){const t=g.value.replace(/^\s*\n\s*/,"");if(t){if(n&&"JSXElement"===n.type&&Array.isArray(n.children)&&0===n.children.length){p+=t;continue}const s=e[i-2];if(s&&"JSXText"===s.type){const e=s.value.replace(/\n\s*$/,""),n=/[A-Za-z0-9]$/.test(e),i=/^[A-Za-z0-9]/.test(t),r=/^[a-z]/.test(t);if(n&&i&&r){p+=t;continue}}p+=" "+t;continue}continue}}if(/\n\s*$/.test(g.value)&&t&&"JSXElement"===t.type){const n=g.value.replace(/\n\s*$/,"");if(n.trim()){const o="Identifier"===t.opening?.name?.type?t.opening.name.value:void 0,l=o&&s.has(o),a=(t.children||[]).length>0,c=/\s\n/.test(g.value),u=e[i+2],y=u&&"JSXText"===u.type&&!r(u)&&/[a-zA-Z0-9]/.test(u.value),d=!!(t.opening&&Array.isArray(t.opening.attributes)&&t.opening.attributes.length>0),x=/^\s/.test(n)&&!/^\n/.test(n),h=n.trim(),v=x?" "+h:h,S=/[A-Za-z0-9]$/.test(h),m=u&&"string"==typeof u.value&&/^[A-Za-z0-9]/.test(u.value.trim()),J=u&&"string"==typeof u.value&&/^[a-z]/.test(u.value.trim()),X=u&&"string"==typeof u.value&&/^\s/.test(u.value)&&!/^\n/.test(u.value),E=d&&a&&y&&!(S&&m&&J&&!c&&!x&&!X),$=u&&"string"==typeof u.value&&/^[,;:!?.]/.test(u.value.trim()),b=E&&!$;S&&m&&J&&!c&&!x&&!X&&f.add(t),p+=c||l&&a||!l&&y&&x||b?v+" ":v;continue}}p+=g.value;continue}if("JSXExpressionContainer"===g.type){const e=g.expression;if(!e)continue;const i=t(e);if(void 0!==i)p+=i;else if("Identifier"===e.type)p+=`{{${e.value}}}`;else if("TsAsExpression"===e.type&&"ObjectExpression"===e.expression?.type){const t=e.expression,n=t.properties.filter(e=>"KeyValueProperty"===e.type&&e.key&&"Identifier"===e.key.type).map(e=>e.key.value);if(n.length>0)p+=n.map(e=>`{{${e}}}`).join("");else{const e=t.properties[0];if(!e||"Identifier"!==e.type)throw new Error("Unrecognized expression in JSX placeholder");p+=`{{${e.value}}}`}}else if("ObjectExpression"===e.type){const t=e.properties[0];if(t&&"KeyValueProperty"===t.type&&t.key&&"Identifier"===t.key.type)p+=`{{${t.key.value}}}`;else{if(!t||"Identifier"!==t.type)throw new Error("Unrecognized expression in JSX placeholder");p+=`{{${t.value}}}`}}else if("MemberExpression"===e.type&&e.property&&"Identifier"===e.property.type)p+=`{{${e.property.value}}}`;else if("CallExpression"===e.type&&"Identifier"===e.callee?.type)p+=`{{${e.callee.value}}}`;else if("ConditionalExpression"===e.type){const t=n(e.consequent),i=n(e.alternate);p+=void 0!==t&&void 0!==i?t.length>=i.length?t:i:void 0!==t?t:void 0!==i?i:""}else if("JSXEmptyExpression"!==e.type)throw new Error(`Unrecognized expression in JSX placeholder: ${e.type}`);continue}if("JSXElement"===g.type){const n=o?a(g):void 0;let f;if(g.opening&&g.opening.name&&"Identifier"===g.opening.name.type&&(f=g.opening.name.value),f&&s.has(f)){const a=g.opening&&Array.isArray(g.opening.attributes)&&g.opening.attributes.length>0,x=g.children||[],h=x.length>0,v=1===x.length&&("JSXText"===x[0]?.type||"JSXExpressionContainer"===x[0]?.type&&void 0!==t(x[0].expression)),S="p"===f;let m=0;if(S&&o&&(m=e.filter(e=>e&&"JSXElement"===e.type&&"p"===e.opening?.name?.value).length),!h||v){const t=v?d(x,void 0):"";if(""!==String(t).trim())if(s.has(f)&&"p"!==f)p+=`<${f}>${y(t)}</${f}>`;else if(S&&o&&m>1)p+=`<${f}>${y(t)}</${f}>`;else if(S){const e=o&&void 0!==n?n:c.indexOf(g);p+=`<${e}>${y(t)}</${e}>`}else{const e=o&&void 0!==n?n:c.indexOf(g);p+=`<${e}>${y(t)}</${e}>`}else{const t=e[i-1];t&&"JSXText"===t.type&&/\n\s*$/.test(t.value)&&(p=p.replace(/\s+$/,"")),p+=`<${f} />`}}else if(a&&!v){const e=x,i=o&&void 0!==n?n:l(g);if(u(e)){const n=new Map;let r=0;for(const i of e)if(i&&"JSXElement"===i.type){const e=i.opening&&i.opening.name&&"Identifier"===i.opening.name.type?i.opening.name.value:void 0;if(e&&s.has(e)){const e=i.opening&&Array.isArray(i.opening.attributes)&&i.opening.attributes.length>0,s=i.children||[],o=1===s.length&&("JSXText"===s[0]?.type||"JSXExpressionContainer"===s[0]?.type&&void 0!==t(s[0].expression));!e&&(!s.length||o)||n.set(i,r++)}else n.set(i,r++)}const o=d(e,n.size?n:void 0);p+=`<${i}>${y(o)}</${i}>`}else{const e=new Map;let n=0;for(const i of x)if(i&&"JSXElement"===i.type){const r=i.opening&&i.opening.name&&"Identifier"===i.opening.name.type?i.opening.name.value:void 0;if(r&&s.has(r)){const s=i.opening&&Array.isArray(i.opening.attributes)&&i.opening.attributes.length>0,r=i.children||[],o=1===r.length&&("JSXText"===r[0]?.type||"JSXExpressionContainer"===r[0]?.type&&void 0!==t(r[0].expression));!s&&(!r.length||o)||e.set(i,n++)}else e.set(i,n++)}const r=d(x,e.size?e:void 0);p+=`<${i}>${y(r)}</${i}>`}}else{const e=c.indexOf(g);if(-1!==e){const i=void 0!==n?n:e;if((()=>{let e=!1;for(const t of x)if(t)if("JSXElement"!==t.type){if("JSXExpressionContainer"===t.type&&-1!==c.indexOf(t))return e;if("JSXText"===t.type&&-1!==c.indexOf(t)){if(r(t))continue;if(!e)return!0;if(x.slice(x.indexOf(t)+1).some(e=>e&&"JSXElement"===e.type))return!0}}else e=!0;return!1})()){if(0===i){const e=d(x,void 0,!1);p+=`<${i}>${y(e)}</${i}>`;continue}const e=new Map;for(const e of x)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&&s.has(n)){const n=e.opening&&Array.isArray(e.opening.attributes)&&e.opening.attributes.length>0,s=e.children||[],r=1===s.length&&("JSXText"===s[0]?.type||"JSXExpressionContainer"===s[0]?.type&&void 0!==t(s[0].expression));if(!(!n&&(!s.length||r))){const t=d(e.children,void 0,!1);p+=`<${i}>${y(t)}</${i}>`}}else{const t=d(e.children,void 0,!1);p+=`<${i}>${y(t)}</${i}>`}}const n=d(x,e.size?e:void 0,!1);p+=`<${i}>${y(n)}</${i}>`;continue}const o=new Map;let l=0;for(const e of x)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&&s.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)||o.set(e,l++)}else o.set(e,l++)}const a=d(x,o.size>0?o:void 0,!1);p+=`<${i}>${y(a)}</${i}>`}else{const e=d(x,void 0,!1);p+=`<${f}>${y(e)}</${f}>`}}}else{const e=g.children||[];if(e.some(e=>e&&("JSXText"===e.type||"JSXExpressionContainer"===e.type)&&-1!==c.indexOf(e))){const t=c.indexOf(g),n=d(e,void 0);p+=`<${t}>${y(n)}</${t}>`}else{const t=new Map,n=l(g);let i=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&&s.has(e)){const e=n.opening&&Array.isArray(n.opening.attributes)&&n.opening.attributes.length>0,s=n.children||[],r=1===s.length&&"JSXText"===s[0]?.type;!e&&(!s.length||r)||t.set(n,i++)}else t.set(n,i++)}const r=d(e,t.size?t:void 0);p+=`<${n}>${y(r)}</${n}>`}}continue}"JSXFragment"!==g.type||(p+=d(g.children||[]))}}return p}const g=d(e,void 0,!0),x=String(g).replace(/<br \/>\s*\n\s*/g,"<br />"),h=String(x),v=new Set;if(f&&f.size>0)for(let e=0;e<c.length;e++)f.has(c[e])&&v.add(e);let S=String(h);if(v&&v.size>0)for(const e of v)try{S=S.replace(new RegExp("\\s+<"+e+">","g"),"<"+e+">"),S=S.replace(new RegExp("<\\/"+e+">\\s+","g"),"</"+e+">")}catch(e){}S=S.replace(/<\/(\d+)>\s*\n\s*(\S)/g,(e,t,n)=>{const i=Number(t);return v.has(i)?`</${t}>${n}`:`</${t}> ${n}`}),S=S.replace(/(\S)\s*\n\s*<(\d+)/g,(e,t,n)=>{const i=Number(n);return v.has(i)?`${t}<${n}`:`${t} <${n}`}),S=S.replace(/\s*\n\s*/g," "),S=S.replace(/\s+/g," "),S=S.replace(/\s+([,;:!?.])/g,"$1");const m=S.trim();let J=String(m);if(v&&v.size>0)for(const e of v)try{J=J.replace(new RegExp("[\\s\\u00A0]+<"+e+">","g"),"<"+e+">"),J=J.replace(new RegExp("<\\/"+e+">[\\s\\u00A0]+","g"),"</"+e+">")}catch(e){}try{for(let e=0;e<c.length;e++){const t=c[e];if(!t||"JSXElement"!==t.type)continue;const n=c[e-1],i=c[e+1];if(!n||!i)continue;if("JSXText"!==n.type||"JSXText"!==i.type)continue;const s=String(n.value),r=String(i.value),o=s.replace(/\n\s*$/,""),p=/[A-Za-z0-9]$/.test(o),l=/^[A-Za-z0-9]/.test(r.trim()),a=/^[a-z]/.test(r.trim()),u=/\s\n/.test(s),f=r&&/^\s/.test(r)&&!/^\n/.test(r);if(p&&l&&a&&!u&&!f){const t=e;J=J.replace(new RegExp("\\s+<"+t+">","g"),"<"+t+">")}}}catch(e){}function X(e){if(!e||!e.includes("<"))return e;function t(e){const t=[],n=[];let i=0;const s=/<\/?(\d+)>|<[^>]+>/g;let r;for(;r=s.exec(e);){const o=r[0],p=r.index;if(p>i){const s={type:"text",text:e.slice(i,p)};n.length?n[n.length-1].node.children.push(s):t.push(s)}const l=/^<\/(\d+)>$/.exec(o),a=/^<(\d+)>$/.exec(o);if(a){const e=Number(a[1]),i={type:"ph",idx:e,children:[]};n.length?n[n.length-1].node.children.push(i):t.push(i),n.push({node:i,idx:e})}else if(l)n.length&&n.pop();else{const e={type:"text",text:o};n.length?n[n.length-1].node.children.push(e):t.push(e)}i=s.lastIndex}if(i<e.length){const s={type:"text",text:e.slice(i)};n.length?n[n.length-1].node.children.push(s):t.push(s)}return t}function n(e,t=null){let i="";for(const o of e)if("text"===o.type)i+=o.text;else{const e=o.children.filter(e=>"ph"===e.type);let p;if("number"==typeof o.idx){const t=c[o.idx];if(t&&Array.isArray(t.children)){const n=new Map;let i=0;for(const e of t.children){const t=c.indexOf(e);if("JSXText"===e.type){if(r(e))continue;-1!==t&&n.set(t,i),i++}else"JSXElement"===e.type?(-1!==t&&n.set(t,i),i++):"JSXExpressionContainer"===e.type&&-1!==t&&(n.set(t,i),i++)}e.every(e=>n.has(e.idx))&&(p=n)}}if(!p){const n=e.map(e=>e.idx);if(n.length<=1||n.every((e,t)=>0===t||e===n[t-1]+1)){p=new Map;let n=0;try{const i=c[o.idx];if(i&&i.span){const r=i.span.start,p=i.span.end;let l=0,a=-1;for(let e=o.idx+1;e<c.length;e++){const t=c[e];if(t&&t.span&&(t.span.start>=r&&t.span.end<=p&&"JSXElement"===t.type)){a=e;break}}if(-1!==a)for(let e=o.idx+1;e<a;e++){const t=c[e];t&&t.span&&(t.span.start>=r&&t.span.end<=p&&("JSXText"!==t.type&&"JSXExpressionContainer"!==t.type||l++))}else for(let e=o.idx+1;e<c.length;e++){const t=c[e];t&&t.span&&(t.span.start>=r&&t.span.end<=p&&("JSXText"!==t.type&&"JSXExpressionContainer"!==t.type||l++))}const u=i?.opening?.name?.value,f=u&&s.has(u),y=null===t;1===e.length&&(n=Math.max(1,l+1),!y||f||0!==e[0].idx||"a"!==u&&"p"!==u||(n=0))}}catch(e){}for(const t of e)p.has(t.idx)||p.set(t.idx,n++)}}const l=o.children.map(e=>{if("text"===e.type)return e.text;const t=e.idx,i=p&&p.has(t)?p.get(t):t;return`<${i}>${n(e.children,i)}</${i}>`}).join("");i+=`<${o.idx}>${l}</${o.idx}>`}return i}try{const i=t(e);return n(i)}catch(t){return e}}return J=X(J),J.trim()}(s.children,r);let J;const X="JSXAttribute"===p?.type?i(p):void 0;if(void 0!==X)J=X;else{const e=r.extract.defaultValue;J="string"==typeof e?e:""}let E,$;if("JSXAttribute"===o?.type){if("StringLiteral"===o.value?.type){if(E=o.value,$=E.value,!$||""===$.trim())return null;if(S&&"StringLiteral"===E.type){const e=r.extract.nsSeparator??":",t=E.value;if(e&&t.startsWith(`${S}${e}`)){if($=t.slice(`${S}${e}`.length),!$||""===$.trim())return null;E={...E,value:$}}}}else"JSXExpressionContainer"===o.value?.type&&"JSXEmptyExpression"!==o.value.expression.type&&(E=o.value.expression);if(!E)return null}return p||!$||m.trim()?!p&&m.trim()&&(J=m):J=$,{keyExpression:E,serializedChildren:m,ns:S,defaultValue:J,hasCount:u,isOrdinal:g,contextExpression:h,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)}};
|
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 r,loadConfig as a}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"@croct/json5-parser";import{runTypesGenerator as l}from"./types-generator.js";import{runSyncer as p}from"./syncer.js";import{runMigrator as d}from"./migrator.js";import{runInit as f}from"./init.js";import{runLinterCli as m}from"./linter.js";import{runStatus as u}from"./status.js";import{runLocizeSync as y,runLocizeDownload as g,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.33.
|
|
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 r,loadConfig as a}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"@croct/json5-parser";import{runTypesGenerator as l}from"./types-generator.js";import{runSyncer as p}from"./syncer.js";import{runMigrator as d}from"./migrator.js";import{runInit as f}from"./init.js";import{runLinterCli as m}from"./linter.js";import{runStatus as u}from"./status.js";import{runLocizeSync as y,runLocizeDownload as g,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.33.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.").option("--sync-all","Sync primary language values with default values from code AND clear synced keys in all other locales.").action(async o=>{try{const t=x.opts().config,i=await r(t),a=async()=>{const e=!!o.syncPrimary||!!o.syncAll,t=await s(i,{isWatchMode:!!o.watch,isDryRun:!!o.dryRun,syncPrimaryWithDefaults:e,syncAll:!!o.syncAll});return o.ci&&!t?(console.log("✅ No files were updated."),process.exit(0)):o.ci&&t&&(console.error("❌ Some files were updated. This should not happen in CI mode."),process.exit(1)),t};if(await a(),o.watch){console.log("\nWatching for changes...");const o=await z(i.extract.input),t=j(i.extract.ignore),r=v(i.extract.output),c=[...t,...r].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}`),a()})}}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 a(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 u(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 r(t),a=()=>l(i);if(await a(),o.watch){console.log("\nWatching for changes...");const o=await z(i.types?.input||[]),t=[...j(i.extract?.ignore)].filter(Boolean),r=o.filter(o=>!t.some(e=>n(o,e,{dot:!0})));e.watch(r,{persistent:!0}).on("change",o=>{console.log(`\nFile changed: ${o}`),a()})}}),x.command("sync").description("Synchronize secondary language files with the primary language file.").action(async()=>{const o=x.opts().config,e=await r(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 d(o)}),x.command("init").description("Create a new i18next.config.ts/js file with an interactive setup wizard.").action(f),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,r=async()=>{let o=await a(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 m(o)};if(await r(),o.watch){console.log("\nWatching for changes...");const o=await a(t);if(o?.extract?.input){const t=await z(o.extract.input),i=[...j(o.extract.ignore),...v(o.extract.output)].filter(Boolean),a=t.filter(o=>!i.some(e=>n(o,e,{dot:!0})));e.watch(a,{ignored:/node_modules/,persistent:!0}).on("change",o=>{console.log(`\nFile changed: ${o}`),r()})}}}),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.").option("--cdn-type <standard|pro>","Specify the cdn endpoint that should be used (depends on which cdn type you've in your locize project)").action(async o=>{const e=x.opts().config,t=await r(e);await y(t,o)}),x.command("locize-download").description("Download all translations from your locize project.").option("--cdn-type <standard|pro>","Specify the cdn endpoint that should be used (depends on which cdn type you've in your locize project)").action(async o=>{const e=x.opts().config,t=await r(e);await g(t,o)}),x.command("locize-migrate").description("Migrate local translation files to a new locize project.").option("--cdn-type <standard|pro>","Specify the cdn endpoint that should be used (depends on which cdn type you've in your locize project)").action(async o=>{const e=x.opts().config,t=await r(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,a=await r(n),c=await w(a,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]:[],v=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()))};export{x as program};
|
|
@@ -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){if(e)return"StringLiteral"===e.type?e.value:"TemplateLiteral"===e.type&&n(e)?e.quasis?.[0]?.cooked:"ParenExpression"===e.type&&e.expr?s(e.expr):"ParenthesizedExpression"===e.type&&e.expression||"TsAsExpression"===e.type&&e.expression?s(e.expression):void 0}function r(e){return"StringLiteral"===e.value?.type?e.value.value:"JSXExpressionContainer"===e.value?.type?i(e.value.expression):void 0}function o(n,o){const p=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"i18nKey"===e.name.value),l=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),c=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"values"===e.name.value);let u;a||"JSXAttribute"!==c?.type||"JSXExpressionContainer"!==c.value?.type||"ObjectExpression"!==c.value.expression.type||(u=e(c.value.expression,"count"));const f=!!a||!!u,y=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"tOptions"===e.name.value),d="JSXAttribute"===y?.type&&"JSXExpressionContainer"===y.value?.type&&"ObjectExpression"===y.value.expression.type?y.value.expression:void 0,g=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),x=!!g,h=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"context"===e.name.value);let v="JSXAttribute"===h?.type&&"JSXExpressionContainer"===h.value?.type?h.value.expression:"JSXAttribute"===h?.type&&"StringLiteral"===h.value?.type?h.value:void 0;const S=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ns"===e.name.value);let m;m="JSXAttribute"===S?.type?r(S):void 0,d&&(void 0===m&&(m=t(d,"ns")),void 0===v&&(v=e(d,"context")));const J=function(e,t){if(!e||0===e.length)return"";const n=new Set(t.extract.transKeepBasicHtmlNodesFor??["br","strong","i","p"]),r=e=>e&&"JSXText"===e.type&&/^\s*$/.test(e.value)&&e.value.includes("\n"),o=e=>e&&("JSXElement"===e.type||"JSXFragment"===e.type),p=(e,t)=>{for(let n=t;n<e.length;n++){const t=e[n];if(t&&("JSXText"!==t.type||!r(t)))return t}};function l(e,t,s=!1,a=!1){if(!e||!e.length)return;const c=a&&e.filter(e=>e&&"JSXElement"===e.type&&"p"===e.opening?.name?.value).length>1;let u=0,f=e.length-1;for(;u<=f&&r(e[u]);)u++;for(;f>=u&&r(e[f]);)f--;const y=u<=f?e.slice(u,f+1):[],d=y.some(e=>e&&("JSXElement"===e.type||"JSXFragment"===e.type));for(let e=0;e<y.length;e++){const u=y[e];if(u)if("JSXText"!==u.type){if("JSXExpressionContainer"===u.type){if(s&&!d&&u.expression){const e=u.expression.type;if("ObjectExpression"===e)continue;if(void 0!==i(u.expression))continue;if("Identifier"===e||"MemberExpression"===e||"CallExpression"===e)continue}const n=i(u.expression);if(void 0!==n){const i=/^\s*$/.test(n)&&!n.includes("\n"),s=y[e-1],l=y[e+1];if(i){const n=p(y,e+1);if(o(s)&&o(n));else{const n=y[e+2];if(l&&"JSXText"===l.type&&r(l)&&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(s&&("JSXElement"===s.type||"JSXFragment"===s.type)&&l&&"JSXText"===l.type&&r(l))continue;const i=!l||"JSXText"===l.type&&!r(l);if(s&&"JSXText"===s.type&&i){const e=t[t.length-1];if(e&&"JSXText"===e.type){e.value=String(e.value)+u.expression.value;continue}}if(s&&("JSXElement"===s.type||"JSXFragment"===s.type)&&l&&"JSXText"===l.type&&r(l))continue}}}t.push(u);continue}if("JSXElement"===u.type){const e=u.opening&&u.opening.name&&"Identifier"===u.opening.name.type?u.opening.name.value:void 0;if(e&&n.has(e)){const s=u.opening&&Array.isArray(u.opening.attributes)&&u.opening.attributes.length>0,o=u.children||[],p=1===o.length&&("JSXText"===o[0]?.type||"JSXExpressionContainer"===o[0]?.type&&void 0!==i(o[0].expression)),f=!o.length,d=p;if(s&&!p)t.push(u),l(u.children||[],t,!0);else if(f)t.push(u);else if(!d){if((u.children||[]).some(e=>"JSXExpressionContainer"===e.type&&void 0===i(e.expression)))t.push(u),l(u.children||[],t,!0);else if("p"===e){const e=(o||[]).some(e=>{if(!e||"JSXElement"!==e.type)return!1;const t=e.opening&&e.opening.name&&"Identifier"===e.opening.name.type?e.opening.name.value:void 0,s=e.opening&&Array.isArray(e.opening.attributes)&&e.opening.attributes.length>0,r=e.children||[],o=1===r.length&&("JSXText"===r[0]?.type||"JSXExpressionContainer"===r[0]?.type&&void 0!==i(r[0].expression));return!(t&&n.has(t)&&!s&&(0===r.length||o))}),s=a&&y.some(e=>e&&e!==u&&"JSXText"===e.type&&!r(e));c||a&&e&&!s?(t.push(u),l(u.children||[],t,!0,!1)):l(u.children||[],t,!1,!1)}else l(u.children||[],t,!1,!1)}continue}t.push(u),l(u.children||[],t,!0);continue}"JSXFragment"!==u.type||l(u.children||[],t,s)}else{if(s&&!d)continue;if(s&&r(u))continue;if(r(u)){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],r=y[e-1];if(s){if(r&&"JSXExpressionContainer"===r.type)continue;if("JSXText"===s.type&&r&&"JSXText"===r.type){s.value=String(s.value)+u.value;continue}}}if(s&&d&&0===e)continue;t.push(u)}}}const a=[];function c(e){if(!e||!e.length)return!1;let t=!1;for(const n of e)if(n)if("JSXElement"!==n.type){if("JSXExpressionContainer"===n.type&&-1!==a.indexOf(n))return t;if("JSXText"===n.type&&-1!==a.indexOf(n)){if(r(n))continue;if(!t)return!0;const i=e.indexOf(n);if(e.slice(i+1).some(e=>e&&"JSXElement"===e.type))return!0}}else t=!0;return!1}l(e,a,!1,!0);const u=new Set,f=e=>String(e).replace(/^\s*\n\s*/g,"").replace(/\s*\n\s*$/g,"");function y(e,t,o=!1){if(!e||0===e.length)return"";let p="";const l=e=>{if(!e)return-1;if(t&&t.has(e))return t.get(e);if(t)for(const[n,i]of t.entries())try{if(n&&e&&n.span&&e.span&&n.span.start===e.span.start&&n.span.end===e.span.end)return i}catch(e){}return a.indexOf(e)},d=t=>{if(!o)return a.indexOf(t);const n=e.indexOf(t);if(-1===n)return-1;let i=0;for(let t=0;t<n;t++){const n=e[t];n&&("JSXText"===n.type?r(n)||i++:"JSXExpressionContainer"===n.type?n.expression&&"JSXEmptyExpression"!==n.expression.type&&i++:"JSXElement"===n.type&&i++)}return i};for(let t=0;t<e.length;t++){const g=e[t];if(g){if("JSXText"===g.type){if(r(g))continue;const i=e[t+1],s=e[t-1];if(s&&"JSXElement"===s.type){const i="Identifier"===s.opening?.name?.type?s.opening.name.value:void 0,r=i&&n.has(i),o=0===(s.children||[]).length;if(r&&o&&/^\s*\n\s*/.test(g.value)){const e=g.value.replace(/^\s*\n\s*/,"");if(e){p+=e;continue}continue}if(!r&&/^\s*\n\s*/.test(g.value)){const n=g.value.replace(/^\s*\n\s*/,"");if(n){if(s&&"JSXElement"===s.type&&Array.isArray(s.children)&&0===s.children.length){p+=n;continue}const i=e[t-2];if(i&&"JSXText"===i.type){const e=i.value.replace(/\n\s*$/,""),t=/[A-Za-z0-9]$/.test(e),s=/^[A-Za-z0-9]/.test(n),r=/^[a-z]/.test(n);if(t&&s&&r){p+=n;continue}}p+=" "+n;continue}continue}}if(/\n\s*$/.test(g.value)&&i&&"JSXElement"===i.type){const s=g.value.replace(/\n\s*$/,"");if(s.trim()){const o="Identifier"===i.opening?.name?.type?i.opening.name.value:void 0,l=o&&n.has(o),a=(i.children||[]).length>0,c=/\s\n/.test(g.value),f=e[t+2],y=f&&"JSXText"===f.type&&!r(f)&&/[a-zA-Z0-9]/.test(f.value),d=!!(i.opening&&Array.isArray(i.opening.attributes)&&i.opening.attributes.length>0),x=/^\s/.test(s)&&!/^\n/.test(s),h=s.trim(),v=x?" "+h:h,S=/[A-Za-z0-9]$/.test(h),m=f&&"string"==typeof f.value&&/^[A-Za-z0-9]/.test(f.value.trim()),J=f&&"string"==typeof f.value&&/^[a-z]/.test(f.value.trim()),X=f&&"string"==typeof f.value&&/^\s/.test(f.value)&&!/^\n/.test(f.value),$=d&&a&&y&&!(S&&m&&J&&!c&&!x&&!X),E=f&&"string"==typeof f.value&&/^[,;:!?.]/.test(f.value.trim()),b=$&&!E;S&&m&&J&&!c&&!x&&!X&&u.add(i),p+=c||l&&a||!l&&y&&x||b?v+" ":v;continue}}p+=g.value;continue}if("JSXExpressionContainer"===g.type){const e=g.expression;if(!e)continue;const t=i(e);if(void 0!==t)p+=t;else if("Identifier"===e.type)p+=`{{${e.value}}}`;else if("TsAsExpression"===e.type&&"ObjectExpression"===e.expression?.type){const t=e.expression,n=t.properties.filter(e=>"KeyValueProperty"===e.type&&e.key&&"Identifier"===e.key.type).map(e=>e.key.value);if(n.length>0)p+=n.map(e=>`{{${e}}}`).join("");else{const e=t.properties[0];if(!e||"Identifier"!==e.type)throw new Error("Unrecognized expression in JSX placeholder");p+=`{{${e.value}}}`}}else if("ObjectExpression"===e.type){const t=e.properties[0];if(t&&"KeyValueProperty"===t.type&&t.key&&"Identifier"===t.key.type)p+=`{{${t.key.value}}}`;else{if(!t||"Identifier"!==t.type)throw new Error("Unrecognized expression in JSX placeholder");p+=`{{${t.value}}}`}}else if("MemberExpression"===e.type&&e.property&&"Identifier"===e.property.type)p+=`{{${e.property.value}}}`;else if("CallExpression"===e.type&&"Identifier"===e.callee?.type)p+=`{{${e.callee.value}}}`;else if("ConditionalExpression"===e.type){const t=s(e.consequent),n=s(e.alternate);p+=void 0!==t&&void 0!==n?t.length>=n.length?t:n:void 0!==t?t:void 0!==n?n:""}else if("JSXEmptyExpression"!==e.type)throw new Error(`Unrecognized expression in JSX placeholder: ${e.type}`);continue}if("JSXElement"===g.type){const s=o?d(g):void 0;let u;if(g.opening&&g.opening.name&&"Identifier"===g.opening.name.type&&(u=g.opening.name.value),u&&n.has(u)){const d=g.opening&&Array.isArray(g.opening.attributes)&&g.opening.attributes.length>0,x=g.children||[],h=x.length>0,v=1===x.length&&("JSXText"===x[0]?.type||"JSXExpressionContainer"===x[0]?.type&&void 0!==i(x[0].expression)),S="p"===u;let m=0;if(S&&o&&(m=e.filter(e=>e&&"JSXElement"===e.type&&"p"===e.opening?.name?.value).length),!h||v){const i=v?y(x,void 0):"";if(""!==String(i).trim())if(n.has(u)&&"p"!==u)p+=`<${u}>${f(i)}</${u}>`;else if(S&&o&&m>1)p+=`<${u}>${f(i)}</${u}>`;else if(S){const e=o&&void 0!==s?s:a.indexOf(g);p+=`<${e}>${f(i)}</${e}>`}else{const e=o&&void 0!==s?s:a.indexOf(g);p+=`<${e}>${f(i)}</${e}>`}else{const n=e[t-1];n&&"JSXText"===n.type&&/\n\s*$/.test(n.value)&&(p=p.replace(/\s+$/,"")),p+=`<${u} />`}}else if(d&&!v){const e=x,t=o&&void 0!==s?s:l(g);if(c(e)){const s=new Map;let r=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||"JSXExpressionContainer"===n[0]?.type&&void 0!==i(n[0].expression));!e&&(!n.length||o)||s.set(t,r++)}else s.set(t,r++)}const o=y(e,s.size?s:void 0);p+=`<${t}>${f(o)}</${t}>`}else{const e=new Map;let s=0;for(const t of x)if(t&&"JSXElement"===t.type){const r=t.opening&&t.opening.name&&"Identifier"===t.opening.name.type?t.opening.name.value:void 0;if(r&&n.has(r)){const n=t.opening&&Array.isArray(t.opening.attributes)&&t.opening.attributes.length>0,r=t.children||[],o=1===r.length&&("JSXText"===r[0]?.type||"JSXExpressionContainer"===r[0]?.type&&void 0!==i(r[0].expression));!n&&(!r.length||o)||e.set(t,s++)}else e.set(t,s++)}const r=y(x,e.size?e:void 0);p+=`<${t}>${f(r)}</${t}>`}}else{const e=a.indexOf(g);if(-1!==e){const t=void 0!==s?s:e;if((()=>{let e=!1;for(const t of x)if(t)if("JSXElement"!==t.type){if("JSXExpressionContainer"===t.type&&-1!==a.indexOf(t))return e;if("JSXText"===t.type&&-1!==a.indexOf(t)){if(r(t))continue;if(!e)return!0;if(x.slice(x.indexOf(t)+1).some(e=>e&&"JSXElement"===e.type))return!0}}else e=!0;return!1})()){if(0===t){const e=y(x,void 0,!1);p+=`<${t}>${f(e)}</${t}>`;continue}const e=new Map;for(const e of x)if(e&&"JSXElement"===e.type){const s=e.opening&&e.opening.name&&"Identifier"===e.opening.name.type?e.opening.name.value:void 0;if(s&&n.has(s)){const n=e.opening&&Array.isArray(e.opening.attributes)&&e.opening.attributes.length>0,s=e.children||[],r=1===s.length&&("JSXText"===s[0]?.type||"JSXExpressionContainer"===s[0]?.type&&void 0!==i(s[0].expression));if(!(!n&&(!s.length||r))){const n=y(e.children,void 0,!1);p+=`<${t}>${f(n)}</${t}>`}}else{const n=y(e.children,void 0,!1);p+=`<${t}>${f(n)}</${t}>`}}const s=y(x,e.size?e:void 0,!1);p+=`<${t}>${f(s)}</${t}>`;continue}const o=new Map;let l=0;for(const e of x)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,l++)}else o.set(e,l++)}const c=y(x,o.size>0?o:void 0,!1);p+=`<${t}>${f(c)}</${t}>`}else{const e=y(x,void 0,!1);p+=`<${u}>${f(e)}</${u}>`}}}else{const e=g.children||[];if(e.some(e=>e&&("JSXText"===e.type||"JSXExpressionContainer"===e.type)&&-1!==a.indexOf(e))){const t=a.indexOf(g),n=y(e,void 0);p+=`<${t}>${f(n)}</${t}>`}else{const t=new Map,i=l(g);let s=0;for(const i of e)if(i&&"JSXElement"===i.type){const e=i.opening&&i.opening.name&&"Identifier"===i.opening.name.type?i.opening.name.value:void 0;if(e&&n.has(e)){const e=i.opening&&Array.isArray(i.opening.attributes)&&i.opening.attributes.length>0,n=i.children||[],r=1===n.length&&"JSXText"===n[0]?.type;!e&&(!n.length||r)||t.set(i,s++)}else t.set(i,s++)}const r=y(e,t.size?t:void 0);p+=`<${i}>${f(r)}</${i}>`}}continue}"JSXFragment"!==g.type||(p+=y(g.children||[]))}}return p}const d=y(e,void 0,!0),g=String(d).replace(/<br \/>\s*\n\s*/g,"<br />"),x=String(g),h=new Set;if(u&&u.size>0)for(let e=0;e<a.length;e++)u.has(a[e])&&h.add(e);let v=String(x);if(h&&h.size>0)for(const e of h)try{v=v.replace(new RegExp("\\s+<"+e+">","g"),"<"+e+">"),v=v.replace(new RegExp("<\\/"+e+">\\s+","g"),"</"+e+">")}catch(e){}v=v.replace(/<\/(\d+)>\s*\n\s*(\S)/g,(e,t,n)=>{const i=Number(t);return h.has(i)?`</${t}>${n}`:`</${t}> ${n}`}),v=v.replace(/(\S)\s*\n\s*<(\d+)/g,(e,t,n)=>{const i=Number(n);return h.has(i)?`${t}<${n}`:`${t} <${n}`}),v=v.replace(/\s*\n\s*/g," "),v=v.replace(/\s+/g," "),v=v.replace(/\s+([,;:!?.])/g,"$1");const S=v.trim();let m=String(S);if(h&&h.size>0)for(const e of h)try{m=m.replace(new RegExp("[\\s\\u00A0]+<"+e+">","g"),"<"+e+">"),m=m.replace(new RegExp("<\\/"+e+">[\\s\\u00A0]+","g"),"</"+e+">")}catch(e){}try{for(let e=0;e<a.length;e++){const t=a[e];if(!t||"JSXElement"!==t.type)continue;const n=a[e-1],i=a[e+1];if(!n||!i)continue;if("JSXText"!==n.type||"JSXText"!==i.type)continue;const s=String(n.value),r=String(i.value),o=s.replace(/\n\s*$/,""),p=/[A-Za-z0-9]$/.test(o),l=/^[A-Za-z0-9]/.test(r.trim()),c=/^[a-z]/.test(r.trim()),u=/\s\n/.test(s),f=r&&/^\s/.test(r)&&!/^\n/.test(r);if(p&&l&&c&&!u&&!f){const t=e;m=m.replace(new RegExp("\\s+<"+t+">","g"),"<"+t+">")}}}catch(e){}function J(e){if(!e||!e.includes("<"))return e;function t(e){const t=[],n=[];let i=0;const s=/<\/?(\d+)>|<[^>]+>/g;let r;for(;r=s.exec(e);){const o=r[0],p=r.index;if(p>i){const s={type:"text",text:e.slice(i,p)};n.length?n[n.length-1].node.children.push(s):t.push(s)}const l=/^<\/(\d+)>$/.exec(o),a=/^<(\d+)>$/.exec(o);if(a){const e=Number(a[1]),i={type:"ph",idx:e,children:[]};n.length?n[n.length-1].node.children.push(i):t.push(i),n.push({node:i,idx:e})}else if(l)n.length&&n.pop();else{const e={type:"text",text:o};n.length?n[n.length-1].node.children.push(e):t.push(e)}i=s.lastIndex}if(i<e.length){const s={type:"text",text:e.slice(i)};n.length?n[n.length-1].node.children.push(s):t.push(s)}return t}function i(e,t=null){let s="";for(const o of e)if("text"===o.type)s+=o.text;else{const e=o.children.filter(e=>"ph"===e.type);let p;if("number"==typeof o.idx){const t=a[o.idx];if(t&&Array.isArray(t.children)){const n=new Map;let i=0;for(const e of t.children){const t=a.indexOf(e);if("JSXText"===e.type){if(r(e))continue;-1!==t&&n.set(t,i),i++}else"JSXElement"===e.type?(-1!==t&&n.set(t,i),i++):"JSXExpressionContainer"===e.type&&-1!==t&&(n.set(t,i),i++)}e.every(e=>n.has(e.idx))&&(p=n)}}if(!p){const i=e.map(e=>e.idx);if(i.length<=1||i.every((e,t)=>0===t||e===i[t-1]+1)){p=new Map;let i=0;try{const s=a[o.idx];if(s&&s.span){const r=s.span.start,p=s.span.end;let l=0,c=-1;for(let e=o.idx+1;e<a.length;e++){const t=a[e];if(t&&t.span&&(t.span.start>=r&&t.span.end<=p&&"JSXElement"===t.type)){c=e;break}}if(-1!==c)for(let e=o.idx+1;e<c;e++){const t=a[e];t&&t.span&&(t.span.start>=r&&t.span.end<=p&&("JSXText"!==t.type&&"JSXExpressionContainer"!==t.type||l++))}else for(let e=o.idx+1;e<a.length;e++){const t=a[e];t&&t.span&&(t.span.start>=r&&t.span.end<=p&&("JSXText"!==t.type&&"JSXExpressionContainer"!==t.type||l++))}const u=s?.opening?.name?.value,f=u&&n.has(u),y=null===t;1===e.length&&(i=Math.max(1,l+1),!y||f||0!==e[0].idx||"a"!==u&&"p"!==u||(i=0))}}catch(e){}for(const t of e)p.has(t.idx)||p.set(t.idx,i++)}}const l=o.children.map(e=>{if("text"===e.type)return e.text;const t=e.idx,n=p&&p.has(t)?p.get(t):t;return`<${n}>${i(e.children,n)}</${n}>`}).join("");s+=`<${o.idx}>${l}</${o.idx}>`}return s}try{const n=t(e);return i(n)}catch(t){return e}}return m=J(m),m.trim()}(n.children,o);let X;const $="JSXAttribute"===l?.type?r(l):void 0;if(void 0!==$)X=$;else{const e=o.extract.defaultValue;X="string"==typeof e?e:""}let E,b;if("JSXAttribute"===p?.type){if("StringLiteral"===p.value?.type){if(E=p.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"===p.value?.type&&"JSXEmptyExpression"!==p.value.expression.type&&(E=p.value.expression);if(!E)return null}l||!b||J.trim()?!l&&J.trim()&&(X=J):X=b;return{keyExpression:E,serializedChildren:J,ns:m,defaultValue:X,hasCount:f,isOrdinal:x,contextExpression:v,optionsNode:d,explicitDefault:void 0!==$||(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)}}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){if(e)return"StringLiteral"===e.type?e.value:"TemplateLiteral"===e.type&&n(e)?e.quasis?.[0]?.cooked:"ParenExpression"===e.type&&e.expr?s(e.expr):"ParenthesizedExpression"===e.type&&e.expression||"TsAsExpression"===e.type&&e.expression?s(e.expression):void 0}function r(e){return"StringLiteral"===e.value?.type?e.value.value:"JSXExpressionContainer"===e.value?.type?i(e.value.expression):void 0}function o(n,o){const p=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"i18nKey"===e.name.value),l=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),c=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"values"===e.name.value);let f;a||"JSXAttribute"!==c?.type||"JSXExpressionContainer"!==c.value?.type||"ObjectExpression"!==c.value.expression.type||(f=e(c.value.expression,"count"));const u=!!a||!!f,y=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"tOptions"===e.name.value),d="JSXAttribute"===y?.type&&"JSXExpressionContainer"===y.value?.type&&"ObjectExpression"===y.value.expression.type?y.value.expression:void 0,g=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),x=!!g,h=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"context"===e.name.value);let v="JSXAttribute"===h?.type&&"JSXExpressionContainer"===h.value?.type?h.value.expression:"JSXAttribute"===h?.type&&"StringLiteral"===h.value?.type?h.value:void 0;const S=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ns"===e.name.value);let m;m="JSXAttribute"===S?.type?r(S):void 0,d&&(void 0===m&&(m=t(d,"ns")),void 0===v&&(v=e(d,"context")));const J=function(e,t){if(!e||0===e.length)return"";const n=new Set(t.extract.transKeepBasicHtmlNodesFor??["br","strong","i","p"]),r=e=>e&&"JSXText"===e.type&&/^\s*$/.test(e.value)&&e.value.includes("\n"),o=e=>e&&("JSXElement"===e.type||"JSXFragment"===e.type),p=(e,t)=>{for(let n=t;n<e.length;n++){const t=e[n];if(t&&("JSXText"!==t.type||!r(t)))return t}},l=e=>{let t=e;for(;t;)if("ParenExpression"===t.type&&t.expr)t=t.expr;else if("ParenthesizedExpression"===t.type&&t.expression)t=t.expression;else{if("TsAsExpression"!==t.type||!t.expression)break;t=t.expression}return t};function a(e,t,c=!1,f=!1){if(!e||!e.length)return;const u=f&&e.filter(e=>e&&"JSXElement"===e.type&&"p"===e.opening?.name?.value).length>1;let y=0,d=e.length-1;for(;y<=d&&r(e[y]);)y++;for(;d>=y&&r(e[d]);)d--;const g=y<=d?e.slice(y,d+1):[],x=g.some(e=>e&&("JSXElement"===e.type||"JSXFragment"===e.type));for(let e=0;e<g.length;e++){const y=g[e];if(y)if("JSXText"!==y.type){if("JSXExpressionContainer"===y.type){if(c&&!x&&y.expression){const e=l(y.expression),t=e?.type;if("ObjectExpression"===t)continue;if(void 0!==s(e))continue;if("Identifier"===t||"MemberExpression"===t||"CallExpression"===t)continue}const n=i(y.expression);if(void 0!==n){const i=/^\s*$/.test(n)&&!n.includes("\n"),s=g[e-1],l=g[e+1];if(i){const n=p(g,e+1);if(o(s)&&o(n));else{const n=g[e+2];if(l&&"JSXText"===l.type&&r(l)&&n&&("JSXElement"===n.type||"JSXFragment"===n.type)){const t=g[e-1],n=g[e-2];if(!t||"JSXText"!==t.type&&n&&"JSXExpressionContainer"===n.type)continue}if(s&&("JSXElement"===s.type||"JSXFragment"===s.type)&&l&&"JSXText"===l.type&&r(l))continue;const i=!l||"JSXText"===l.type&&!r(l);if(s&&"JSXText"===s.type&&i){const e=t[t.length-1];if(e&&"JSXText"===e.type){e.value=String(e.value)+y.expression.value;continue}}if(s&&("JSXElement"===s.type||"JSXFragment"===s.type)&&l&&"JSXText"===l.type&&r(l))continue}}}t.push(y);continue}if("JSXElement"===y.type){const e=y.opening&&y.opening.name&&"Identifier"===y.opening.name.type?y.opening.name.value:void 0;if(e&&n.has(e)){const s=y.opening&&Array.isArray(y.opening.attributes)&&y.opening.attributes.length>0,o=y.children||[],p=1===o.length&&("JSXText"===o[0]?.type||"JSXExpressionContainer"===o[0]?.type&&void 0!==i(o[0].expression)),l=!o.length,c=p;if(s&&!p)t.push(y),a(y.children||[],t,!0);else if(l)t.push(y);else if(!c){if((y.children||[]).some(e=>"JSXExpressionContainer"===e.type&&void 0===i(e.expression)))t.push(y),a(y.children||[],t,!0);else if("p"===e){const e=(o||[]).some(e=>{if(!e||"JSXElement"!==e.type)return!1;const t=e.opening&&e.opening.name&&"Identifier"===e.opening.name.type?e.opening.name.value:void 0,s=e.opening&&Array.isArray(e.opening.attributes)&&e.opening.attributes.length>0,r=e.children||[],o=1===r.length&&("JSXText"===r[0]?.type||"JSXExpressionContainer"===r[0]?.type&&void 0!==i(r[0].expression));return!(t&&n.has(t)&&!s&&(0===r.length||o))}),s=f&&g.some(e=>e&&e!==y&&"JSXText"===e.type&&!r(e));u||f&&e&&!s?(t.push(y),a(y.children||[],t,!0,!1)):a(y.children||[],t,!1,!1)}else a(y.children||[],t,!1,!1)}continue}t.push(y),a(y.children||[],t,!0);continue}"JSXFragment"!==y.type||a(y.children||[],t,c)}else{if(c&&!x)continue;if(c&&r(y))continue;if(r(y)){const n=g[e-1],i=g[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=g[e-1];if(s){if(r&&"JSXExpressionContainer"===r.type)continue;if("JSXText"===s.type&&r&&"JSXText"===r.type){s.value=String(s.value)+y.value;continue}}}if(c&&x&&0===e)continue;t.push(y)}}}const c=[];function f(e){if(!e||!e.length)return!1;let t=!1;for(const n of e)if(n)if("JSXElement"!==n.type){if("JSXExpressionContainer"===n.type&&-1!==c.indexOf(n))return t;if("JSXText"===n.type&&-1!==c.indexOf(n)){if(r(n))continue;if(!t)return!0;const i=e.indexOf(n);if(e.slice(i+1).some(e=>e&&"JSXElement"===e.type))return!0}}else t=!0;return!1}a(e,c,!1,!0);const u=new Set,y=e=>String(e).replace(/^\s*\n\s*/g,"").replace(/\s*\n\s*$/g,"");function d(e,t,o=!1){if(!e||0===e.length)return"";let p="";const l=e=>{if(!e)return-1;if(t&&t.has(e))return t.get(e);if(t)for(const[n,i]of t.entries())try{if(n&&e&&n.span&&e.span&&n.span.start===e.span.start&&n.span.end===e.span.end)return i}catch(e){}return c.indexOf(e)},a=t=>{if(!o)return c.indexOf(t);const n=e.indexOf(t);if(-1===n)return-1;let i=0;for(let t=0;t<n;t++){const n=e[t];n&&("JSXText"===n.type?r(n)||i++:"JSXExpressionContainer"===n.type?n.expression&&"JSXEmptyExpression"!==n.expression.type&&i++:"JSXElement"===n.type&&i++)}return i};for(let t=0;t<e.length;t++){const g=e[t];if(g){if("JSXText"===g.type){if(r(g))continue;const i=e[t+1],s=e[t-1];if(s&&"JSXElement"===s.type){const i="Identifier"===s.opening?.name?.type?s.opening.name.value:void 0,r=i&&n.has(i),o=0===(s.children||[]).length;if(r&&o&&/^\s*\n\s*/.test(g.value)){const e=g.value.replace(/^\s*\n\s*/,"");if(e){p+=e;continue}continue}if(!r&&/^\s*\n\s*/.test(g.value)){const n=g.value.replace(/^\s*\n\s*/,"");if(n){if(s&&"JSXElement"===s.type&&Array.isArray(s.children)&&0===s.children.length){p+=n;continue}const i=e[t-2];if(i&&"JSXText"===i.type){const e=i.value.replace(/\n\s*$/,""),t=/[A-Za-z0-9]$/.test(e),s=/^[A-Za-z0-9]/.test(n),r=/^[a-z]/.test(n);if(t&&s&&r){p+=n;continue}}p+=" "+n;continue}continue}}if(/\n\s*$/.test(g.value)&&i&&"JSXElement"===i.type){const s=g.value.replace(/\n\s*$/,"");if(s.trim()){const o="Identifier"===i.opening?.name?.type?i.opening.name.value:void 0,l=o&&n.has(o),a=(i.children||[]).length>0,c=/\s\n/.test(g.value),f=e[t+2],y=f&&"JSXText"===f.type&&!r(f)&&/[a-zA-Z0-9]/.test(f.value),d=!!(i.opening&&Array.isArray(i.opening.attributes)&&i.opening.attributes.length>0),x=/^\s/.test(s)&&!/^\n/.test(s),h=s.trim(),v=x?" "+h:h,S=/[A-Za-z0-9]$/.test(h),m=f&&"string"==typeof f.value&&/^[A-Za-z0-9]/.test(f.value.trim()),J=f&&"string"==typeof f.value&&/^[a-z]/.test(f.value.trim()),X=f&&"string"==typeof f.value&&/^\s/.test(f.value)&&!/^\n/.test(f.value),E=d&&a&&y&&!(S&&m&&J&&!c&&!x&&!X),$=f&&"string"==typeof f.value&&/^[,;:!?.]/.test(f.value.trim()),b=E&&!$;S&&m&&J&&!c&&!x&&!X&&u.add(i),p+=c||l&&a||!l&&y&&x||b?v+" ":v;continue}}p+=g.value;continue}if("JSXExpressionContainer"===g.type){const e=g.expression;if(!e)continue;const t=i(e);if(void 0!==t)p+=t;else if("Identifier"===e.type)p+=`{{${e.value}}}`;else if("TsAsExpression"===e.type&&"ObjectExpression"===e.expression?.type){const t=e.expression,n=t.properties.filter(e=>"KeyValueProperty"===e.type&&e.key&&"Identifier"===e.key.type).map(e=>e.key.value);if(n.length>0)p+=n.map(e=>`{{${e}}}`).join("");else{const e=t.properties[0];if(!e||"Identifier"!==e.type)throw new Error("Unrecognized expression in JSX placeholder");p+=`{{${e.value}}}`}}else if("ObjectExpression"===e.type){const t=e.properties[0];if(t&&"KeyValueProperty"===t.type&&t.key&&"Identifier"===t.key.type)p+=`{{${t.key.value}}}`;else{if(!t||"Identifier"!==t.type)throw new Error("Unrecognized expression in JSX placeholder");p+=`{{${t.value}}}`}}else if("MemberExpression"===e.type&&e.property&&"Identifier"===e.property.type)p+=`{{${e.property.value}}}`;else if("CallExpression"===e.type&&"Identifier"===e.callee?.type)p+=`{{${e.callee.value}}}`;else if("ConditionalExpression"===e.type){const t=s(e.consequent),n=s(e.alternate);p+=void 0!==t&&void 0!==n?t.length>=n.length?t:n:void 0!==t?t:void 0!==n?n:""}else if("JSXEmptyExpression"!==e.type)throw new Error(`Unrecognized expression in JSX placeholder: ${e.type}`);continue}if("JSXElement"===g.type){const s=o?a(g):void 0;let u;if(g.opening&&g.opening.name&&"Identifier"===g.opening.name.type&&(u=g.opening.name.value),u&&n.has(u)){const a=g.opening&&Array.isArray(g.opening.attributes)&&g.opening.attributes.length>0,x=g.children||[],h=x.length>0,v=1===x.length&&("JSXText"===x[0]?.type||"JSXExpressionContainer"===x[0]?.type&&void 0!==i(x[0].expression)),S="p"===u;let m=0;if(S&&o&&(m=e.filter(e=>e&&"JSXElement"===e.type&&"p"===e.opening?.name?.value).length),!h||v){const i=v?d(x,void 0):"";if(""!==String(i).trim())if(n.has(u)&&"p"!==u)p+=`<${u}>${y(i)}</${u}>`;else if(S&&o&&m>1)p+=`<${u}>${y(i)}</${u}>`;else if(S){const e=o&&void 0!==s?s:c.indexOf(g);p+=`<${e}>${y(i)}</${e}>`}else{const e=o&&void 0!==s?s:c.indexOf(g);p+=`<${e}>${y(i)}</${e}>`}else{const n=e[t-1];n&&"JSXText"===n.type&&/\n\s*$/.test(n.value)&&(p=p.replace(/\s+$/,"")),p+=`<${u} />`}}else if(a&&!v){const e=x,t=o&&void 0!==s?s:l(g);if(f(e)){const s=new Map;let r=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||"JSXExpressionContainer"===n[0]?.type&&void 0!==i(n[0].expression));!e&&(!n.length||o)||s.set(t,r++)}else s.set(t,r++)}const o=d(e,s.size?s:void 0);p+=`<${t}>${y(o)}</${t}>`}else{const e=new Map;let s=0;for(const t of x)if(t&&"JSXElement"===t.type){const r=t.opening&&t.opening.name&&"Identifier"===t.opening.name.type?t.opening.name.value:void 0;if(r&&n.has(r)){const n=t.opening&&Array.isArray(t.opening.attributes)&&t.opening.attributes.length>0,r=t.children||[],o=1===r.length&&("JSXText"===r[0]?.type||"JSXExpressionContainer"===r[0]?.type&&void 0!==i(r[0].expression));!n&&(!r.length||o)||e.set(t,s++)}else e.set(t,s++)}const r=d(x,e.size?e:void 0);p+=`<${t}>${y(r)}</${t}>`}}else{const e=c.indexOf(g);if(-1!==e){const t=void 0!==s?s:e;if((()=>{let e=!1;for(const t of x)if(t)if("JSXElement"!==t.type){if("JSXExpressionContainer"===t.type&&-1!==c.indexOf(t))return e;if("JSXText"===t.type&&-1!==c.indexOf(t)){if(r(t))continue;if(!e)return!0;if(x.slice(x.indexOf(t)+1).some(e=>e&&"JSXElement"===e.type))return!0}}else e=!0;return!1})()){if(0===t){const e=d(x,void 0,!1);p+=`<${t}>${y(e)}</${t}>`;continue}const e=new Map;for(const e of x)if(e&&"JSXElement"===e.type){const s=e.opening&&e.opening.name&&"Identifier"===e.opening.name.type?e.opening.name.value:void 0;if(s&&n.has(s)){const n=e.opening&&Array.isArray(e.opening.attributes)&&e.opening.attributes.length>0,s=e.children||[],r=1===s.length&&("JSXText"===s[0]?.type||"JSXExpressionContainer"===s[0]?.type&&void 0!==i(s[0].expression));if(!(!n&&(!s.length||r))){const n=d(e.children,void 0,!1);p+=`<${t}>${y(n)}</${t}>`}}else{const n=d(e.children,void 0,!1);p+=`<${t}>${y(n)}</${t}>`}}const s=d(x,e.size?e:void 0,!1);p+=`<${t}>${y(s)}</${t}>`;continue}const o=new Map;let l=0;for(const e of x)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,l++)}else o.set(e,l++)}const a=d(x,o.size>0?o:void 0,!1);p+=`<${t}>${y(a)}</${t}>`}else{const e=d(x,void 0,!1);p+=`<${u}>${y(e)}</${u}>`}}}else{const e=g.children||[];if(e.some(e=>e&&("JSXText"===e.type||"JSXExpressionContainer"===e.type)&&-1!==c.indexOf(e))){const t=c.indexOf(g),n=d(e,void 0);p+=`<${t}>${y(n)}</${t}>`}else{const t=new Map,i=l(g);let s=0;for(const i of e)if(i&&"JSXElement"===i.type){const e=i.opening&&i.opening.name&&"Identifier"===i.opening.name.type?i.opening.name.value:void 0;if(e&&n.has(e)){const e=i.opening&&Array.isArray(i.opening.attributes)&&i.opening.attributes.length>0,n=i.children||[],r=1===n.length&&"JSXText"===n[0]?.type;!e&&(!n.length||r)||t.set(i,s++)}else t.set(i,s++)}const r=d(e,t.size?t:void 0);p+=`<${i}>${y(r)}</${i}>`}}continue}"JSXFragment"!==g.type||(p+=d(g.children||[]))}}return p}const g=d(e,void 0,!0),x=String(g).replace(/<br \/>\s*\n\s*/g,"<br />"),h=String(x),v=new Set;if(u&&u.size>0)for(let e=0;e<c.length;e++)u.has(c[e])&&v.add(e);let S=String(h);if(v&&v.size>0)for(const e of v)try{S=S.replace(new RegExp("\\s+<"+e+">","g"),"<"+e+">"),S=S.replace(new RegExp("<\\/"+e+">\\s+","g"),"</"+e+">")}catch(e){}S=S.replace(/<\/(\d+)>\s*\n\s*(\S)/g,(e,t,n)=>{const i=Number(t);return v.has(i)?`</${t}>${n}`:`</${t}> ${n}`}),S=S.replace(/(\S)\s*\n\s*<(\d+)/g,(e,t,n)=>{const i=Number(n);return v.has(i)?`${t}<${n}`:`${t} <${n}`}),S=S.replace(/\s*\n\s*/g," "),S=S.replace(/\s+/g," "),S=S.replace(/\s+([,;:!?.])/g,"$1");const m=S.trim();let J=String(m);if(v&&v.size>0)for(const e of v)try{J=J.replace(new RegExp("[\\s\\u00A0]+<"+e+">","g"),"<"+e+">"),J=J.replace(new RegExp("<\\/"+e+">[\\s\\u00A0]+","g"),"</"+e+">")}catch(e){}try{for(let e=0;e<c.length;e++){const t=c[e];if(!t||"JSXElement"!==t.type)continue;const n=c[e-1],i=c[e+1];if(!n||!i)continue;if("JSXText"!==n.type||"JSXText"!==i.type)continue;const s=String(n.value),r=String(i.value),o=s.replace(/\n\s*$/,""),p=/[A-Za-z0-9]$/.test(o),l=/^[A-Za-z0-9]/.test(r.trim()),a=/^[a-z]/.test(r.trim()),f=/\s\n/.test(s),u=r&&/^\s/.test(r)&&!/^\n/.test(r);if(p&&l&&a&&!f&&!u){const t=e;J=J.replace(new RegExp("\\s+<"+t+">","g"),"<"+t+">")}}}catch(e){}function X(e){if(!e||!e.includes("<"))return e;function t(e){const t=[],n=[];let i=0;const s=/<\/?(\d+)>|<[^>]+>/g;let r;for(;r=s.exec(e);){const o=r[0],p=r.index;if(p>i){const s={type:"text",text:e.slice(i,p)};n.length?n[n.length-1].node.children.push(s):t.push(s)}const l=/^<\/(\d+)>$/.exec(o),a=/^<(\d+)>$/.exec(o);if(a){const e=Number(a[1]),i={type:"ph",idx:e,children:[]};n.length?n[n.length-1].node.children.push(i):t.push(i),n.push({node:i,idx:e})}else if(l)n.length&&n.pop();else{const e={type:"text",text:o};n.length?n[n.length-1].node.children.push(e):t.push(e)}i=s.lastIndex}if(i<e.length){const s={type:"text",text:e.slice(i)};n.length?n[n.length-1].node.children.push(s):t.push(s)}return t}function i(e,t=null){let s="";for(const o of e)if("text"===o.type)s+=o.text;else{const e=o.children.filter(e=>"ph"===e.type);let p;if("number"==typeof o.idx){const t=c[o.idx];if(t&&Array.isArray(t.children)){const n=new Map;let i=0;for(const e of t.children){const t=c.indexOf(e);if("JSXText"===e.type){if(r(e))continue;-1!==t&&n.set(t,i),i++}else"JSXElement"===e.type?(-1!==t&&n.set(t,i),i++):"JSXExpressionContainer"===e.type&&-1!==t&&(n.set(t,i),i++)}e.every(e=>n.has(e.idx))&&(p=n)}}if(!p){const i=e.map(e=>e.idx);if(i.length<=1||i.every((e,t)=>0===t||e===i[t-1]+1)){p=new Map;let i=0;try{const s=c[o.idx];if(s&&s.span){const r=s.span.start,p=s.span.end;let l=0,a=-1;for(let e=o.idx+1;e<c.length;e++){const t=c[e];if(t&&t.span&&(t.span.start>=r&&t.span.end<=p&&"JSXElement"===t.type)){a=e;break}}if(-1!==a)for(let e=o.idx+1;e<a;e++){const t=c[e];t&&t.span&&(t.span.start>=r&&t.span.end<=p&&("JSXText"!==t.type&&"JSXExpressionContainer"!==t.type||l++))}else for(let e=o.idx+1;e<c.length;e++){const t=c[e];t&&t.span&&(t.span.start>=r&&t.span.end<=p&&("JSXText"!==t.type&&"JSXExpressionContainer"!==t.type||l++))}const f=s?.opening?.name?.value,u=f&&n.has(f),y=null===t;1===e.length&&(i=Math.max(1,l+1),!y||u||0!==e[0].idx||"a"!==f&&"p"!==f||(i=0))}}catch(e){}for(const t of e)p.has(t.idx)||p.set(t.idx,i++)}}const l=o.children.map(e=>{if("text"===e.type)return e.text;const t=e.idx,n=p&&p.has(t)?p.get(t):t;return`<${n}>${i(e.children,n)}</${n}>`}).join("");s+=`<${o.idx}>${l}</${o.idx}>`}return s}try{const n=t(e);return i(n)}catch(t){return e}}return J=X(J),J.trim()}(n.children,o);let X;const E="JSXAttribute"===l?.type?r(l):void 0;if(void 0!==E)X=E;else{const e=o.extract.defaultValue;X="string"==typeof e?e:""}let $,b;if("JSXAttribute"===p?.type){if("StringLiteral"===p.value?.type){if($=p.value,b=$.value,!b||""===b.trim())return null;if(m&&"StringLiteral"===$.type){const e=o.extract.nsSeparator??":",t=$.value;if(e&&t.startsWith(`${m}${e}`)){if(b=t.slice(`${m}${e}`.length),!b||""===b.trim())return null;$={...$,value:b}}}}else"JSXExpressionContainer"===p.value?.type&&"JSXEmptyExpression"!==p.value.expression.type&&($=p.value.expression);if(!$)return null}l||!b||J.trim()?!l&&J.trim()&&(X=J):X=b;return{keyExpression:$,serializedChildren:J,ns:m,defaultValue:X,hasCount:u,isOrdinal:x,contextExpression:v,optionsNode:d,explicitDefault:void 0!==E||(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)}}export{o as extractFromTransComponent};
|