Skip to content

Commit

Permalink
[Tools] Fix line breaks in default JSON serializer (elastic#22653) (e…
Browse files Browse the repository at this point in the history
…lastic#22801)

* [Tools] Fix line breaks in default JSON serializer

* Add test for not escaped line breaks
  • Loading branch information
LeanidShutau authored Sep 7, 2018
1 parent d37abe1 commit da87111
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
7 changes: 6 additions & 1 deletion src/dev/i18n/__snapshots__/utils.test.js.snap
Original file line number Diff line number Diff line change
@@ -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
"
`;
3 changes: 1 addition & 2 deletions src/dev/i18n/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 4 additions & 9 deletions src/dev/i18n/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down

0 comments on commit da87111

Please sign in to comment.