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

Function canonicalizeEditedTextSubtree

packages/cli/src/transform.ts:974–1021  ·  view source on GitHub ↗
(node: any, source?: string, lineStarts = source ? getLineStarts(source) : undefined)

Source from the content-addressed store, hash-verified

972}
973
974function canonicalizeEditedTextSubtree(node: any, source?: string, lineStarts = source ? getLineStarts(source) : undefined): void {
975 const children = node.children;
976 if (!children || children.length === 0) return;
977
978 for (const child of children) {
979 if (child.type === "JSXElement") {
980 canonicalizeEditedTextSubtree(child, source, lineStarts);
981 }
982 }
983
984 const normalized: any[] = [];
985 let pendingBoundarySpace = false;
986
987 for (const child of children) {
988 if (isTextLikeChild(child)) {
989 const value = getTextLikeValue(child) ?? "";
990 if (!value) continue;
991
992 const { core, hasLeadingWhitespace, hasTrailingWhitespace } = trimTextLikeEdges(value);
993 if (!core) {
994 if (hasVisibleTextInEntries(normalized, source, lineStarts)) {
995 pendingBoundarySpace = true;
996 }
997 continue;
998 }
999
1000 setTextLikeValue(child, core);
1001 if ((pendingBoundarySpace || hasLeadingWhitespace) && hasVisibleTextInEntries(normalized, source, lineStarts)) {
1002 insertSingleVisibleBoundarySpace(normalized, child);
1003 }
1004
1005 normalized.push(child);
1006 pendingBoundarySpace = hasTrailingWhitespace;
1007 continue;
1008 }
1009
1010 const childText = getRenderedChildText(child, source, lineStarts);
1011 const childHasVisibleText = /\S/.test(childText);
1012 if ((pendingBoundarySpace || /^\s/.test(childText)) && childHasVisibleText && hasVisibleTextInEntries(normalized, source, lineStarts)) {
1013 insertSingleVisibleBoundarySpace(normalized, child);
1014 }
1015
1016 normalized.push(child);
1017 pendingBoundarySpace = childHasVisibleText && /\s$/.test(childText);
1018 }
1019
1020 node.children = mergeAdjacentTextLikeChildren(normalized);
1021}
1022
1023function canonicalizeVisibleWhitespace(node: any, source?: string, lineStarts = source ? getLineStarts(source) : undefined): void {
1024 const children = node.children;

Callers 1

finishFunction · 0.85

Calls 9

getLineStartsFunction · 0.85
isTextLikeChildFunction · 0.85
getTextLikeValueFunction · 0.85
trimTextLikeEdgesFunction · 0.85
hasVisibleTextInEntriesFunction · 0.85
setTextLikeValueFunction · 0.85
getRenderedChildTextFunction · 0.85

Tested by

no test coverage detected