i18next-cli 1.23.6 → 1.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -295,9 +295,14 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
295
295
  n.value.includes('\n')
296
296
 
297
297
  // Build deterministic global slot list (pre-order)
298
- function collectSlots (nodes: any[], slots: any[], parentIsNonPreserved = false) {
298
+ function collectSlots (nodes: any[], slots: any[], parentIsNonPreserved = false, isRootLevel = false) {
299
299
  if (!nodes || !nodes.length) return
300
300
 
301
+ // Check if there are multiple <p> elements at root level
302
+ const multiplePAtRoot = isRootLevel && nodes.filter((n: any) =>
303
+ n && n.type === 'JSXElement' && n.opening?.name?.value === 'p'
304
+ ).length > 1
305
+
301
306
  // First, identify boundary whitespace nodes (start and end of sibling list)
302
307
  // We trim ONLY pure-whitespace JSXText nodes from the boundaries
303
308
  let startIdx = 0
@@ -504,16 +509,50 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
504
509
  ? n.opening.name.value
505
510
  : undefined
506
511
  if (tagName && allowedTags.has(tagName)) {
507
- // Count preserved HTML element as a global slot only when the AST
508
- // marks it self-closing (e.g. <br />). Self-closing preserved tags
509
- // should influence placeholder indexes (they appear inline without
510
- // children), while non-self-closing preserved tags (e.g. <strong>)
511
- // should not.
512
- const isAstSelfClosing = !!(n.opening && (n.opening as any).selfClosing)
513
- if (isAstSelfClosing) {
512
+ // Check if this preserved tag will actually be preserved as literal HTML
513
+ // or if it will be indexed (has complex children or attributes)
514
+ const hasAttrs =
515
+ n.opening &&
516
+ Array.isArray((n.opening as any).attributes) &&
517
+ (n.opening as any).attributes.length > 0
518
+ const children = n.children || []
519
+ // Check for single PURE text child (JSXText OR simple string expression)
520
+ const isSinglePureTextChild =
521
+ children.length === 1 && (
522
+ children[0]?.type === 'JSXText' ||
523
+ (children[0]?.type === 'JSXExpressionContainer' &&
524
+ getStringLiteralFromExpression(children[0].expression) !== undefined)
525
+ )
526
+
527
+ // Self-closing tags (no children) should be added to slots but rendered as literal HTML
528
+ // Tags with single pure text child should NOT be added to slots and rendered as literal HTML
529
+ const isSelfClosing = !children.length
530
+ const hasTextContent = isSinglePureTextChild
531
+
532
+ if (hasAttrs && !isSinglePureTextChild) {
533
+ // Has attributes AND complex children -> will be indexed, add to slots
534
+ slots.push(n)
535
+ collectSlots(n.children || [], slots, true)
536
+ } else if (isSelfClosing) {
537
+ // Self-closing tag with no attributes: add to slots (affects indexes) but will render as literal
514
538
  slots.push(n)
539
+ } else if (!hasTextContent) {
540
+ // Has complex children but no attributes
541
+ // For <p> tags at root level with multiple <p> siblings, index them
542
+ // For other preserved tags, preserve as literal (don't add to slots)
543
+ if (tagName === 'p' && multiplePAtRoot) {
544
+ slots.push(n)
545
+ collectSlots(n.children || [], slots, true, false)
546
+ } else {
547
+ // Other preserved tags: preserve as literal, don't add to slots
548
+ // But DO process children to add them to slots
549
+ collectSlots(n.children || [], slots, false, false)
550
+ }
551
+ } else {
552
+ // Has single pure text child and no attributes: preserve as literal, don't add to slots
553
+ // Don't process children either - they're part of the preserved tag
515
554
  }
516
- collectSlots(n.children || [], slots, false)
555
+ continue
517
556
  } else {
518
557
  // non-preserved element: the element itself is a single slot.
519
558
  // Pre-order: allocate the parent's slot first, then descend into its
@@ -537,7 +576,7 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
537
576
 
538
577
  // prepare the global slot list for the whole subtree
539
578
  const globalSlots: any[] = []
540
- collectSlots(children, globalSlots, false)
579
+ collectSlots(children, globalSlots, false, true)
541
580
 
542
581
  // Trim only newline-only indentation at the edges of serialized inner text.
543
582
  // This preserves single leading/trailing spaces which are meaningful between inline placeholders.
@@ -548,16 +587,52 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
548
587
  // remove trailing newline-only indentation
549
588
  .replace(/\s*\n\s*$/g, '')
550
589
 
551
- function visitNodes (nodes: any[], localIndexMap?: Map<any, number>): string {
590
+ function visitNodes (nodes: any[], localIndexMap?: Map<any, number>, isRootLevel = false): string {
552
591
  if (!nodes || nodes.length === 0) return ''
553
592
  let out = ''
554
593
 
594
+ // At root level, build index based on element position among siblings
595
+ let rootElementIndex = 0
596
+
555
597
  for (let i = 0; i < nodes.length; i++) {
556
598
  const node = nodes[i]
557
599
  if (!node) continue
558
600
 
559
601
  if (node.type === 'JSXText') {
560
602
  if (isFormattingWhitespace(node)) continue
603
+
604
+ const nextNode = nodes[i + 1]
605
+
606
+ // If this text node ends with newline+whitespace and is followed by an element,
607
+ if (/\n\s*$/.test(node.value) && nextNode && nextNode.type === 'JSXElement') {
608
+ const textWithoutTrailingNewline = node.value.replace(/\n\s*$/, '')
609
+ if (textWithoutTrailingNewline.trim()) {
610
+ // Check if there's text content AFTER the next element (not counting punctuation-only or formatting)
611
+ const nodeAfterNext = nodes[i + 2]
612
+ const hasTextAfter = nodeAfterNext &&
613
+ nodeAfterNext.type === 'JSXText' &&
614
+ !isFormattingWhitespace(nodeAfterNext) &&
615
+ // Check if it's not just punctuation (period, comma, etc.)
616
+ /[a-zA-Z0-9]/.test(nodeAfterNext.value)
617
+
618
+ // Preserve leading whitespace
619
+ const hasLeadingSpace = /^\s/.test(textWithoutTrailingNewline)
620
+ const trimmed = textWithoutTrailingNewline.trim()
621
+ const withLeading = hasLeadingSpace ? ' ' + trimmed : trimmed
622
+
623
+ // Add trailing space only if:
624
+ // 1. There was a space before the newline, OR
625
+ // 2. There's meaningful text (not just punctuation) after the next element
626
+ const hasSpaceBeforeNewline = /\s\n/.test(node.value)
627
+ if (hasSpaceBeforeNewline || hasTextAfter) {
628
+ out += withLeading + ' '
629
+ } else {
630
+ out += withLeading
631
+ }
632
+ continue
633
+ }
634
+ }
635
+
561
636
  out += node.value
562
637
  continue
563
638
  }
@@ -596,24 +671,195 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
596
671
  tag = node.opening.name.value
597
672
  }
598
673
 
674
+ // Track root element index for root-level elements
675
+ const myRootIndex = isRootLevel ? rootElementIndex : undefined
676
+ if (isRootLevel && node.type === 'JSXElement') {
677
+ rootElementIndex++
678
+ }
679
+
599
680
  if (tag && allowedTags.has(tag)) {
600
- const inner = visitNodes(node.children || [], localIndexMap)
601
- // consider element self-closing for rendering when AST marks it so or it has no meaningful children
602
- const isAstSelfClosing = !!(node.opening && (node.opening as any).selfClosing)
603
- const hasMeaningfulChildren = String(inner).trim() !== ''
604
-
605
- if (isAstSelfClosing || !hasMeaningfulChildren) {
606
- // If the previous original sibling is a JSXText that ends with a
607
- // newline (the tag was placed on its own indented line), trim any
608
- // trailing space we've accumulated so we don't leave " ... . <br/>".
609
- // This targeted trimming avoids breaking other spacing-sensitive cases.
610
- const prevOriginal = nodes[i - 1]
611
- if (prevOriginal && prevOriginal.type === 'JSXText' && /\n\s*$/.test(prevOriginal.value)) {
612
- out = out.replace(/\s+$/, '')
681
+ // Match react-i18next behavior: only preserve as literal HTML when:
682
+ // 1. No children (!childChildren) AND no attributes (!childPropsCount)
683
+ // 2. OR: Has children but only the children prop (childPropsCount === 1) AND children is a simple string (isString(childChildren))
684
+
685
+ const hasAttrs =
686
+ node.opening &&
687
+ Array.isArray((node.opening as any).attributes) &&
688
+ (node.opening as any).attributes.length > 0
689
+
690
+ const children = node.children || []
691
+ const hasChildren = children.length > 0
692
+
693
+ // Check if children is a single PURE text node (JSXText OR simple string expression)
694
+ const isSinglePureTextChild =
695
+ children.length === 1 && (
696
+ children[0]?.type === 'JSXText' ||
697
+ (children[0]?.type === 'JSXExpressionContainer' &&
698
+ getStringLiteralFromExpression(children[0].expression) !== undefined)
699
+ )
700
+
701
+ // Preserve as literal HTML in two cases:
702
+ // 1. No children and no attributes: <br />
703
+ // 2. Single pure text child (with or without attributes): <strong>text</strong> or <strong title="...">text</strong>
704
+ if ((!hasChildren || isSinglePureTextChild)) {
705
+ const inner = isSinglePureTextChild ? visitNodes(children, undefined) : ''
706
+ const hasMeaningfulChildren = String(inner).trim() !== ''
707
+
708
+ if (!hasMeaningfulChildren) {
709
+ // Self-closing
710
+ const prevOriginal = nodes[i - 1]
711
+ if (prevOriginal && prevOriginal.type === 'JSXText' && /\n\s*$/.test(prevOriginal.value)) {
712
+ out = out.replace(/\s+$/, '')
713
+ }
714
+ out += `<${tag} />`
715
+ } else {
716
+ // Preserve with content: <strong>text</strong>
717
+ out += `<${tag}>${inner}</${tag}>`
718
+ }
719
+ } else if (hasAttrs && !isSinglePureTextChild) {
720
+ // Has attributes -> treat as indexed element with numeric placeholder
721
+ const childrenLocal = children
722
+ const hasNonElementGlobalSlots = childrenLocal.some((ch: any) =>
723
+ ch && (ch.type === 'JSXText' || ch.type === 'JSXExpressionContainer') && globalSlots.indexOf(ch) !== -1
724
+ )
725
+
726
+ if (hasNonElementGlobalSlots) {
727
+ const idx = globalSlots.indexOf(node)
728
+ const inner = visitNodes(childrenLocal, undefined)
729
+ out += `<${idx}>${trimFormattingEdges(inner)}</${idx}>`
730
+ } else {
731
+ const childrenLocalMap = new Map<any, number>()
732
+ let localIdxCounter = 0
733
+ for (const ch of childrenLocal) {
734
+ if (!ch) continue
735
+ if (ch.type === 'JSXElement') {
736
+ const chTag = ch.opening && ch.opening.name && ch.opening.name.type === 'Identifier'
737
+ ? ch.opening.name.value
738
+ : undefined
739
+ if (chTag && allowedTags.has(chTag)) {
740
+ const chHasAttrs =
741
+ ch.opening &&
742
+ Array.isArray((ch.opening as any).attributes) &&
743
+ (ch.opening as any).attributes.length > 0
744
+ const chChildren = ch.children || []
745
+ const chIsSingleText = chChildren.length === 1 && chChildren[0]?.type === 'JSXText'
746
+ // Only skip indexing if it would be preserved as literal
747
+ if (!chHasAttrs && (!chChildren.length || chIsSingleText)) {
748
+ // Will be preserved, don't index
749
+ } else {
750
+ childrenLocalMap.set(ch, localIdxCounter++)
751
+ }
752
+ } else {
753
+ childrenLocalMap.set(ch, localIdxCounter++)
754
+ }
755
+ }
756
+ }
757
+
758
+ const idx = localIndexMap && localIndexMap.has(node) ? localIndexMap.get(node) : globalSlots.indexOf(node)
759
+ const inner = visitNodes(childrenLocal, childrenLocalMap.size ? childrenLocalMap : undefined)
760
+ out += `<${idx}>${trimFormattingEdges(inner)}</${idx}>`
613
761
  }
614
- out += `<${tag} />`
615
762
  } else {
616
- out += `<${tag}>${inner}</${tag}>`
763
+ // Has complex children but no attributes -> preserve tag as literal but index children
764
+ // Check if this tag is in globalSlots - if so, index it
765
+ const idx = globalSlots.indexOf(node)
766
+ if (idx !== -1) {
767
+ // This tag is in globalSlots, so index it
768
+ // At root level, use the element's position among root elements
769
+ const indexToUse = myRootIndex !== undefined ? myRootIndex : idx
770
+
771
+ // Check if children have text/expression nodes in globalSlots
772
+ // that appear BEFORE or BETWEEN element children (not just trailing)
773
+ // Exclude formatting whitespace (newline-only text) from this check
774
+ const hasNonElementGlobalSlots = (() => {
775
+ let foundElement = false
776
+
777
+ for (const ch of children) {
778
+ if (!ch) continue
779
+
780
+ if (ch.type === 'JSXElement') {
781
+ foundElement = true
782
+ continue
783
+ }
784
+
785
+ if (ch.type === 'JSXExpressionContainer' && globalSlots.indexOf(ch) !== -1) {
786
+ // Only count if before/between elements, not trailing
787
+ return foundElement // false if before first element, true if after
788
+ }
789
+
790
+ if (ch.type === 'JSXText' && globalSlots.indexOf(ch) !== -1) {
791
+ // Exclude formatting whitespace
792
+ if (isFormattingWhitespace(ch)) continue
793
+
794
+ // Only count text that appears BEFORE the first element
795
+ // Trailing text after all elements should not force global indexing
796
+ if (!foundElement) {
797
+ // Text before first element - counts
798
+ return true
799
+ }
800
+ // Text after an element - check if there are more elements after this text
801
+ const remainingNodes = children.slice(children.indexOf(ch) + 1)
802
+ const hasMoreElements = remainingNodes.some((n: any) => n && n.type === 'JSXElement')
803
+ if (hasMoreElements) {
804
+ // Text between elements - counts
805
+ return true
806
+ }
807
+ // Trailing text after last element - doesn't count
808
+ }
809
+ }
810
+
811
+ return false
812
+ })()
813
+
814
+ // If children have non-element global slots, use global indexes
815
+ // Otherwise use local indexes starting from parent's index + 1
816
+ if (hasNonElementGlobalSlots) {
817
+ const inner = visitNodes(children, undefined, false)
818
+ out += `<${indexToUse}>${trimFormattingEdges(inner)}</${indexToUse}>`
819
+ continue
820
+ }
821
+
822
+ // Build local index map for children of this indexed element
823
+ const childrenLocalMap = new Map<any, number>()
824
+ let localIdxCounter = indexToUse // Start from parent index (reuse parent's index for first child)
825
+ for (const ch of children) {
826
+ if (!ch) continue
827
+ if (ch.type === 'JSXElement') {
828
+ const chTag = ch.opening && ch.opening.name && ch.opening.name.type === 'Identifier'
829
+ ? ch.opening.name.value
830
+ : undefined
831
+
832
+ if (chTag && allowedTags.has(chTag)) {
833
+ // Check if this child will be preserved as literal HTML
834
+ const chHasAttrs =
835
+ ch.opening &&
836
+ Array.isArray((ch.opening as any).attributes) &&
837
+ (ch.opening as any).attributes.length > 0
838
+ const chChildren = ch.children || []
839
+ const chIsSinglePureText =
840
+ chChildren.length === 1 && (
841
+ chChildren[0]?.type === 'JSXText' ||
842
+ (chChildren[0]?.type === 'JSXExpressionContainer' &&
843
+ getStringLiteralFromExpression(chChildren[0].expression) !== undefined)
844
+ )
845
+ const chWillBePreserved = !chHasAttrs && (!chChildren.length || chIsSinglePureText)
846
+ if (!chWillBePreserved) {
847
+ // Will be indexed, add to local map
848
+ childrenLocalMap.set(ch, localIdxCounter++)
849
+ }
850
+ } else {
851
+ // Non-preserved tag, always indexed
852
+ childrenLocalMap.set(ch, localIdxCounter++)
853
+ }
854
+ }
855
+ }
856
+ const inner = visitNodes(children, childrenLocalMap.size > 0 ? childrenLocalMap : undefined, false)
857
+ out += `<${indexToUse}>${trimFormattingEdges(inner)}</${indexToUse}>`
858
+ } else {
859
+ // Not in globalSlots, preserve as literal HTML
860
+ const inner = visitNodes(children, undefined, false)
861
+ out += `<${tag}>${trimFormattingEdges(inner)}</${tag}>`
862
+ }
617
863
  }
618
864
  } else {
619
865
  // Decide whether to use local (restarted) indexes for this element's
@@ -643,8 +889,16 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
643
889
  ? ch.opening.name.value
644
890
  : undefined
645
891
  if (chTag && allowedTags.has(chTag)) {
646
- const isChSelfClosing = !!(ch.opening && (ch.opening as any).selfClosing)
647
- if (isChSelfClosing) {
892
+ // Check if this child will be preserved as literal HTML
893
+ const chHasAttrs =
894
+ ch.opening &&
895
+ Array.isArray((ch.opening as any).attributes) &&
896
+ (ch.opening as any).attributes.length > 0
897
+ const chChildren = ch.children || []
898
+ const chIsSinglePureText = chChildren.length === 1 && chChildren[0]?.type === 'JSXText'
899
+ const chWillBePreserved = !chHasAttrs && (!chChildren.length || chIsSinglePureText)
900
+ if (!chWillBePreserved) {
901
+ // Will be indexed, add to local map
648
902
  childrenLocalMap.set(ch, localIdxCounter++)
649
903
  }
650
904
  } else {
@@ -672,7 +926,7 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
672
926
  return out
673
927
  }
674
928
 
675
- const result = visitNodes(children)
929
+ const result = visitNodes(children, undefined, true)
676
930
 
677
931
  // Final cleanup in correct order:
678
932
  // 1. First, handle <br /> followed by whitespace+newline (boundary formatting)
package/src/index.ts CHANGED
@@ -4,7 +4,8 @@ export type {
4
4
  PluginContext,
5
5
  ExtractedKey,
6
6
  TranslationResult,
7
- ExtractedKeysMap
7
+ ExtractedKeysMap,
8
+ RenameKeyResult
8
9
  } from './types'
9
10
  export { defineConfig } from './config'
10
11
  export {
@@ -18,3 +19,4 @@ export { runLinter } from './linter'
18
19
  export { runSyncer } from './syncer'
19
20
  export { runStatus } from './status'
20
21
  export { runTypesGenerator } from './types-generator'
22
+ export { runRenameKey } from './rename-key'