Skip to content

Commit

Permalink
Fix default messages extractor bug with line break escaping (elastic#…
Browse files Browse the repository at this point in the history
…22140)

* Fix default messages extractor bug with line break escaping

* Change test case
  • Loading branch information
LeanidShutau committed Aug 21, 2018
1 parent a5f8740 commit d81ca08
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/dev/i18n/__snapshots__/utils.test.js.snap
@@ -0,0 +1,3 @@
// 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 "`;
3 changes: 2 additions & 1 deletion src/dev/i18n/utils.js
Expand Up @@ -31,6 +31,7 @@ import { promisify } from 'util';
const ESCAPE_LINE_BREAK_REGEX = /(?<!\\)\\\n/g;
const ESCAPE_SINGLE_QUOTE_REGEX = /\\([\s\S])|(')/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 @@ -63,7 +64,7 @@ export function formatJSString(string) {
return (string || '')
.replace(ESCAPE_LINE_BREAK_REGEX, '')
.replace(ESCAPE_SINGLE_QUOTE_REGEX, '\\$1$2')
.replace('\n', '\\n');
.replace(LINE_BREAK_REGEX, '\\n');
}

export function formatHTMLString(string) {
Expand Down
11 changes: 11 additions & 0 deletions src/dev/i18n/utils.test.js
Expand Up @@ -45,6 +45,17 @@ describe('i18n utils', () => {
expect(formatJSString('Test\\\n str\\\ning')).toEqual('Test string');
});

test('should escape linebreaks', () => {
expect(
formatJSString(`Text with
line-breaks and \n\n
\n\n
`)
).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 d81ca08

Please sign in to comment.