Skip to content

Commit

Permalink
[Tools] Fix pug regular expression (#24006) (#24122)
Browse files Browse the repository at this point in the history
* [Tools] Fix pug regular expression

* Add one more test
  • Loading branch information
LeanidShutau committed Oct 17, 2018
1 parent 4a4c91c commit b2ba7c3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
12 changes: 11 additions & 1 deletion src/dev/i18n/extractors/__snapshots__/pug.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`dev/i18n/extractors/pug extracts messages from pug template 1`] = `
exports[`dev/i18n/extractors/pug extracts messages from pug template with interpolation 1`] = `
Array [
"message-id",
Object {
"context": "Message context",
"message": "Default message",
},
]
`;

exports[`dev/i18n/extractors/pug extracts messages from pug template without interpolation 1`] = `
Array [
"message-id",
Object {
Expand Down
2 changes: 1 addition & 1 deletion src/dev/i18n/extractors/pug.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { createFailError } from '../../run';
/**
* Matches `i18n(...)` in `#{i18n('id', { defaultMessage: 'Message text' })}`
*/
const PUG_I18N_REGEX = /(?<=\#\{)i18n\((([^)']|'([^'\\]|\\.)*')*\)(?=\}))/g;
const PUG_I18N_REGEX = /i18n\((([^)']|'([^'\\]|\\.)*')*)\)/g;

/**
* Example: `#{i18n('message-id', { defaultMessage: 'Message text' })}`
Expand Down
13 changes: 11 additions & 2 deletions src/dev/i18n/extractors/pug.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import { extractPugMessages } from './pug';

describe('dev/i18n/extractors/pug', () => {
test('extracts messages from pug template', () => {
test('extracts messages from pug template with interpolation', () => {
const source = Buffer.from(`\
#{i18n('message-id', { defaultMessage: 'Default message', context: 'Message context' })}
`);
Expand All @@ -29,9 +29,18 @@ describe('dev/i18n/extractors/pug', () => {
expect(messageObject).toMatchSnapshot();
});

test('extracts messages from pug template without interpolation', () => {
const source = Buffer.from(`\
.kibanaWelcomeText(data-error-message=i18n('message-id', { defaultMessage: 'Default message', context: 'Message context' }))
`);
const [messageObject] = extractPugMessages(source);

expect(messageObject).toMatchSnapshot();
});

test('throws on empty id', () => {
const source = Buffer.from(`\
#{i18n('', { defaultMessage: 'Default message', context: 'Message context' })}
h1= i18n('', { defaultMessage: 'Default message', context: 'Message context' })
`);

expect(() => extractPugMessages(source).next()).toThrowErrorMatchingSnapshot();
Expand Down

0 comments on commit b2ba7c3

Please sign in to comment.