2829
return lowerExpressionToTemporary(builder, expression);
2830
}
2831
} else if (exprPath.isJSXText()) {
2832
+ const text = trimJsxText(exprPath.node.value);
2833
+ if (text === null) {
2834
+ return null;
2835
+ }
2836
const place = lowerValueToTemporary(builder, {
2837
kind: "JSXText",
2834
- value: exprPath.node.value,
2838
+ value: text,
2839
loc: exprLoc,
2840
});
2841
return place;
2855
}
2856
}
2857
2858
+/**
2859
+ * Trims whitespace according to the JSX spec:
2860
+ * > JSX removes whitespace at the beginning and ending of a line.
2861
+ * > It also removes blank lines. New lines adjacent to tags are removed;
2862
+ * > new lines that occur in the middle of string literals are condensed
2863
+ * > into a single space.
2864
+ *
2865
+ * From https://legacy.reactjs.org/docs/jsx-in-depth.html#string-literals-1
2866
+ *
2867
+ * Implementation adapted from Babel:
2868
+ * https://github.com/babel/babel/blob/54d30f206057be64b496d2da1ec8c49d244ba4e4/packages/babel-types/src/utils/react/cleanJSXElementLiteralChild.ts#L5
2869
+ */
2870
+function trimJsxText(original: string): string | null {
2871
+ const lines = original.split(/\r\n|\n|\r/);
2872
+
2873
+ let lastNonEmptyLine = 0;
2874
+
2875
+ for (let i = 0; i < lines.length; i++) {
2876
+ if (lines[i].match(/[^ \t]/)) {
2877
+ lastNonEmptyLine = i;
2878
+ }
2879
+ }
2880
+
2881
+ let str = "";
2882
+
2883
+ for (let i = 0; i < lines.length; i++) {
2884
+ const line = lines[i];
2885
+
2886
+ const isFirstLine = i === 0;
2887
+ const isLastLine = i === lines.length - 1;
2888
+ const isLastNonEmptyLine = i === lastNonEmptyLine;
2889
+
2890
+ // replace rendered whitespace tabs with spaces
2891
+ let trimmedLine = line.replace(/\t/g, " ");
2892
+
2893
+ // trim whitespace touching a newline
2894
+ if (!isFirstLine) {
2895
+ trimmedLine = trimmedLine.replace(/^[ ]+/, "");
2896
+ }
2897
+
2898
+ // trim whitespace touching an endline
2899
+ if (!isLastLine) {
2900
+ trimmedLine = trimmedLine.replace(/[ ]+$/, "");
2901
+ }
2902
+
2903
+ if (trimmedLine) {
2904
+ if (!isLastNonEmptyLine) {
2905
+ trimmedLine += " ";
2906
+ }
2907
+
2908
+ str += trimmedLine;
2909
+ }
2910
+ }
2911
+
2912
+ if (str.length !== 0) {
2913
+ return str;
2914
+ } else {
2915
+ return null;
2916
+ }
2917
+}
2918
+
2919
function lowerFunctionExpression(
2920
builder: HIRBuilder,
2921
expr: NodePath<t.FunctionExpression | t.ArrowFunctionExpression>