MCPcopy Create free account
hub / github.com/donghaxkim/react-rewrite / insertBetweenImmediateChildren

Function insertBetweenImmediateChildren

packages/cli/src/transform.ts:1200–1226  ·  view source on GitHub ↗
(
  node: any,
  leftChildIndex: number,
  rightChildIndex: number,
  insertion: string,
  existingBoundaryText = "",
)

Source from the content-addressed store, hash-verified

1198}
1199
1200function insertBetweenImmediateChildren(
1201 node: any,
1202 leftChildIndex: number,
1203 rightChildIndex: number,
1204 insertion: string,
1205 existingBoundaryText = "",
1206): boolean {
1207 const children = node.children;
1208 if (!children || !insertion) return false;
1209
1210 const leftChild = children[leftChildIndex];
1211 const rightChild = children[rightChildIndex];
1212 if (/^\s+$/.test(insertion) && /^\s*$/.test(existingBoundaryText)) {
1213 const totalSpaces = existingBoundaryText.length + insertion.length;
1214 children.splice(rightChildIndex, 0, ...createWhitespaceInsertionNodes(Math.max(1, totalSpaces)));
1215 return true;
1216 }
1217
1218 const preferExplicitBoundary = leftChild?.type !== "JSXText" || rightChild?.type !== "JSXText";
1219 if (!preferExplicitBoundary && rightChild?.type === "JSXText" && leftChildIndex + 1 === rightChildIndex) {
1220 rightChild.value = insertion + rightChild.value;
1221 return true;
1222 }
1223
1224 children.splice(rightChildIndex, 0, createInsertionNode(insertion));
1225 return true;
1226}
1227
1228/**
1229 * Replace text that spans across multiple child elements.

Callers 1

insertInJSXTextAtOffsetFunction · 0.85

Calls 2

createInsertionNodeFunction · 0.85

Tested by

no test coverage detected