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

Function materializeBoundarySpaces

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

Source from the content-addressed store, hash-verified

1066}
1067
1068function materializeBoundarySpaces(node: any, source: string, lineStarts = getLineStarts(source)): void {
1069 const children = node.children;
1070 if (!children || children.length === 0) return;
1071
1072 for (const child of children) {
1073 if (child.type === "JSXElement") {
1074 materializeBoundarySpaces(child, source, lineStarts);
1075 }
1076 }
1077
1078 for (let i = 1; i < children.length; i++) {
1079 const previousChild = children[i - 1];
1080 const nextChild = children[i];
1081 const previousText = getRenderedChildText(previousChild, source, lineStarts);
1082 const nextText = getRenderedChildText(nextChild, source, lineStarts);
1083 const sourceBetween = getSourceBetween(previousChild, nextChild, source, lineStarts);
1084 const boundaryWhitespaceInChildren = Boolean(
1085 previousText &&
1086 nextText &&
1087 (previousChild.type !== "JSXText" || nextChild.type !== "JSXText") &&
1088 (/\s$/.test(previousText) || /^\s/.test(nextText))
1089 );
1090 const hasBoundaryWhitespace = Boolean(previousText && nextText) &&
1091 (/\s/.test(sourceBetween) || boundaryWhitespaceInChildren);
1092 if (!hasBoundaryWhitespace) continue;
1093
1094 const preferExplicitBoundary = previousChild.type !== "JSXText" || nextChild.type !== "JSXText";
1095 if (preferExplicitBoundary) {
1096 stripTrailingWhitespace(previousChild);
1097 stripLeadingWhitespace(nextChild);
1098 children.splice(i, 0, createSpaceExpressionNode());
1099 i++;
1100 continue;
1101 }
1102
1103 if (prependToChildText(nextChild, " ")) continue;
1104 if (appendToChildText(previousChild, " ")) continue;
1105
1106 children.splice(i, 0, { type: "JSXText", value: " " });
1107 i++;
1108 }
1109
1110 const firstTextLike = children.findIndex(isTextLikeChild);
1111 if (firstTextLike !== -1) {
1112 stripLeadingWhitespace(children[firstTextLike]);
1113 }
1114
1115 for (let i = children.length - 1; i >= 0; i--) {
1116 if (isTextLikeChild(children[i])) {
1117 stripTrailingWhitespace(children[i]);
1118 break;
1119 }
1120 }
1121
1122 for (let i = children.length - 1; i >= 0; i--) {
1123 if (isEmptyTextLikeChild(children[i])) {
1124 children.splice(i, 1);
1125 }

Callers

nothing calls this directly

Calls 11

getLineStartsFunction · 0.85
getRenderedChildTextFunction · 0.85
getSourceBetweenFunction · 0.85
stripTrailingWhitespaceFunction · 0.85
stripLeadingWhitespaceFunction · 0.85
prependToChildTextFunction · 0.85
appendToChildTextFunction · 0.85
isTextLikeChildFunction · 0.85
isEmptyTextLikeChildFunction · 0.85

Tested by

no test coverage detected