[Tools] Fix line breaks in default JSON serializer (#22653)

* [Tools] Fix line breaks in default JSON serializer

* Add test for not escaped line breaks
This commit is contained in:
Leanid Shutau 2018-09-06 20:34:26 +03:00 committed by GitHub
parent 3d6de7cbc4
commit 4d83cfde56
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 12 deletions

View file

@ -1,3 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`i18n utils should escape linebreaks 1`] = `"Text with\\\\n\\\\n\\\\nline-breaks and \\\\n\\\\n\\\\n \\\\n\\\\n\\\\n "`;
exports[`i18n utils should not escape linebreaks 1`] = `
"Text
with
line-breaks
"
`;

View file

@ -30,7 +30,6 @@ import { promisify } from 'util';
const ESCAPE_LINE_BREAK_REGEX = /(?<!\\)\\\n/g;
const HTML_LINE_BREAK_REGEX = /[\s]*\n[\s]*/g;
const LINE_BREAK_REGEX = /\n/g;
export const readFileAsync = promisify(fs.readFile);
export const writeFileAsync = promisify(fs.writeFile);
@ -58,7 +57,7 @@ export function isI18nTranslateFunction(node) {
}
export function formatJSString(string) {
return (string || '').replace(ESCAPE_LINE_BREAK_REGEX, '').replace(LINE_BREAK_REGEX, '\\n');
return (string || '').replace(ESCAPE_LINE_BREAK_REGEX, '');
}
export function formatHTMLString(string) {

View file

@ -44,18 +44,13 @@ describe('i18n utils', () => {
test('should remove escaped linebreak', () => {
expect(formatJSString('Test\\\n str\\\ning')).toEqual('Test string');
});
test('should escape linebreaks', () => {
test('should not escape linebreaks', () => {
expect(
formatJSString(`Text with
line-breaks and \n\n
\n\n
`)
formatJSString(`Text \n with
line-breaks
`)
).toMatchSnapshot();
});
test('should detect i18n translate function call', () => {
let source = i18nTranslateSources[0];
let expressionStatementNode = [...traverseNodes(parse(source).program.body)].find(node =>