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

Function buildImmediateTextSegments

packages/cli/src/transform.ts:1135–1174  ·  view source on GitHub ↗
(parentNode: any, source?: string)

Source from the content-addressed store, hash-verified

1133 | { type: "boundary"; text: string; leftChildIndex: number; rightChildIndex: number };
1134
1135function buildImmediateTextSegments(parentNode: any, source?: string): ImmediateTextSegment[] {
1136 const children = parentNode.children;
1137 if (!children || children.length === 0) return [];
1138
1139 const lineStarts = source ? getLineStarts(source) : undefined;
1140 const segments: ImmediateTextSegment[] = [];
1141 let previousChildIndex: number | null = null;
1142 let previousText = "";
1143
1144 for (let i = 0; i < children.length; i++) {
1145 const childText = getRenderedChildText(children[i], source, lineStarts);
1146 if (!childText) continue;
1147
1148 if (previousChildIndex != null) {
1149 const boundarySource = getSourceBetween(children[previousChildIndex], children[i], source, lineStarts);
1150 if (needsBoundarySpace(previousText, childText, boundarySource)) {
1151 segments.push({ type: "boundary", text: " ", leftChildIndex: previousChildIndex, rightChildIndex: i });
1152 }
1153 }
1154
1155 segments.push({ type: "child", text: childText, childIndex: i });
1156 previousChildIndex = i;
1157 previousText = childText;
1158 }
1159
1160 const collapsedSegments: ImmediateTextSegment[] = [];
1161 let previousEndsWithWhitespace = false;
1162
1163 for (const segment of segments) {
1164 if (!segment.text) continue;
1165 const whitespaceOnly = /^[\t\n\f\r ]+$/.test(segment.text);
1166 if (whitespaceOnly && previousEndsWithWhitespace) {
1167 continue;
1168 }
1169 collapsedSegments.push(whitespaceOnly ? { ...segment, text: " " } : segment);
1170 previousEndsWithWhitespace = /\s$/.test(collapsedSegments[collapsedSegments.length - 1].text);
1171 }
1172
1173 return collapsedSegments;
1174}
1175
1176function getSegmentsText(segments: ImmediateTextSegment[]): string {
1177 return segments.map((segment) => segment.text).join("");

Callers 3

replaceCrossElementTextFunction · 0.85
insertInJSXTextAtOffsetFunction · 0.85

Calls 4

getLineStartsFunction · 0.85
getRenderedChildTextFunction · 0.85
getSourceBetweenFunction · 0.85
needsBoundarySpaceFunction · 0.85

Tested by

no test coverage detected