Skip to content

Commit 16e99af

Browse files
author
Konstantinos Bairaktaris
committed
(Finally) fix the issue with how JSX handles newlines
1 parent 0dbcce5 commit 16e99af

File tree

6 files changed

+26
-15
lines changed

6 files changed

+26
-15
lines changed

packages/cli/src/api/parsers/babel.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,25 @@ function toStr(children, counter = 0) {
7373
}
7474
} else if (child.type === 'JSXText') {
7575
// Child is not a React element, append as-is
76-
const chunk = child.value.trim().replace(/\s+/g, ' ');
76+
let chunk = child.value;
77+
78+
// Try to mimic how JSX is parsed in runtime React
79+
const [startMatch] = /^[\s\n]*/.exec(child.value);
80+
if (startMatch.includes('\n')) {
81+
chunk = chunk.substring(startMatch.length);
82+
}
83+
84+
const [endMatch] = /[\s\n]*$/.exec(child.value);
85+
if (endMatch.includes('\n')) {
86+
chunk = chunk.substring(0, chunk.length - endMatch.length);
87+
}
88+
7789
if (chunk) { result.push(chunk); }
7890
} else if (
7991
child.type === 'JSXExpressionContainer'
8092
&& child.expression.type === 'StringLiteral'
8193
) {
82-
const chunk = child.expression.value.trim();
94+
const chunk = child.expression.value;
8395
if (chunk) { result.push(chunk); }
8496
} else {
8597
return [[], 0];
@@ -195,7 +207,7 @@ function babelExtractPhrases(HASHES, source, relativeFile, options) {
195207

196208
if (!string && elem.name.name === 'T' && node.children && node.children.length) {
197209
const [result] = toStr(node.children);
198-
string = result.join(' ').trim();
210+
string = result.join('');
199211
}
200212

201213
if (!string) return;

packages/cli/test/api/extract.hashedkeys.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,12 @@ describe('extractPhrases with hashed keys', () => {
158158
string: 'Text 5',
159159
meta: { context: [], tags: [], occurrences: ['react.jsx'] },
160160
},
161-
'404d0c0fef510bc89da7bc58ef160ccc': {
162-
string: 'Text <1> 6 </1>',
161+
'121b687b8625b4e58ba7f36dca77ad7f': {
162+
string: 'Text <1>6</1>',
163163
meta: { context: [], tags: [], occurrences: ['react.jsx'] },
164164
},
165-
'4f5fe2d7356c474bd2f4c03176c6bc45': {
166-
string: 'Text <1> <2> 7 </2> </1>',
165+
'3ed8a3c47f6a32ece9c9ae0c2a060d45': {
166+
string: 'Text <1><2>7</2></1>',
167167
meta: { context: [], tags: [], occurrences: ['react.jsx'] },
168168
},
169169
'1ecaf4c087b894bf86987fc2972ddba7': {

packages/cli/test/api/extract.sourcekeys.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,12 @@ describe('extractPhrases with source keys', () => {
151151
string: 'Text 5',
152152
meta: { context: [], tags: [], occurrences: ['react.jsx'] },
153153
},
154-
'Text <1> 6 </1>': {
155-
string: 'Text <1> 6 </1>',
154+
'Text <1>6</1>': {
155+
string: 'Text <1>6</1>',
156156
meta: { context: [], tags: [], occurrences: ['react.jsx'] },
157157
},
158-
'Text <1> <2> 7 </2> </1>': {
159-
string: 'Text <1> <2> 7 </2> </1>',
158+
'Text <1><2>7</2></1>': {
159+
string: 'Text <1><2>7</2></1>',
160160
meta: { context: [], tags: [], occurrences: ['react.jsx'] },
161161
},
162162
'Text 8::foo': {

packages/react/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ If you do this, the string that will be sent to Transifex for translation will
138138
look like this:
139139

140140
```
141-
A <1> button </1> and a <2> bold </2> walk into a bar
141+
A <1>button</1> and a <2>bold</2> walk into a bar
142142
```
143143

144144
As long as the translation respects the numbered tags, the T-component will

packages/react/src/components/T.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export default function T({ _str, children, ...props }) {
4949
if (!children) { return t(_str, props); }
5050

5151
const [templateArray, propsContainer] = toStr(children);
52-
const templateString = templateArray.join(' ').trim();
52+
const templateString = templateArray.join('');
5353
const translation = t(templateString, props);
5454

5555
const result = toElement(translation, propsContainer);

packages/react/src/utils/toStr.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ export function toStr(children, counter = 0) {
6464
// Child is not a React element, append as-is
6565
/* eslint-disable no-lonely-if */
6666
if (typeof child === 'string' || child instanceof String) {
67-
const chunk = child.trim().replace(/\s+/g, ' ');
68-
if (chunk) { result.push(chunk); }
67+
if (child) { result.push(child); }
6968
} else {
7069
result.push(child);
7170
}

0 commit comments

Comments
 (0)